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 "logger_types.h"
54 static oc_log_ctx_t *logCtx = 0;
57 #elif defined __linux__ || defined __APPLE__
58 static oc_log_level LEVEL_XTABLE[] = {OC_LOG_DEBUG, OC_LOG_INFO,
59 OC_LOG_WARNING, OC_LOG_ERROR, OC_LOG_FATAL};
62 // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
63 static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1;
65 // Convert LogLevel to platform-specific severity level. Store in PROGMEM on Arduino
68 static const char *LEVEL[] =
69 {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
72 static android_LogPriority LEVEL[] =
73 {ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
75 #elif defined (__linux__) || defined (__APPLE__)
76 static const char *LEVEL[] __attribute__ ((unused)) =
77 {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
81 #include "oic_string.h"
83 PROGMEM const char level0[] = "DEBUG";
84 PROGMEM const char level1[] = "INFO";
85 PROGMEM const char level2[] = "WARNING";
86 PROGMEM const char level3[] = "ERROR";
87 PROGMEM const char level4[] = "FATAL";
89 PROGMEM const char * const LEVEL[] = {level0, level1, level2, level3, level4};
91 static void OCLogString(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr);
92 #ifdef ARDUINO_ARCH_AVR
93 //Mega2560 and other 8-bit AVR microcontrollers
94 #define GET_PROGMEM_BUFFER(buffer, addr) { OICStrcpy(buffer, sizeof(buffer), (char*)pgm_read_word(addr));}
95 #elif defined ARDUINO_ARCH_SAM
96 //Arduino Due and other 32-bit ARM micro-controllers
97 #define GET_PROGMEM_BUFFER(buffer, addr) { OICStrcpy(buffer, sizeof(buffer), (char*)pgm_read_dword(addr));}
99 #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';}
101 #endif // __ANDROID__
106 void OCLogConfig(oc_log_ctx_t *ctx)
118 #if defined(__linux__) || defined(__APPLE__)
119 if (logCtx && logCtx->destroy)
121 logCtx->destroy(logCtx);
127 * Output a variable argument list log string with the specified priority level.
128 * Only defined for Linux and Android
130 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
131 * @param tag - Module name
132 * @param format - variadic log string
134 void OCLogv(LogLevel level, const char * tag, const char * format, ...)
136 if (!format || !tag) {
139 char buffer[MAX_LOG_V_BUFFER_SIZE] = {};
141 va_start(args, format);
142 vsnprintf(buffer, sizeof buffer - 1, format, args);
144 OCLog(level, tag, buffer);
148 * Output a log string with the specified priority level.
149 * Only defined for Linux and Android
151 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
152 * @param tag - Module name
153 * @param logStr - log string
155 void OCLog(LogLevel level, const char * tag, const char * logStr)
165 printf("%s: %s: %s\n", LEVEL[level], tag, logStr);
167 __android_log_write(LEVEL[level], tag, logStr);
170 #elif defined __linux__ || defined __APPLE__
171 if (logCtx && logCtx->write_level)
173 logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
181 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
182 struct timespec when = { .tv_sec = 0, .tv_nsec = 0 };
183 clockid_t clk = CLOCK_REALTIME;
184 #ifdef CLOCK_REALTIME_COARSE
185 clk = CLOCK_REALTIME_COARSE;
187 if (!clock_gettime(clk, &when))
189 min = (when.tv_sec / 60) % 60;
190 sec = when.tv_sec % 60;
191 ms = when.tv_nsec / 1000000;
195 if (!gettimeofday(&now, NULL))
197 min = (now.tv_sec / 60) % 60;
198 sec = now.tv_sec % 60;
199 ms = now.tv_usec * 1000;
202 printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
208 * Output the contents of the specified buffer (in hex) with the specified priority level.
210 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
211 * @param tag - Module name
212 * @param buffer - pointer to buffer of bytes
213 * @param bufferSize - max number of byte in buffer
215 void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize)
217 if (!buffer || !tag || (bufferSize == 0))
222 // No idea why the static initialization won't work here, it seems the compiler is convinced
223 // that this is a variable-sized object.
224 char lineBuffer[LINE_BUFFER_SIZE];
225 memset(lineBuffer, 0, sizeof lineBuffer);
228 for (i = 0; i < bufferSize; i++)
230 // Format the buffer data into a line
231 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
233 // Output 16 values per line
236 OCLog(level, tag, lineBuffer);
237 memset(lineBuffer, 0, sizeof lineBuffer);
241 // Output last values in the line, if any
244 OCLog(level, tag, lineBuffer);
251 * Initialize the serial logger for Arduino
252 * Only defined for Arduino
256 Serial.begin(115200);
260 * Output a log string with the specified priority level.
261 * Only defined for Arduino. Only uses PROGMEM strings
262 * for the tag parameter
264 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
265 * @param tag - Module name
266 * @param logStr - log string
268 void OCLogString(LogLevel level, PROGMEM const char * tag, const char * logStr)
275 char buffer[LINE_BUFFER_SIZE];
277 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
278 Serial.print(buffer);
281 Serial.print(F(": "));
282 while ((c = pgm_read_byte(tag)))
287 Serial.print(F(": "));
289 Serial.println(logStr);
293 * Output the contents of the specified buffer (in hex) with the specified
296 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
297 * @param tag - Module name
298 * @param buffer - pointer to buffer of bytes
299 * @param bufferSize - max number of byte in buffer
301 void OCLogBuffer(LogLevel level, PROGMEM const char * tag,
302 const uint8_t * buffer, size_t bufferSize)
304 if (!buffer || !tag || (bufferSize == 0))
309 char lineBuffer[LINE_BUFFER_SIZE] = {0};
310 uint8_t lineIndex = 0;
311 for (uint8_t i = 0; i < bufferSize; i++)
313 // Format the buffer data into a line
314 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
316 // Output 16 values per line
319 OCLogString(level, tag, lineBuffer);
320 memset(lineBuffer, 0, sizeof lineBuffer);
324 // Output last values in the line, if any
327 OCLogString(level, tag, lineBuffer);
332 * Output a log string with the specified priority level.
333 * Only defined for Arduino. Uses PROGMEM strings
335 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
336 * @param tag - Module name
337 * @param logStr - log string
339 void OCLog(LogLevel level, PROGMEM const char *tag, const int lineNum,
340 PROGMEM const char *logStr)
346 char buffer[LINE_BUFFER_SIZE] = {0};
347 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
348 Serial.print(buffer);
350 Serial.print(F(": "));
351 while ((c = pgm_read_byte(tag)))
356 Serial.print(F(": "));
357 Serial.print(lineNum);
358 Serial.print(F(": "));
359 while ((c = pgm_read_byte(logStr)))
368 * Output a variable argument list log string with the specified priority level.
369 * Only defined for Arduino as depicted below.
371 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
372 * @param tag - Module name
373 * @param format - variadic log string
375 void OCLogv(LogLevel level, PROGMEM const char *tag, const int lineNum,
376 PROGMEM const char *format, ...)
378 char buffer[LINE_BUFFER_SIZE];
380 va_start(ap, format);
381 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
382 Serial.print(buffer);
385 Serial.print(F(": "));
386 while ((c = pgm_read_byte(tag)))
391 Serial.print(F(": "));
392 Serial.print(lineNum);
393 Serial.print(F(": "));
396 vsnprintf_P(buffer, sizeof(buffer), format, ap);
398 vsnprintf(buffer, sizeof(buffer), format, ap);
400 for (char *p = &buffer[0]; *p; p++)
402 // emulate cooked mode for newlines
413 * Output a variable argument list log string with the specified priority level.
414 * Only defined for Arduino as depicted below.
416 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
417 * @param tag - Module name
418 * @param format - variadic log string
420 void OCLogv(LogLevel level, const char *tag, const __FlashStringHelper *format, ...)
422 char buffer[LINE_BUFFER_SIZE];
424 va_start(ap, format);
425 // strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level])));
426 // Serial.print(buffer);
428 Serial.print(LEVEL[level]);
430 Serial.print(F(": "));
432 /*while ((c = pgm_read_byte(tag))) {
437 Serial.print(F(": "));
440 vsnprintf_P(buffer, sizeof(buffer), (const char *)format, ap); // progmem for AVR
442 vsnprintf(buffer, sizeof(buffer), (const char *)format, ap); // for the rest of the world
444 for (char *p = &buffer[0]; *p; p++)
446 // emulate cooked mode for newlines
449 // Serial.write('\r');