07b6ac12e093e07bee5c4693f5489fd80e143954
[platform/upstream/iotivity.git] / csdk / logger / include / logger.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation 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
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #ifdef __ANDROID__
33     #include <android/log.h>
34 #elif defined ARDUINO
35     #include "Arduino.h"
36     #include <avr/pgmspace.h>
37 #endif
38
39 // Use the PCF macro to wrap strings stored in FLASH on the Arduino
40 // Example:  OC_LOG(INFO, TAG, PCF("Entering function"));
41 #ifdef ARDUINO
42     #define PCF(str)  ((const prog_char*)(F(str)))
43 #else
44     #define PCF(str) str
45 #endif
46
47 // Max buffer size used in variable argument log function
48 #define MAX_LOG_V_BUFFER_SIZE (256)
49
50 // Log levels
51 typedef enum {
52     DEBUG = 0,
53     INFO,
54     WARNING,
55     ERROR,
56     FATAL
57 } LogLevel;
58
59
60 #if defined(__ANDROID__) || defined(__linux__)
61     /**
62      * Output a variable argument list log string with the specified priority level.
63      * Only defined for Linux and Android
64      *
65      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
66      * @param tag    - Module name
67      * @param format - variadic log string
68      */
69     void OCLogv(LogLevel level, const char * tag, const char * format, ...);
70
71     /**
72      * Output a log string with the specified priority level.
73      * Only defined for Linux and Android
74      *
75      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
76      * @param tag    - Module name
77      * @param logStr - log string
78      */
79     void OCLog(LogLevel level, const char * tag, const char * logStr);
80
81     /**
82      * Output the contents of the specified buffer (in hex) with the specified priority level.
83      *
84      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
85      * @param tag        - Module name
86      * @param buffer     - pointer to buffer of bytes
87      * @param bufferSize - max number of byte in buffer
88      */
89     void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize);
90 #endif
91
92 #ifdef ARDUINO
93     /**
94      * Initialize the serial logger for Arduino
95      * Only defined for Arduino
96      */
97     void OCLogInit();
98
99     /**
100      * Output a log string with the specified priority level.
101      * Only defined for Arduino.  Uses PROGMEM strings
102      *
103      * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
104      * @param tag    - Module name
105      * @param logStr - log string
106      */
107     void OCLog(LogLevel level, const prog_char * tag, const prog_char * logStr);
108
109     /**
110      * Output the contents of the specified buffer (in hex) with the specified priority level.
111      *
112      * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
113      * @param tag        - Module name
114      * @param buffer     - pointer to buffer of bytes
115      * @param bufferSize - max number of byte in buffer
116      */
117     void OCLogBuffer(LogLevel level, const prog_char * tag, const uint8_t * buffer, uint16_t bufferSize);
118 #endif
119
120 #ifdef TB_LOG
121     // These macros are defined for Linux, Android, and Arduino
122     #define OC_LOG(level, tag, logStr)  OCLog((level), (tag), (logStr))
123     #define OC_LOG_BUFFER(level, tag, buffer, bufferSize)  OCLogBuffer((level), (tag), (buffer), (bufferSize))
124
125     #ifdef ARDUINO
126         // Use full namespace for logInit to avoid function name collision
127         #define OC_LOG_INIT()    OCLogInit()
128         // Don't define variable argument log function for Arduino
129         #define OC_LOG_V(level, tag, ...)
130     #else
131         // Don't define LOG_INIT for Linux and Android
132         #define OC_LOG_INIT()
133         // Define variable argument log function for Linux and Android
134         #define OC_LOG_V(level, tag, ...)  OCLogv((level), (tag), __VA_ARGS__)
135     #endif
136
137 #else
138     #define OC_LOG(level, tag, logStr)
139     #define OC_LOG_V(level, tag, ...)
140     #define OC_LOG_BUFFER(level, tag, buffer, bufferSize)
141     #define OC_LOG_INIT()
142 #endif
143
144 #ifdef __cplusplus
145 }
146 #endif // __cplusplus
147
148 #endif /* LOGGER_H_ */