Implementation of connectivity abstraction feature Release v0.3
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / inc / logger.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef _U_LOGGER_H_
22 #define _U_LOGGER_H_
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include "oic_logger.h"
28 #include "oic_console_logger.h"
29
30 #ifdef __ANDROID__
31 #include <android/log.h>
32 #elif defined(__TIZEN__)
33 #include <dlog.h>
34 #elif defined ARDUINO
35 #include "Arduino.h"
36 #include <avr/pgmspace.h>
37 #endif
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43
44 // Use the PCF macro to wrap strings stored in FLASH on the Arduino
45 // Example:  OC_LOG(INFO, TAG, PCF("Entering function"));
46 #ifdef ARDUINO
47 #define PCF(str)  ((PROGMEM const char *)(F(str)))
48 #else
49 #define PCF(str) str
50 #endif
51
52 // Max buffer size used in variable argument log function
53 #define MAX_LOG_V_BUFFER_SIZE (256)
54
55 // Log levels
56 #ifndef __TIZEN__
57 typedef enum
58 {
59     DEBUG = 0, INFO, WARNING, ERROR, FATAL
60 } LogLevel;
61 #else
62 #define DEBUG DLOG_DEBUG
63 #define INFO DLOG_INFO
64 #define WARNING DLOG_WARNING
65 #define ERROR DLOG_ERROR
66 #define FATAL DLOG_ERROR
67 #endif
68
69 #ifdef __TIZEN__
70 #define OICLog(level,tag,mes) LOG(level,tag,mes)
71 #define OICLogv(level,tag,fmt,args...) LOG(level,tag,fmt,##args)
72 #elif defined(ANDROID) || defined(__linux__)
73 /**
74  * Configure logger to use a context that defines a custom logger function
75  *
76  * @param ctx - pointer to oc_log_ctx_t struct that defines custom logging functions
77  */
78 void OICLogConfig(oic_log_ctx_t *ctx);
79
80 /**
81  * Initialize the logger.  Optional on Android and Linux.  Configures serial port on Arduino
82  */
83 void OICLogInit();
84
85 /**
86  * Called to Free dyamically allocated resources used with custom logging.
87  * Not necessary if default logging is used
88  *
89  */
90 void OICLogShutdown();
91
92 /**
93  * Output a log string with the specified priority level.
94  * Only defined for Linux and Android
95  *
96  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
97  * @param tag    - Module name
98  * @param logStr - log string
99  */
100 void OICLog(LogLevel level, const char *tag, const char *logStr);
101
102 /**
103  * Output a variable argument list log string with the specified priority level.
104  * Only defined for Linux and Android
105  *
106  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
107  * @param tag    - Module name
108  * @param format - variadic log string
109  */
110 void OICLogv(LogLevel level, const char *tag, const char *format, ...);
111
112 /**
113  * Output the contents of the specified buffer (in hex) with the specified priority level.
114  *
115  * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
116  * @param tag        - Module name
117  * @param buffer     - pointer to buffer of bytes
118  * @param bufferSize - max number of byte in buffer
119  */
120 void OICLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, uint16_t bufferSize);
121 #else  // For arduino platforms
122 /**
123  * Initialize the serial logger for Arduino
124  * Only defined for Arduino
125  */
126 void OICLogInit();
127
128 /**
129  * Output a log string with the specified priority level.
130  * Only defined for Arduino.  Uses PROGMEM strings
131  *
132  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
133  * @param tag    - Module name
134  * @param logStr - log string
135  */
136 void OICLog(LogLevel level, const char *tag, const int16_t lineNum, const char *logStr);
137
138 /**
139  * Output the contents of the specified buffer (in hex) with the specified priority level.
140  *
141  * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
142  * @param tag        - Module name
143  * @param buffer     - pointer to buffer of bytes
144  * @param bufferSize - max number of byte in buffer
145  */
146 void OICLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, uint16_t bufferSize);
147
148 /**
149  * Output a variable argument list log string with the specified priority level.
150  *
151  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
152  * @param tag    - Module name
153  * @param format - variadic log string
154  */
155 void OICLogv(LogLevel level, const char *tag, const int16_t lineNum, const char *format, ...);
156 #endif
157
158 #ifdef TB_LOG
159
160 #ifdef __TIZEN__
161 #define OIC_LOG(level,tag,mes) LOG_(LOG_ID_MAIN, level, tag, mes)
162 #define OIC_LOG_V(level,tag,fmt,args...) LOG_(LOG_ID_MAIN, level, tag, fmt,##args)
163 #else // These macros are defined for Linux, Android, and Arduino
164 #define OIC_LOG_INIT()    OICLogInit()
165 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)  OICLogBuffer((level), (tag), (buffer), (bufferSize))
166
167 #ifdef ARDUINO
168 #define OIC_LOG_CONFIG(ctx)
169 #define OIC_LOG_SHUTDOWN()
170 #define OIC_LOG(level, tag, logStr)  OICLog((level), (tag), __LINE__, (logStr))
171 #define OIC_LOG_V(level, tag, ...) OICLogv((level), (tag), __LINE__, __VA_ARGS__)
172 #else
173 #define OIC_LOG_CONFIG(ctx)    OICLogConfig((ctx))
174 #define OIC_LOG_SHUTDOWN()     OICLogShutdown()
175 #define OIC_LOG(level, tag, logStr)  OICLog((level), (tag), (logStr))
176 #define OIC_LOG_V(level, tag, ...) OICLogv((level), (tag), __VA_ARGS__)
177 #endif //ARDUINO
178 #endif //__TIZEN__
179 #else //TB_LOG
180
181 #define OIC_LOG_CONFIG(ctx)
182 #define OIC_LOG_SHUTDOWN()
183 #define OIC_LOG(level, tag, logStr)
184 #define OIC_LOG_V(level, tag, ...)
185 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)
186 #define OIC_LOG_INIT()
187 #endif
188
189 #ifdef __cplusplus
190 }
191 #endif // __cplusplus
192 #endif /* _U_LOGGER_H_ */