1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1b, Real-time extensions
24 // (IEEE Std 1003.1b-1993) specification
26 // For this specific file, see use of clock_gettime,
27 // Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
28 // and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
29 #ifndef _POSIX_C_SOURCE
30 #define _POSIX_C_SOURCE 200809L
33 // Platform check can be extended to check and/or define more, or could be
34 // moved into a config.h
35 #if !defined(__ARDUINO__) && !defined(ARDUINO)
36 #define HAVE_UNISTD_H 1
39 // Pull in _POSIX_TIMERS feature test macro to check for
40 // clock_gettime() support.
44 // if we have unistd.h, we're a Unix system
51 #include "oc_logger.h"
52 #include "oc_console_logger.h"
55 static oc_log_ctx_t *logCtx = 0;
57 static oc_log_level LEVEL_XTABLE[] = {OC_LOG_DEBUG, OC_LOG_INFO, OC_LOG_WARNING, OC_LOG_ERROR, OC_LOG_FATAL};
60 static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
62 // Convert LogLevel to platform-specific severity level. Store in PROGMEM on Arduino
64 static android_LogPriority LEVEL[] = {ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
65 #elif defined(__linux__) || defined(__APPLE__)
66 static const char * LEVEL[] __attribute__ ((unused)) = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
70 PROGMEM const char level0[] = "DEBUG";
71 PROGMEM const char level1[] = "INFO";
72 PROGMEM const char level2[] = "WARNING";
73 PROGMEM const char level3[] = "ERROR";
74 PROGMEM const char level4[] = "FATAL";
76 PROGMEM const char * const LEVEL[] = {level0, level1, level2, level3, level4};
78 static void OCLogString(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr);
79 #ifdef ARDUINO_ARCH_AVR
80 //Mega2560 and other 8-bit AVR microcontrollers
81 #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_word(addr));}
82 #elif defined ARDUINO_ARCH_SAM
83 //Arduino Due and other 32-bit ARM micro-controllers
84 #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_dword(addr));}
86 #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';}
93 void OCLogConfig(oc_log_ctx_t *ctx) {
101 void OCLogShutdown() {
102 #if defined(__linux__) || defined(__APPLE__)
103 if (logCtx && logCtx->destroy)
105 logCtx->destroy(logCtx);
111 * Output a variable argument list log string with the specified priority level.
112 * Only defined for Linux and Android
114 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
115 * @param tag - Module name
116 * @param format - variadic log string
118 void OCLogv(LogLevel level, const char * tag, const char * format, ...) {
119 if (!format || !tag) {
122 char buffer[MAX_LOG_V_BUFFER_SIZE] = {};
124 va_start(args, format);
125 vsnprintf(buffer, sizeof buffer - 1, format, args);
127 OCLog(level, tag, buffer);
130 static void osalGetTime(int *min,int *sec, int *ms)
132 if (min && sec && ms)
134 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
135 struct timespec when = {0};
136 clockid_t clk = CLOCK_REALTIME;
137 #ifdef CLOCK_REALTIME_COARSE
138 clk = CLOCK_REALTIME_COARSE;
140 if (!clock_gettime(clk, &when))
142 *min = (when.tv_sec / 60) % 60;
143 *sec = when.tv_sec % 60;
144 *ms = when.tv_nsec / 1000000;
148 if (!gettimeofday(&now, NULL))
150 *min = (now.tv_sec / 60) % 60;
151 *sec = now.tv_sec % 60;
152 *ms = now.tv_usec * 1000;
159 * Output a log string with the specified priority level.
160 * Only defined for Linux and Android
162 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
163 * @param tag - Module name
164 * @param logStr - log string
166 void OCLog(LogLevel level, const char * tag, const char * logStr) {
167 if (!logStr || !tag) {
172 __android_log_write(LEVEL[level], tag, logStr);
173 #elif defined(__linux__) || defined(__APPLE__)
174 if (logCtx && logCtx->write_level)
176 logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
183 osalGetTime(&min,&sec,&ms);
185 printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
191 * Output the contents of the specified buffer (in hex) with the specified priority level.
193 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
194 * @param tag - Module name
195 * @param buffer - pointer to buffer of bytes
196 * @param bufferSize - max number of byte in buffer
198 void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize) {
199 if (!buffer || !tag || (bufferSize == 0)) {
203 // No idea why the static initialization won't work here, it seems the compiler is convinced
204 // that this is a variable-sized object.
205 char lineBuffer[LINE_BUFFER_SIZE];
206 memset(lineBuffer, 0, sizeof lineBuffer);
209 for (i = 0; i < bufferSize; i++) {
210 // Format the buffer data into a line
211 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
213 // Output 16 values per line
214 if (((i+1)%16) == 0) {
215 OCLog(level, tag, lineBuffer);
216 memset(lineBuffer, 0, sizeof lineBuffer);
220 // Output last values in the line, if any
221 if (bufferSize % 16) {
222 OCLog(level, tag, lineBuffer);
228 * Initialize the serial logger for Arduino
229 * Only defined for Arduino
232 Serial.begin(115200);
236 * Output a log string with the specified priority level.
237 * Only defined for Arduino. Only uses PROGMEM strings
238 * for the tag parameter
240 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
241 * @param tag - Module name
242 * @param logStr - log string
244 void OCLogString(LogLevel level, PROGMEM const char * tag, const char * logStr) {
245 if (!logStr || !tag) {
249 char buffer[LINE_BUFFER_SIZE];
251 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
252 Serial.print(buffer);
255 Serial.print(F(": "));
256 while ((c = pgm_read_byte(tag))) {
260 Serial.print(F(": "));
262 Serial.println(logStr);
266 * Output the contents of the specified buffer (in hex) with the specified priority level.
268 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
269 * @param tag - Module name
270 * @param buffer - pointer to buffer of bytes
271 * @param bufferSize - max number of byte in buffer
273 void OCLogBuffer(LogLevel level, PROGMEM const char * tag, const uint8_t * buffer, uint16_t bufferSize) {
274 if (!buffer || !tag || (bufferSize == 0)) {
278 char lineBuffer[LINE_BUFFER_SIZE] = {0};
279 uint8_t lineIndex = 0;
280 for (uint8_t i = 0; i < bufferSize; i++) {
281 // Format the buffer data into a line
282 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
284 // Output 16 values per line
285 if (((i+1)%16) == 0) {
286 OCLogString(level, tag, lineBuffer);
287 memset(lineBuffer, 0, sizeof lineBuffer);
291 // Output last values in the line, if any
292 if (bufferSize % 16) {
293 OCLogString(level, tag, lineBuffer);
298 * Output a log string with the specified priority level.
299 * Only defined for Arduino. Uses PROGMEM strings
301 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
302 * @param tag - Module name
303 * @param logStr - log string
305 void OCLog(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr) {
306 if (!logStr || !tag) {
310 char buffer[LINE_BUFFER_SIZE];
312 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
313 Serial.print(buffer);
316 Serial.print(F(": "));
317 while ((c = pgm_read_byte(tag))) {
321 Serial.print(F(": "));
323 while ((c = pgm_read_byte(logStr))) {
331 * Output a variable argument list log string with the specified priority level.
332 * Only defined for Arduino as depicted below.
334 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
335 * @param tag - Module name
336 * @param format - variadic log string
338 void OCLogv(LogLevel level, PROGMEM const char * tag, PROGMEM const char * format, ...)
340 char buffer[LINE_BUFFER_SIZE];
342 va_start(ap, format);
344 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
345 Serial.print(buffer);
348 Serial.print(F(": "));
350 while ((c = pgm_read_byte(tag))) {
354 Serial.print(F(": "));
357 vsnprintf_P(buffer, sizeof(buffer), format, ap);
359 vsnprintf(buffer, sizeof(buffer), format, ap);
361 for(char *p = &buffer[0]; *p; p++) // emulate cooked mode for newlines