Merge branch 'master' into easysetup
[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 "oc_logger.h"
28 #include "oc_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 #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
47 #ifdef __cplusplus
48     #define PCF(str)  ((PROGMEM const char *)(F(str)))
49 #else
50     #define PCF(str)  ((PROGMEM const char *)(PSTR(str)))
51 #endif //__cplusplus
52 #else
53     #define PCF(str) str
54 #endif
55
56 // Max buffer size used in variable argument log function
57 #define MAX_LOG_V_BUFFER_SIZE (256)
58
59 // Log levels
60 typedef enum {
61     DEBUG = 0,
62     INFO,
63     WARNING,
64     ERROR,
65     FATAL
66 } LogLevel;
67
68 #ifdef __TIZEN__
69
70 int OCGetTizenLogLevel(LogLevel level);
71
72 #endif
73
74 #ifdef __TIZEN__
75 #define OCLog(level,tag,mes)
76 #define OCLogv(level,tag,fmt,args...)
77 #elif defined(ANDROID) || defined(__linux__) || defined(__APPLE__)
78     /**
79      * Configure logger to use a context that defines a custom logger function
80      *
81      * @param ctx - pointer to oc_log_ctx_t struct that defines custom logging functions
82      */
83     void OCLogConfig(oc_log_ctx_t *ctx);
84
85     /**
86      * Initialize the logger.  Optional on Android and Linux.  Configures serial port on Arduino
87      */
88     void OCLogInit();
89
90     /**
91      * Called to Free dyamically allocated resources used with custom logging.
92      * Not necessary if default logging is used
93      *
94      */
95     void OCLogShutdown();
96
97     /**
98      * Output a variable argument list log string with the specified priority level.
99      * Only defined for Linux and Android
100      *
101      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
102      * @param tag    - Module name
103      * @param format - variadic log string
104      */
105     void OCLogv(LogLevel level, const char * tag, const char * format, ...);
106
107     /**
108      * Output a log string with the specified priority level.
109      * Only defined for Linux and Android
110      *
111      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
112      * @param tag    - Module name
113      * @param logStr - log string
114      */
115     void OCLog(LogLevel level, const char * tag, const char * logStr);
116
117     /**
118      * Output the contents of the specified buffer (in hex) with the specified priority level.
119      *
120      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
121      * @param tag        - Module name
122      * @param buffer     - pointer to buffer of bytes
123      * @param bufferSize - max number of byte in buffer
124      */
125     void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize);
126 #else
127     /**
128      * Initialize the serial logger for Arduino
129      * Only defined for Arduino
130      */
131     void OCLogInit();
132
133     /**
134      * Output a log string with the specified priority level.
135      * Only defined for Arduino.  Uses PROGMEM strings
136      *
137      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
138      * @param tag    - Module name
139      * @param logStr - log string
140      */
141     void OCLog(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr);
142
143     /**
144      * Output the contents of the specified buffer (in hex) with the specified priority level.
145      *
146      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
147      * @param tag        - Module name
148      * @param buffer     - pointer to buffer of bytes
149      * @param bufferSize - max number of byte in buffer
150      */
151     void OCLogBuffer(LogLevel level, PROGMEM const char * tag, const uint8_t * buffer, uint16_t bufferSize);
152
153     /**
154      * Output a variable argument list log string with the specified priority level.
155      *
156      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
157      * @param tag    - Module name
158      * @param format - variadic log string
159      */
160     void OCLogv(LogLevel level, const char * tag, const char * format, ...);
161 #endif
162
163 #ifdef TB_LOG
164 #ifdef __TIZEN__
165     #define OC_LOG(level,tag,mes) LOG_(LOG_ID_MAIN, OCGetTizenLogLevel(level), tag, mes)
166     #define OC_LOG_V(level,tag,fmt,args...) LOG_(LOG_ID_MAIN, OCGetTizenLogLevel(level), tag, fmt,##args)
167     #define OC_LOG_BUFFER(level, tag, buffer, bufferSize)
168 #else // These macros are defined for Linux, Android, and Arduino
169     #define OC_LOG_INIT()    OCLogInit()
170     #define OC_LOG_BUFFER(level, tag, buffer, bufferSize)  OCLogBuffer((level), PCF(tag), (buffer), (bufferSize))
171
172     #ifdef ARDUINO
173         #define OC_LOG_CONFIG(ctx)
174         #define OC_LOG_SHUTDOWN()
175         #define OC_LOG(level, tag, logStr)  OCLog((level), PCF(tag), PCF(logStr))
176         // Use full namespace for logInit to avoid function name collision
177         #define OC_LOG_INIT()    OCLogInit()
178         // Don't define variable argument log function for Arduino
179         #define OC_LOG_V(level, tag, format, ...) OCLogv((level), PCF(tag), PCF(format), __VA_ARGS__)
180     #else
181         #define OC_LOG_CONFIG(ctx)    OCLogConfig((ctx))
182         #define OC_LOG(level, tag, logStr)  OCLog((level), (tag), (logStr))
183         #define OC_LOG_SHUTDOWN()     OCLogShutdown()
184         // Define variable argument log function for Linux and Android
185         #define OC_LOG_V(level, tag, ...)  OCLogv((level), (tag), __VA_ARGS__)
186     #endif
187 #endif
188 #else
189     #define OC_LOG_CONFIG(ctx)
190     #define OC_LOG_SHUTDOWN()
191     #define OC_LOG(level, tag, logStr)
192     #define OC_LOG_V(level, tag, ...)
193     #define OC_LOG_BUFFER(level, tag, buffer, bufferSize)
194     #define OC_LOG_INIT()
195 #endif
196
197 #ifdef __cplusplus
198 }
199 #endif // __cplusplus
200
201 #endif /* LOGGER_H_ */