iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / inc / 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 _U_LOGGER_H_
22 #define _U_LOGGER_H_
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include "oic_logger.h"
28 #include "oic_console_logger.h"
29
30 #ifdef __ANDROID__
31 #include <android/log.h>
32 #elif defined ARDUINO
33 #include "Arduino.h"
34 #include <avr/pgmspace.h>
35 #endif
36
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #endif
41
42 // Use the PCF macro to wrap strings stored in FLASH on the Arduino
43 // Example:  OC_LOG(INFO, TAG, PCF("Entering function"));
44 #ifdef ARDUINO
45 #define PCF(str)  ((PROGMEM const char *)(F(str)))
46 #else
47 #define PCF(str) str
48 #endif
49
50 // Max buffer size used in variable argument log function
51 #define MAX_LOG_V_BUFFER_SIZE (256)
52
53 // Log levels
54 typedef enum
55 {
56     DEBUG = 0, INFO, WARNING, ERROR, FATAL
57 } LogLevel;
58
59 #ifndef ARDUINO
60
61 /**
62  * Configure logger to use a context that defines a custom logger function
63  *
64  * @param ctx - pointer to oc_log_ctx_t struct that defines custom logging functions
65  */
66 void OICLogConfig(oic_log_ctx_t *ctx);
67
68 /**
69  * Initialize the logger.  Optional on Android and Linux.  Configures serial port on Arduino
70  */
71 void OICLogInit();
72
73 /**
74  * Called to Free dyamically allocated resources used with custom logging.
75  * Not necessary if default logging is used
76  *
77  */
78 void OICLogShutdown();
79
80 /**
81  * Output a log string with the specified priority level.
82  * Only defined for Linux and Android
83  *
84  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
85  * @param tag    - Module name
86  * @param logStr - log string
87  */
88 void OICLog(LogLevel level, const char * tag, const char * logStr);
89
90 /**
91  * Output a variable argument list log string with the specified priority level.
92  * Only defined for Linux and Android
93  *
94  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
95  * @param tag    - Module name
96  * @param format - variadic log string
97  */
98 void OICLogv(LogLevel level, const char * tag, const char * format, ...);
99
100 /**
101  * Output the contents of the specified buffer (in hex) with the specified priority level.
102  *
103  * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
104  * @param tag        - Module name
105  * @param buffer     - pointer to buffer of bytes
106  * @param bufferSize - max number of byte in buffer
107  */
108 void OICLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize);
109 #else
110 /**
111  * Initialize the serial logger for Arduino
112  * Only defined for Arduino
113  */
114 void OICLogInit();
115
116 /**
117  * Output a log string with the specified priority level.
118  * Only defined for Arduino.  Uses PROGMEM strings
119  *
120  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
121  * @param tag    - Module name
122  * @param logStr - log string
123  */
124 void OICLog(LogLevel level, PROGMEM const char * tag, PROGMEM 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 OICLogBuffer(LogLevel level, PROGMEM const char * tag, const uint8_t * buffer, uint16_t bufferSize);
135
136 /**
137  * Output a variable argument list log string with the specified priority level.
138  *
139  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
140  * @param tag    - Module name
141  * @param format - variadic log string
142  */
143 void OICLogv(LogLevel level, const char * tag, const char * format, ...);
144 #endif
145
146 #ifdef TB_LOG
147 // These macros are defined for Linux, Android, and Arduino
148 #define OIC_LOG_INIT()    OICLogInit()
149 #define OIC_LOG(level, tag, logStr)  OICLog((level), (tag), (logStr))
150 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)  OICLogBuffer((level), (tag), (buffer), (bufferSize))
151
152 #ifdef ARDUINO
153 #define OIC_LOG_CONFIG(ctx)
154 #define OIC_LOG_SHUTDOWN()
155 // Use full namespace for logInit to avoid function name collision
156 #define OIC_LOG_INIT()    OICLogInit()
157 // Don't define variable argument log function for Arduino
158 #define OIC_LOG_V(level, tag, ...) OICLogv((level), (tag), __VA_ARGS__)
159 #else
160 #define OIC_LOG_CONFIG(ctx)    OICLogConfig((ctx))
161 #define OIC_LOG_SHUTDOWN()     OICLogShutdown()
162 // Define variable argument log function for Linux and Android
163 #define OIC_LOG_V(level, tag, ...)  OICLogv((level), (tag), __VA_ARGS__)
164 #endif
165
166 #else
167 #define OIC_LOG_CONFIG(ctx)
168 #define OIC_LOG_SHUTDOWN()
169 #define OIC_LOG(level, tag, logStr)
170 #define OIC_LOG_V(level, tag, ...)
171 #define OIC_LOG_BUFFER(level, tag, buffer, bufferSize)
172 #define OIC_LOG_INIT()
173 #endif
174
175 #ifdef __cplusplus
176 }
177 #endif // __cplusplus
178 #endif /* _U_LOGGER_H_ */