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