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