Development of CoAP-HTTP Proxy
[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 #define IOTIVITY_VERSION "1.2.0"
25
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include "logger_types.h"
30
31 #ifdef __ANDROID__
32 #include <android/log.h>
33 #elif defined(__TIZEN__)
34 #include <dlog.h>
35 #elif defined(ARDUINO)
36 #include "Arduino.h"
37 #include "avr/pgmspace.h"
38 #endif
39
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44
45 /**
46 * Helper for unused warning.
47 */
48 #define UNUSED(x) (void)(x)
49
50 // Use the PCF macro to wrap strings stored in FLASH on the Arduino
51 // Example:  OIC_LOG(INFO, TAG, PCF("Entering function"));
52 #ifdef ARDUINO
53 #ifdef __cplusplus
54 #define PCF(str)  ((PROGMEM const char *)(F(str)))
55 #else
56 #define PCF(str)  ((PROGMEM const char *)(PSTR(str)))
57 #endif //__cplusplus
58 #else
59     #define PCF(str) str
60 #endif
61
62 // Max buffer size used in variable argument log function
63 #define MAX_LOG_V_BUFFER_SIZE (256)
64
65 // Log levels
66 #ifdef __TIZEN__
67 typedef enum {
68     DEBUG = DLOG_DEBUG,
69     INFO = DLOG_INFO,
70     WARNING = DLOG_WARN,
71     ERROR = DLOG_ERROR,
72     FATAL = DLOG_ERROR
73 } LogLevel;
74 #else
75
76 /** @todo temporary work-around until better names with prefixes are used for the enum values. */
77 #ifdef ERROR
78 #undef ERROR
79 #endif
80
81 typedef enum {
82     DEBUG = 0,
83     INFO,
84     WARNING,
85     ERROR,
86     FATAL
87 } LogLevel;
88 #endif
89
90 #ifdef __TIZEN__
91 /**
92  * Output the contents of the specified buffer (in hex) with the specified priority level.
93  *
94  * @param[in]    level      DEBUG, INFO, WARNING, ERROR, FATAL
95  * @param[in]    tag        Module name
96  * @param[in]    buffer     pointer to buffer of bytes
97  * @param[in]    bufferSize max number of byte in buffer
98  */
99 void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize);
100
101 #define OCLog(level,tag,mes) LOG_(LOG_ID_MAIN, (level), (tag), mes)
102 #define OCLogv(level,tag,fmt,args...) LOG_(LOG_ID_MAIN, (level),tag,fmt,##args)
103 #elif !defined(ARDUINO)
104     /**
105      * Configure logger to use a context that defines a custom logger function
106      *
107      * @param ctx - pointer to oc_log_ctx_t struct that defines custom logging functions
108      */
109     void OCLogConfig(oc_log_ctx_t *ctx);
110
111     /**
112      * Initialize the logger.  Optional on Android and Linux.  Configures serial port on Arduino
113      */
114     void OCLogInit();
115
116     /**
117      * Called to Free dyamically allocated resources used with custom logging.
118      * Not necessary if default logging is used
119      *
120      */
121     void OCLogShutdown();
122
123     /**
124      * Output a variable argument list log string with the specified priority level.
125      * Only defined for Linux and Android
126      *
127      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
128      * @param tag    - Module name
129      * @param format - variadic log string
130      */
131     void OCLogv(LogLevel level, const char * tag, const char * format, ...)
132 #if defined(__GNUC__)
133     __attribute__ ((format(printf, 3, 4)))
134 #endif
135     ;
136
137     /**
138      * Output a log string with the specified priority level.
139      * Only defined for Linux and Android
140      *
141      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
142      * @param tag    - Module name
143      * @param logStr - log string
144      */
145     void OCLog(LogLevel level, const char * tag, const char * logStr);
146
147     /**
148      * Output the contents of the specified buffer (in hex) with the specified priority level.
149      *
150      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
151      * @param tag        - Module name
152      * @param buffer     - pointer to buffer of bytes
153      * @param bufferSize - max number of byte in buffer
154      */
155     void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize);
156 #else  // For arduino platforms
157     /**
158      * Initialize the serial logger for Arduino
159      * Only defined for Arduino
160      */
161     void OCLogInit();
162
163     /**
164      * Output a log string with the specified priority level.
165      * Only defined for Arduino.  Uses PROGMEM strings
166      *
167      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
168      * @param tag    - Module name
169      * @param lineNum- line Number
170      * @param logStr - log string
171      */
172     void OCLog(LogLevel level, PROGMEM const char *tag, const int lineNum,
173                PROGMEM const char *logStr);
174
175     /**
176      * Output the contents of the specified buffer (in hex) with the specified priority level.
177      *
178      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
179      * @param tag        - Module name
180      * @param buffer     - pointer to buffer of bytes
181      * @param bufferSize - max number of byte in buffer
182      */
183     void OCLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, size_t bufferSize);
184
185     /**
186      * Output a variable argument list log string with the specified priority level.
187      *
188      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
189      * @param tag    - Module name
190      * @param lineNum- line Number
191      * @param format - variadic log string
192      */
193     void OCLogv(LogLevel level, PROGMEM const char *tag, const int lineNum,
194                 PROGMEM const char *format, ...)
195 #if defined(__GNUC__)
196     __attribute__ ((format(printf, 4, 5)))
197 #endif
198 ;
199 #endif
200
201 #ifdef TB_LOG
202
203 #ifdef __TIZEN__
204
205 #define OIC_LOG(level,tag,mes) LOG_(LOG_ID_MAIN, (level), (tag), mes)
206 #define OIC_LOG_V(level,tag,fmt,args...) LOG_(LOG_ID_MAIN, level, tag, fmt, ##args)
207 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)\
208     OCLogBuffer((level), (tag), (buffer), (bufferSize))
209
210 #else // These macros are defined for Linux, Android, Win32, and Arduino
211
212 #define OIC_LOG_INIT()    OCLogInit()
213
214 #ifdef ARDUINO
215
216 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)  OCLogBuffer((level), PCF(tag), (buffer), (bufferSize))
217 // Don't define variable argument log function for Arduino
218 #define OIC_LOG_V(level, tag, format, ...) OCLogv((level), PCF(tag), __LINE__, PCF(format),__VA_ARGS__)
219
220 #define OIC_LOG_CONFIG(ctx)
221 #define OIC_LOG_SHUTDOWN()
222 #define OIC_LOG(level, tag, logStr) OCLog((level), PCF(tag), __LINE__, PCF(logStr))
223 #define OIC_LOG_V(level, tag, ...)
224
225 #else
226
227 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)  OCLogBuffer((level), (tag), (buffer), (bufferSize))
228 #define OIC_LOG_CONFIG(ctx)    OCLogConfig((ctx))
229 #define OIC_LOG_SHUTDOWN()     OCLogShutdown()
230 #define OIC_LOG(level, tag, logStr)  OCLog((level), (tag), (logStr))
231 // Define variable argument log function for Linux, Android, and Win32
232 #define OIC_LOG_V(level, tag, ...)  OCLogv((level), (tag), __VA_ARGS__)
233
234 #endif //ARDUINO
235 #endif //__TIZEN__
236
237 #else //TB_LOG
238
239 #define OIC_LOG_CONFIG(ctx)
240 #define OIC_LOG_SHUTDOWN()
241 #define OIC_LOG(level, tag, logStr)
242 #define OIC_LOG_V(level, tag, ...)
243 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)
244 #define OIC_LOG_INIT()
245 #endif
246
247 #ifdef __cplusplus
248 }
249 #endif // __cplusplus
250
251 #endif /* LOGGER_H_ */