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 "oic_logger.h"
52 #include "oic_console_logger.h"
55 static oic_log_ctx_t *logCtx = 0;
57 static oic_log_level LEVEL_XTABLE[] =
58 { OIC_LOG_DEBUG, OIC_LOG_INFO, OIC_LOG_WARNING, OIC_LOG_ERROR, OIC_LOG_FATAL };
62 static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 +
63 1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
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__
76 static const char *LEVEL[] __attribute__ ((unused)) =
77 { "DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
82 PROGMEM const char level0[] = "DEBUG";
83 PROGMEM const char level1[] = "INFO";
84 PROGMEM const char level2[] = "WARNING";
85 PROGMEM const char level3[] = "ERROR";
86 PROGMEM const char level4[] = "FATAL";
88 PROGMEM const char *const LEVEL[] =
89 { level0, level1, level2, level3, level4};
91 static void OICLogString(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) { strcpy_P(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) { strcpy_P(buffer, (char*)pgm_read_dword(addr));}
99 #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';}
101 #endif // __ANDROID__
105 void OICLogConfig(oic_log_ctx_t *ctx)
115 void OICLogShutdown()
118 if (logCtx && logCtx->destroy)
120 logCtx->destroy(logCtx);
126 * Output a log string with the specified priority level.
127 * Only defined for Linux and Android
129 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
130 * @param tag - Module name
131 * @param logStr - log string
133 void OICLog(LogLevel level, const char *tag, const char *logStr)
143 printf("%s: %s: %s\n", LEVEL[level], tag, logStr);
145 __android_log_write(LEVEL[level], tag, logStr);
148 #elif defined __linux__
149 if (logCtx && logCtx->write_level)
151 logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
160 struct timespec when = {};
161 clockid_t clk = CLOCK_REALTIME;
162 #ifdef CLOCK_REALTIME_COARSE
163 clk = CLOCK_REALTIME_COARSE;
165 if (!clock_gettime(clk, &when))
167 min = (when.tv_sec / 60) % 60;
168 sec = when.tv_sec % 60;
169 ms = when.tv_nsec / 1000000;
173 if (!gettimeofday(&now, NULL))
175 min = (now.tv_sec / 60) % 60;
176 sec = now.tv_sec % 60;
177 ms = now.tv_usec * 1000;
180 printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
186 * Output a variable argument list log string with the specified priority level.
187 * Only defined for Linux and Android
189 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
190 * @param tag - Module name
191 * @param format - variadic log string
193 void OICLogv(LogLevel level, const char *tag, const char *format, ...)
199 char buffer[MAX_LOG_V_BUFFER_SIZE];
200 memset(buffer, 0, sizeof buffer);
202 va_start(args, format);
203 vsnprintf(buffer, sizeof buffer - 1, format, args);
205 OICLog(level, tag, buffer);
209 * Output the contents of the specified buffer (in hex) with the specified priority level.
211 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
212 * @param tag - Module name
213 * @param buffer - pointer to buffer of bytes
214 * @param bufferSize - max number of byte in buffer
216 void OICLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, uint16_t bufferSize)
218 if (!buffer || !tag || (bufferSize == 0))
223 char lineBuffer[LINE_BUFFER_SIZE];
224 memset(lineBuffer, 0, sizeof lineBuffer);
227 for (i = 0; i < bufferSize; i++)
229 // Format the buffer data into a line
230 sprintf(&lineBuffer[lineIndex++ * 3], "%02X ", buffer[i]);
231 // Output 16 values per line
232 if (((i + 1) % 16) == 0)
234 OICLog(level, tag, lineBuffer);
235 memset(lineBuffer, 0, sizeof lineBuffer);
239 // Output last values in the line, if any
242 OICLog(level, tag, lineBuffer);
249 * Initialize the serial logger for Arduino
250 * Only defined for Arduino
254 Serial.begin(115200);
258 * Output a log string with the specified priority level.
259 * Only defined for Arduino. Only uses PROGMEM strings
260 * for the tag parameter
262 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
263 * @param tag - Module name
264 * @param logStr - log string
266 void OICLogString(LogLevel level, PROGMEM const char * tag,
274 char buffer[LINE_BUFFER_SIZE] = {0};
275 strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level])));
276 Serial.print(buffer);
279 Serial.print(F(": "));
280 while ((c = pgm_read_byte(tag)))
285 Serial.print(F(": "));
287 Serial.println(logStr);
291 * Output the contents of the specified buffer (in hex) with the specified
294 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
295 * @param tag - Module name
296 * @param buffer - pointer to buffer of bytes
297 * @param bufferSize - max number of byte in buffer
299 void OICLogBuffer(LogLevel level, PROGMEM const char * tag,
300 const uint8_t * buffer, uint16_t bufferSize)
302 if (!buffer || !tag || (bufferSize == 0))
307 char lineBuffer[LINE_BUFFER_SIZE] = {0};
308 uint8_t lineIndex = 0;
309 for (uint8_t i = 0; i < bufferSize; i++)
311 // Format the buffer data into a line
312 sprintf(&lineBuffer[lineIndex++ * 3], "%02X ", buffer[i]);
313 // Output 16 values per line
316 OICLogString(level, tag, lineBuffer);
317 memset(lineBuffer, 0, sizeof lineBuffer);
321 // Output last values in the line, if any
324 OICLogString(level, tag, lineBuffer);
329 * Output a log string with the specified priority level.
330 * Only defined for Arduino. Uses PROGMEM strings
332 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
333 * @param tag - Module name
334 * @param logStr - log string
336 void OICLog(LogLevel level, PROGMEM const char *tag, const int16_t lineNum,
337 PROGMEM const char *logStr)
343 char buffer[LINE_BUFFER_SIZE] = {0};
344 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
345 Serial.print(buffer);
347 Serial.print(F(": "));
348 while ((c = pgm_read_byte(tag)))
353 Serial.print(F(": "));
354 Serial.print(lineNum);
355 Serial.print(F(": "));
356 while ((c = pgm_read_byte(logStr)))
365 * Output a variable argument list log string with the specified priority level.
366 * Only defined for Arduino as depicted below.
368 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
369 * @param tag - Module name
370 * @param format - variadic log string
372 void OICLogv(LogLevel level, PROGMEM const char *tag, const int16_t lineNum,
373 PROGMEM const char *format, ...)
375 char buffer[LINE_BUFFER_SIZE];
377 va_start(ap, format);
378 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
379 Serial.print(buffer);
382 Serial.print(F(": "));
383 while ((c = pgm_read_byte(tag))) {
387 Serial.print(F(": "));
388 Serial.print(lineNum);
389 Serial.print(F(": "));
392 vsnprintf_P(buffer, sizeof(buffer), format, ap);
394 vsnprintf(buffer, sizeof(buffer), format, ap);
396 for (char *p = &buffer[0]; *p; p++)
398 // emulate cooked mode for newlines
409 * Output a variable argument list log string with the specified priority level.
410 * Only defined for Arduino as depicted below.
412 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
413 * @param tag - Module name
414 * @param format - variadic log string
416 void OICLogv(LogLevel level, const char *tag, const __FlashStringHelper *format, ...)
418 char buffer[LINE_BUFFER_SIZE];
420 va_start(ap, format);
421 // strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level])));
422 // Serial.print(buffer);
424 Serial.print(LEVEL[level]);
426 Serial.print(F(": "));
428 /*while ((c = pgm_read_byte(tag))) {
433 Serial.print(F(": "));
436 vsnprintf_P(buffer, sizeof(buffer), (const char *)format, ap); // progmem for AVR
438 vsnprintf(buffer, sizeof(buffer), (const char *)format, ap); // for the rest of the world
440 for (char *p = &buffer[0]; *p; p++)
442 // emulate cooked mode for newlines
445 // Serial.write('\r');