replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / logger / include / 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 LOGGER_H_
22 #define LOGGER_H_
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28 #include "logger_types.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 /**
45 * Helper for unused warning.
46 */
47 #ifndef UNUSED
48 #define UNUSED(x) (void)(x)
49 #endif
50
51
52 // Use the PCF macro to wrap strings stored in FLASH on the Arduino
53 // Example:  OIC_LOG(INFO, TAG, PCF("Entering function"));
54 #ifdef ARDUINO
55 #ifdef __cplusplus
56 #define PCF(str)  ((PROGMEM const char *)(F(str)))
57 #else
58 #define PCF(str)  ((PROGMEM const char *)(PSTR(str)))
59 #endif //__cplusplus
60 #else
61     #define PCF(str) str
62 #endif // ARDUINO
63
64 // Max buffer size used in variable argument log function
65 #define MAX_LOG_V_BUFFER_SIZE (256)
66
67 // Log levels
68 #ifdef __TIZEN__
69 typedef enum {
70     DEBUG = DLOG_INFO,    // DLOG_INFO : 4
71     INFO = DLOG_INFO,
72     WARNING = DLOG_WARN,  // DLOG_WARN : 5
73     ERROR = DLOG_ERROR,   // DLOG_ERROR : 6
74     FATAL = DLOG_ERROR,
75     DEBUG_LITE = DLOG_INFO,
76     INFO_LITE = DLOG_INFO,
77     INFO_PRIVATE = 255,
78 } LogLevel;
79 #else
80
81 /** @todo temporary work-around until better names with prefixes are used for the enum values. */
82 #ifdef ERROR
83 #undef ERROR
84 #endif
85
86 typedef enum {
87     DEBUG = 0,
88     INFO,
89     WARNING,
90     ERROR,
91     FATAL,
92     DEBUG_LITE,       // The DEBUG log for Lite device
93     INFO_LITE,        // The INFO log for Lite device
94     INFO_PRIVATE,     // The log contained private data
95 } LogLevel;
96
97 #endif // __TIZEN__
98
99 #ifdef SET_LOG_DEBUG
100 #define IF_OC_PRINT_LOG_LEVEL(level) if (DEBUG <= (level))
101 #elif defined(SET_LOG_INFO)
102 #define IF_OC_PRINT_LOG_LEVEL(level) if (INFO <= (level))
103 #elif defined(SET_LOG_ERROR)
104 #define IF_OC_PRINT_LOG_LEVEL(level) if (ERROR <= (level) && INFO_PRIVATE != (level))
105 #elif defined(SET_LOG_WARNING)
106 #define IF_OC_PRINT_LOG_LEVEL(level) if (WARNING <= (level) && INFO_PRIVATE != (level))
107 #elif defined(SET_LOG_FATAL)
108 #define IF_OC_PRINT_LOG_LEVEL(level) if (FATAL <= (level) && INFO_PRIVATE != (level))
109 #else
110 #define IF_OC_PRINT_LOG_LEVEL(level) if (DEBUG <= (level))
111 #endif
112
113 #define IF_OC_PRINT_PRIVATE_LOG_LEVEL(level) \
114     if (false == OCGetPrivateLogLevel() || (true == OCGetPrivateLogLevel() && INFO_PRIVATE != (level))) \
115
116 #define MAX_LOG_BUFFER_SIZE (4096)
117
118 /**
119  * Set log level and privacy log to print.
120  *
121  * @param level                   - log level.
122  * @param hidePrivateLogEntries   - Hide Private Log.
123  */
124 void OCSetLogLevel(LogLevel level, bool hidePrivateLogEntries);
125
126 /**
127  * get private log flag.
128  * @return  private log flag
129  */
130 bool OCGetPrivateLogLevel();
131
132 #ifdef __TIZEN__
133 /**
134  * Output the contents of the specified buffer (in hex) with the specified priority level.
135  *
136  * @param[in]    level      DEBUG, INFO, WARNING, ERROR, FATAL
137  * @param[in]    tag        Module name
138  * @param[in]    buffer     pointer to buffer of bytes
139  * @param[in]    bufferSize max number of byte in buffer
140  */
141 void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize);
142 void OCPrintCALogBuffer(LogLevel level, const char *tag, const uint8_t *buffer,
143                         uint16_t bufferSize, uint8_t isHeader);
144
145 #define OCLog(level,tag,mes) LOG_(LOG_ID_MAIN, (level), (tag), mes)
146 #define OCLogv(level,tag,fmt,args...) LOG_(LOG_ID_MAIN, (level),tag,fmt,##args)
147 #elif !defined(ARDUINO)
148     /**
149      * Configure logger to use a context that defines a custom logger function
150      *
151      * @param ctx - pointer to oc_log_ctx_t struct that defines custom logging functions
152      */
153     void OCLogConfig(oc_log_ctx_t *ctx);
154
155     /**
156      * Initialize the logger.  Optional on Android and Linux.  Configures serial port on Arduino
157      */
158     void OCLogInit();
159
160     /**
161      * Called to Free dyamically allocated resources used with custom logging.
162      * Not necessary if default logging is used
163      *
164      */
165     void OCLogShutdown();
166
167     /**
168      * Output a variable argument list log string with the specified priority level.
169      * Only defined for Linux and Android
170      *
171      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
172      * @param tag    - Module name
173      * @param format - variadic log string
174      */
175     void OCLogv(LogLevel level, const char * tag, const char * format, ...)
176 #if defined(__GNUC__)
177     __attribute__ ((format(printf, 3, 4)))
178 #endif
179     ;
180
181     /**
182      * Output a log string with the specified priority level.
183      * Only defined for Linux and Android
184      *
185      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
186      * @param tag    - Module name
187      * @param logStr - log string
188      */
189     void OCLog(LogLevel level, const char * tag, const char * logStr);
190
191     /**
192      * Output the contents of the specified buffer (in hex) with the specified priority level.
193      *
194      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
195      * @param tag        - Module name
196      * @param buffer     - pointer to buffer of bytes
197      * @param bufferSize - max number of byte in buffer
198      */
199     void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize);
200
201     void OCPrintCALogBuffer(LogLevel level, const char *tag, const uint8_t *buffer,
202                             uint16_t bufferSize, uint8_t isHeader);
203
204 #else  // For arduino platforms
205     /**
206      * Initialize the serial logger for Arduino
207      * Only defined for Arduino
208      */
209     void OCLogInit();
210
211     /**
212      * Output a log string with the specified priority level.
213      * Only defined for Arduino.  Uses PROGMEM strings
214      *
215      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
216      * @param tag    - Module name
217      * @param lineNum- line Number
218      * @param logStr - log string
219      */
220     void OCLog(LogLevel level, PROGMEM const char *tag, const int lineNum,
221                PROGMEM const char *logStr);
222
223     /**
224      * Output the contents of the specified buffer (in hex) with the specified priority level.
225      *
226      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
227      * @param tag        - Module name
228      * @param buffer     - pointer to buffer of bytes
229      * @param bufferSize - max number of byte in buffer
230      */
231     void OCLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, size_t bufferSize);
232
233     /**
234      * Output a variable argument list log string with the specified priority level.
235      *
236      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
237      * @param tag    - Module name
238      * @param lineNum- line Number
239      * @param format - variadic log string
240      */
241     void OCLogv(LogLevel level, PROGMEM const char *tag, const int lineNum,
242                 PROGMEM const char *format, ...)
243 #if defined(__GNUC__)
244     __attribute__ ((format(printf, 4, 5)))
245 #endif
246 ;
247 #endif
248
249 #ifdef TB_LOG
250
251 #ifdef __TIZEN__
252 #define OIC_LOG(level,tag,mes) \
253     do { \
254         IF_OC_PRINT_LOG_LEVEL((level)) \
255             IF_OC_PRINT_PRIVATE_LOG_LEVEL((level)) \
256             { \
257                 if ((level) == INFO_PRIVATE) \
258                     LOG_(LOG_ID_MAIN, DLOG_INFO, (tag), mes); \
259                 else \
260                     LOG_(LOG_ID_MAIN, (level), (tag), mes); \
261             } \
262     } while(0)
263
264 #define OIC_LOG_V(level,tag,fmt,args...) \
265     do { \
266         IF_OC_PRINT_LOG_LEVEL((level)) \
267             IF_OC_PRINT_PRIVATE_LOG_LEVEL((level)) \
268             { \
269                 if ((level) == INFO_PRIVATE) \
270                     LOG_(LOG_ID_MAIN, DLOG_INFO, (tag), fmt, ##args); \
271                 else \
272                     LOG_(LOG_ID_MAIN, (level), (tag), fmt, ##args); \
273             } \
274     } while(0)
275
276 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize) \
277     do { \
278         IF_OC_PRINT_LOG_LEVEL((level)) \
279             OCLogBuffer((level), (tag), (buffer), (bufferSize)); \
280     } while(0)
281
282 #define OIC_LOG_CA_BUFFER(level, tag, buffer, bufferSize, isHeader) \
283     do { \
284         IF_OC_PRINT_LOG_LEVEL((level)) \
285             OCPrintCALogBuffer((level), (tag), (buffer), (bufferSize), (isHeader)); \
286     } while(0)
287
288 #else // NO __TIZEN__ - These macros are defined for Linux, Android, Win32, and Arduino
289 #define OIC_LOG_INIT()    OCLogInit()
290
291 #ifdef ARDUINO
292 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize) \
293     do { \
294         IF_OC_PRINT_LOG_LEVEL((level)) \
295             OCLogBuffer((level), PCF(tag), (buffer), (bufferSize)); \
296     } while(0)
297
298 // Don't define variable argument log function for Arduino
299 #define OIC_LOG_V(level, tag, format, ...) \
300     do { \
301         IF_OC_PRINT_LOG_LEVEL((level)) \
302             OCLogv((level), PCF(tag), __LINE__, PCF(format),__VA_ARGS__); \
303     } while(0)
304
305 #define OIC_LOG_CONFIG(ctx)
306 #define OIC_LOG_SHUTDOWN()
307 #define OIC_LOG(level, tag, logStr) \
308     do { \
309         IF_OC_PRINT_LOG_LEVEL((level)) \
310             OCLog((level), PCF(tag), __LINE__, PCF(logStr)); \
311     } while(0)
312
313 #define OIC_LOG_V(level, tag, ...)
314
315 #else // NO ARDUINO
316 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize) \
317     do { \
318         IF_OC_PRINT_LOG_LEVEL((level)) \
319             OCLogBuffer((level), (tag), (buffer), (bufferSize)); \
320     } while(0)
321
322 #define OIC_LOG_CA_BUFFER(level, tag, buffer, bufferSize, isHeader) \
323     do { \
324         IF_OC_PRINT_LOG_LEVEL((level)) \
325             OCPrintCALogBuffer((level), (tag), (buffer), (bufferSize), (isHeader)); \
326     } while(0)
327
328 #define OIC_LOG_CONFIG(ctx)    OCLogConfig((ctx))
329 #define OIC_LOG_SHUTDOWN()     OCLogShutdown()
330 #define OIC_LOG(level, tag, logStr) \
331     do { \
332         IF_OC_PRINT_LOG_LEVEL((level)) \
333             OCLog((level), (tag), (logStr)); \
334     } while(0)
335
336 // Define variable argument log function for Linux, Android, and Win32
337 #define OIC_LOG_V(level, tag, ...) \
338     do { \
339         IF_OC_PRINT_LOG_LEVEL((level)) \
340             OCLogv((level), (tag), __VA_ARGS__); \
341     } while(0)
342
343 #endif // ARDUINO
344 #endif // __TIZEN__
345 #else // NO TB_LOG
346 #define OIC_LOG_CONFIG(ctx)
347 #define OIC_LOG_SHUTDOWN()
348 #define OIC_LOG(level, tag, logStr)
349 #define OIC_LOG_V(level, tag, ...)
350 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)
351 #define OIC_LOG_CA_BUFFER(level, tag, buffer, bufferSize, isHeader)
352 #define OIC_LOG_INIT()
353 #endif // TB_LOG
354
355 #ifdef __cplusplus
356 }
357 #endif // __cplusplus
358
359 #endif /* LOGGER_H_ */