Merge remote-tracking branch 'origin/routing-manager'
[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
48 #ifdef __cplusplus
49 #define PCF(str)  ((PROGMEM const char *)(F(str)))
50 #else
51 #define PCF(str)  ((PROGMEM const char *)(PSTR(str)))
52 #endif //__cplusplus
53
54 #else
55 #define PCF(str) str
56 #endif //ARDUINO
57
58 // Max buffer size used in variable argument log function
59 #define MAX_LOG_V_BUFFER_SIZE (256)
60
61 // Log levels
62 #ifdef __TIZEN__
63 typedef enum {
64     DEBUG = DLOG_DEBUG,
65     INFO = DLOG_INFO,
66     WARNING = DLOG_WARN,
67     ERROR = DLOG_ERROR,
68     FATAL = DLOG_ERROR
69 } LogLevel;
70 #else
71 typedef enum
72 {
73     DEBUG = 0, INFO, WARNING, ERROR, FATAL
74 } LogLevel;
75 #endif
76
77 #ifdef __TIZEN__
78 #define OC_LOG(level,tag,mes) LOG_(LOG_ID_MAIN, level, tag, mes)
79 #define OC_LOG_V(level,tag,fmt,args...) LOG_(LOG_ID_MAIN, level, tag, fmt,##args)
80 #define OC_LOG_BUFFER(level, tag, buffer, bufferSize)
81 #endif
82
83 #ifdef __TIZEN__
84 #define OICLog(level,tag,mes) LOG(level,tag,mes)
85 #define OICLogv(level,tag,fmt,args...) LOG(level,tag,fmt,##args)
86 #elif defined(ANDROID) || defined(__linux__) || defined(__APPLE__)
87 /**
88  * Configure logger to use a context that defines a custom logger function
89  *
90  * @param ctx - pointer to oc_log_ctx_t struct that defines custom logging functions
91  */
92 void OICLogConfig(oic_log_ctx_t *ctx);
93
94 /**
95  * Initialize the logger.  Optional on Android and Linux.  Configures serial port on Arduino
96  */
97 void OICLogInit();
98
99 /**
100  * Called to Free dyamically allocated resources used with custom logging.
101  * Not necessary if default logging is used
102  *
103  */
104 void OICLogShutdown();
105
106 /**
107  * Output a log string with the specified priority level.
108  * Only defined for Linux and Android
109  *
110  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
111  * @param tag    - Module name
112  * @param logStr - log string
113  */
114 void OICLog(LogLevel level, const char *tag, const char *logStr);
115
116 /**
117  * Output a variable argument list log string with the specified priority level.
118  * Only defined for Linux and Android
119  *
120  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
121  * @param tag    - Module name
122  * @param format - variadic log string
123  */
124 void OICLogv(LogLevel level, const char *tag, const char *format, ...);
125
126 /**
127  * Output the contents of the specified buffer (in hex) with the specified priority level.
128  *
129  * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
130  * @param tag        - Module name
131  * @param buffer     - pointer to buffer of bytes
132  * @param bufferSize - max number of byte in buffer
133  */
134 void OICLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, uint16_t bufferSize);
135 #else  // For arduino platforms
136 /**
137  * Initialize the serial logger for Arduino
138  * Only defined for Arduino
139  */
140 void OICLogInit();
141
142 /**
143  * Output a log string with the specified priority level.
144  * Only defined for Arduino.  Uses PROGMEM strings
145  *
146  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
147  * @param tag    - Module name
148  * @param logStr - log string
149  */
150 void OICLog(LogLevel level, PROGMEM const char *tag, const int16_t lineNum,
151               PROGMEM const char *logStr);
152
153 /**
154  * Output the contents of the specified buffer (in hex) with the specified priority level.
155  *
156  * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
157  * @param tag        - Module name
158  * @param buffer     - pointer to buffer of bytes
159  * @param bufferSize - max number of byte in buffer
160  */
161 void OICLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, uint16_t bufferSize);
162
163 /**
164  * Output a variable argument list log string with the specified priority level.
165  *
166  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
167  * @param tag    - Module name
168  * @param format - variadic log string
169  */
170 void OICLogv(LogLevel level, PROGMEM const char *tag, const int16_t lineNum,
171                PROGMEM const char *format, ...);
172 #endif
173
174 #ifdef TB_LOG
175
176 #ifdef __TIZEN__
177 #define OIC_LOG(level,tag,mes) LOG_(LOG_ID_MAIN, level, tag, mes)
178 #define OIC_LOG_V(level,tag,fmt,args...) LOG_(LOG_ID_MAIN, level, tag, fmt,##args)
179 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)
180 #else // These macros are defined for Linux, Android, and Arduino
181 #define OIC_LOG_INIT()    OICLogInit()
182 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)\
183     OICLogBuffer((level), (tag), (buffer), (bufferSize))
184
185 #ifdef ARDUINO
186 #define OIC_LOG_CONFIG(ctx)
187 #define OIC_LOG_SHUTDOWN()
188 #define OIC_LOG(level, tag, logStr) OICLog((level), PCF(tag), __LINE__, PCF(logStr))
189 #define OIC_LOG_V(level, tag, ...)
190 // To enable OIC_LOG_V, uncomment the below
191 //#define OIC_LOG_V(level, tag, format, ...)  OICLogv((level), PCF(tag), __LINE__, PCF(format), __VA_ARGS__)
192 #else
193 #define OIC_LOG_CONFIG(ctx)    OICLogConfig((ctx))
194 #define OIC_LOG_SHUTDOWN()     OICLogShutdown()
195 #define OIC_LOG(level, tag, logStr)  OICLog((level), (tag), (logStr))
196 #define OIC_LOG_V(level, tag, ...) OICLogv((level), (tag), __VA_ARGS__)
197 #endif //ARDUINO
198 #endif //__TIZEN__
199 #else //TB_LOG
200
201 #define OIC_LOG_CONFIG(ctx)
202 #define OIC_LOG_SHUTDOWN()
203 #define OIC_LOG(level, tag, logStr)
204 #define OIC_LOG_V(level, tag, ...)
205 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)
206 #define OIC_LOG_INIT()
207
208 #endif // TB_LOG
209
210
211 #ifdef __cplusplus
212 }
213 #endif // __cplusplus
214 #endif /* U_LOGGER_H_ */