Merge "Merge branch 'master' into group-manager" into group-manager
[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.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 // 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 typedef enum {
71     DEBUG = 0,
72     INFO,
73     WARNING,
74     ERROR,
75     FATAL
76 } LogLevel;
77 #endif
78
79 #ifdef __TIZEN__
80 #define OCLog(level,tag,mes)
81 #define OCLogv(level,tag,fmt,args...)
82 #elif defined(ANDROID) || defined(__linux__) || defined(__APPLE__)
83     /**
84      * Configure logger to use a context that defines a custom logger function
85      *
86      * @param ctx - pointer to oc_log_ctx_t struct that defines custom logging functions
87      */
88     void OCLogConfig(oc_log_ctx_t *ctx);
89
90     /**
91      * Initialize the logger.  Optional on Android and Linux.  Configures serial port on Arduino
92      */
93     void OCLogInit();
94
95     /**
96      * Called to Free dyamically allocated resources used with custom logging.
97      * Not necessary if default logging is used
98      *
99      */
100     void OCLogShutdown();
101
102     /**
103      * Output a variable argument list log string with the specified priority level.
104      * Only defined for Linux and Android
105      *
106      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
107      * @param tag    - Module name
108      * @param format - variadic log string
109      */
110     void OCLogv(LogLevel level, const char * tag, const char * format, ...)
111 #if defined(__GNUC__)
112     __attribute__ ((format(printf, 3, 4)))
113 #endif
114     ;
115
116     /**
117      * Output a 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 logStr - log string
123      */
124     void OCLog(LogLevel level, const char * tag, const char * logStr);
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 OCLogBuffer(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 OCLogInit();
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 lineNum- line Number
149      * @param logStr - log string
150      */
151     void OCLog(LogLevel level, PROGMEM const char *tag, const int lineNum,
152                PROGMEM const char *logStr);
153
154     /**
155      * Output the contents of the specified buffer (in hex) with the specified priority level.
156      *
157      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
158      * @param tag        - Module name
159      * @param buffer     - pointer to buffer of bytes
160      * @param bufferSize - max number of byte in buffer
161      */
162     void OCLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, size_t bufferSize);
163
164     /**
165      * Output a variable argument list log string with the specified priority level.
166      *
167      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
168      * @param tag    - Module name
169      * @param lineNum- line Number
170      * @param format - variadic log string
171      */
172     void OCLogv(LogLevel level, PROGMEM const char *tag, const int lineNum,
173                 PROGMEM const char *format, ...)
174 #if defined(__GNUC__)
175     __attribute__ ((format(printf, 4, 5)))
176 #endif
177 ;
178 #endif
179
180 #ifdef TB_LOG
181
182 #ifdef __TIZEN__
183
184 #define OIC_LOG(level,tag,mes) LOG_(LOG_ID_MAIN, (level), (tag), mes)
185 #define OIC_LOG_V(level,tag,fmt,args...) LOG_(LOG_ID_MAIN, level, tag, fmt, ##args)
186 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)
187
188 #else // These macros are defined for Linux, Android, and Arduino
189
190 #define OIC_LOG_INIT()    OCLogInit()
191
192 #ifdef ARDUINO
193
194 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)  OCLogBuffer((level), PCF(tag), (buffer), (bufferSize))
195 // Don't define variable argument log function for Arduino
196 #define OIC_LOG_V(level, tag, format, ...) OCLogv((level), PCF(tag), __LINE__, PCF(format),__VA_ARGS__)
197
198 #define OIC_LOG_CONFIG(ctx)
199 #define OIC_LOG_SHUTDOWN()
200 #define OIC_LOG(level, tag, logStr) OCLog((level), PCF(tag), __LINE__, PCF(logStr))
201 #define OIC_LOG_V(level, tag, ...)
202
203 #else
204
205 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)  OCLogBuffer((level), (tag), (buffer), (bufferSize))
206 #define OIC_LOG_CONFIG(ctx)    OCLogConfig((ctx))
207 #define OIC_LOG_SHUTDOWN()     OCLogShutdown()
208 #define OIC_LOG(level, tag, logStr)  OCLog((level), (tag), (logStr))
209 // Define variable argument log function for Linux and Android
210 #define OIC_LOG_V(level, tag, ...)  OCLogv((level), (tag), __VA_ARGS__)
211
212 #endif //ARDUINO
213 #endif //__TIZEN__
214
215 #else //TB_LOG
216
217 #define OIC_LOG_CONFIG(ctx)
218 #define OIC_LOG_SHUTDOWN()
219 #define OIC_LOG(level, tag, logStr)
220 #define OIC_LOG_V(level, tag, ...)
221 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)
222 #define OIC_LOG_INIT()
223 #endif
224
225 #ifdef __cplusplus
226 }
227 #endif // __cplusplus
228
229 #endif /* LOGGER_H_ */