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