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