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 // Pull in _POSIX_TIMERS feature test macro to check for
34 // clock_gettime() support.
39 #ifdef HAVE_ARDUINO_TIME_H
45 #ifdef HAVE_SYS_TIME_H
54 #include "logger_types.h"
55 #include "platform_features.h"
58 static oc_log_ctx_t *logCtx = 0;
62 #define LINE_BUFFER_SIZE (16 * 2) + 16 + 1 // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
64 static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
65 #endif //defined(_MSC_VER)
68 #elif defined __linux__ || defined __APPLE__ || defined _WIN32
69 static oc_log_level LEVEL_XTABLE[] = {OC_LOG_DEBUG, OC_LOG_INFO,
70 OC_LOG_WARNING, OC_LOG_ERROR, OC_LOG_FATAL};
73 // Convert LogLevel to platform-specific severity level. Store in PROGMEM on Arduino
76 static const char *LEVEL[] =
77 {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
80 static android_LogPriority LEVEL[] =
81 {ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
83 #elif defined(__linux__) || defined(__APPLE__) || defined(__msys_nt__)
84 static const char * LEVEL[] __attribute__ ((unused)) = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
85 #elif defined(_MSC_VER)
86 static const char * LEVEL[] = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
90 #include "oic_string.h"
92 PROGMEM const char level0[] = "DEBUG";
93 PROGMEM const char level1[] = "INFO";
94 PROGMEM const char level2[] = "WARNING";
95 PROGMEM const char level3[] = "ERROR";
96 PROGMEM const char level4[] = "FATAL";
98 PROGMEM const char * const LEVEL[] = {level0, level1, level2, level3, level4};
100 static void OCLogString(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr);
101 #ifdef ARDUINO_ARCH_AVR
102 //Mega2560 and other 8-bit AVR microcontrollers
103 #define GET_PROGMEM_BUFFER(buffer, addr) { OICStrcpy(buffer, sizeof(buffer), (char*)pgm_read_word(addr));}
104 #elif defined ARDUINO_ARCH_SAM
105 //Arduino Due and other 32-bit ARM micro-controllers
106 #define GET_PROGMEM_BUFFER(buffer, addr) { OICStrcpy(buffer, sizeof(buffer), (char*)pgm_read_dword(addr));}
108 #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';}
110 #else // !defined(__ANDROID__) && !defined(ARDUINO)
111 static const char *LEVEL[] __attribute__ ((unused)) =
112 {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
118 * Output the contents of the specified buffer (in hex) with the specified priority level.
120 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
121 * @param tag - Module name
122 * @param buffer - pointer to buffer of bytes
123 * @param bufferSize - max number of byte in buffer
125 void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize)
127 if (!buffer || !tag || (bufferSize == 0))
132 // No idea why the static initialization won't work here, it seems the compiler is convinced
133 // that this is a variable-sized object.
134 char lineBuffer[LINE_BUFFER_SIZE];
135 memset(lineBuffer, 0, sizeof lineBuffer);
138 for (i = 0; i < bufferSize; i++)
140 // Format the buffer data into a line
141 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
143 // Output 16 values per line
146 OCLogv(level, tag, "%s", lineBuffer);
147 memset(lineBuffer, 0, sizeof lineBuffer);
151 // Output last values in the line, if any
154 OCLogv(level, tag, "%s", lineBuffer);
158 void OCLogConfig(oc_log_ctx_t *ctx)
170 #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
171 if (logCtx && logCtx->destroy)
173 logCtx->destroy(logCtx);
179 * Output a variable argument list log string with the specified priority level.
180 * Only defined for Linux and Android
182 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
183 * @param tag - Module name
184 * @param format - variadic log string
186 void OCLogv(LogLevel level, const char * tag, const char * format, ...)
188 if (!format || !tag) {
191 char buffer[MAX_LOG_V_BUFFER_SIZE] = {0};
193 va_start(args, format);
194 vsnprintf(buffer, sizeof buffer - 1, format, args);
196 OCLog(level, tag, buffer);
200 * Output a log string with the specified priority level.
201 * Only defined for Linux and Android
203 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
204 * @param tag - Module name
205 * @param logStr - log string
207 void OCLog(LogLevel level, const char * tag, const char * logStr)
217 printf("%s: %s: %s\n", LEVEL[level], tag, logStr);
219 __android_log_write(LEVEL[level], tag, logStr);
223 if (logCtx && logCtx->write_level)
225 logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
233 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
234 struct timespec when = { .tv_sec = 0, .tv_nsec = 0 };
235 clockid_t clk = CLOCK_REALTIME;
236 #ifdef CLOCK_REALTIME_COARSE
237 clk = CLOCK_REALTIME_COARSE;
239 if (!clock_gettime(clk, &when))
241 min = (when.tv_sec / 60) % 60;
242 sec = when.tv_sec % 60;
243 ms = when.tv_nsec / 1000000;
245 #elif defined(_WIN32)
246 SYSTEMTIME systemTime = {0};
247 GetLocalTime(&systemTime);
248 min = (int)systemTime.wMinute;
249 sec = (int)systemTime.wSecond;
250 ms = (int)systemTime.wMilliseconds;
253 if (!gettimeofday(&now, NULL))
255 min = (now.tv_sec / 60) % 60;
256 sec = now.tv_sec % 60;
257 ms = now.tv_usec * 1000;
260 printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
268 * Initialize the serial logger for Arduino
269 * Only defined for Arduino
273 Serial.begin(115200);
277 * Output a log string with the specified priority level.
278 * Only defined for Arduino. Only uses PROGMEM strings
279 * for the tag parameter
281 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
282 * @param tag - Module name
283 * @param logStr - log string
285 void OCLogString(LogLevel level, PROGMEM const char * tag, const char * logStr)
292 char buffer[LINE_BUFFER_SIZE];
294 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
295 Serial.print(buffer);
298 Serial.print(F(": "));
299 while ((c = pgm_read_byte(tag)))
304 Serial.print(F(": "));
306 Serial.println(logStr);
310 * Output the contents of the specified buffer (in hex) with the specified
313 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
314 * @param tag - Module name
315 * @param buffer - pointer to buffer of bytes
316 * @param bufferSize - max number of byte in buffer
318 void OCLogBuffer(LogLevel level, PROGMEM const char * tag,
319 const uint8_t * buffer, size_t bufferSize)
321 if (!buffer || !tag || (bufferSize == 0))
326 char lineBuffer[LINE_BUFFER_SIZE] = {0};
327 uint8_t lineIndex = 0;
328 for (uint8_t i = 0; i < bufferSize; i++)
330 // Format the buffer data into a line
331 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
333 // Output 16 values per line
336 OCLogString(level, tag, lineBuffer);
337 memset(lineBuffer, 0, sizeof lineBuffer);
341 // Output last values in the line, if any
344 OCLogString(level, tag, lineBuffer);
349 * Output a log string with the specified priority level.
350 * Only defined for Arduino. Uses PROGMEM strings
352 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
353 * @param tag - Module name
354 * @param logStr - log string
356 void OCLog(LogLevel level, PROGMEM const char *tag, const int lineNum,
357 PROGMEM const char *logStr)
363 char buffer[LINE_BUFFER_SIZE] = {0};
364 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
365 Serial.print(buffer);
367 Serial.print(F(": "));
368 while ((c = pgm_read_byte(tag)))
373 Serial.print(F(": "));
374 Serial.print(lineNum);
375 Serial.print(F(": "));
376 while ((c = pgm_read_byte(logStr)))
385 * Output a variable argument list log string with the specified priority level.
386 * Only defined for Arduino as depicted below.
388 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
389 * @param tag - Module name
390 * @param format - variadic log string
392 void OCLogv(LogLevel level, PROGMEM const char *tag, const int lineNum,
393 PROGMEM const char *format, ...)
395 char buffer[LINE_BUFFER_SIZE];
397 va_start(ap, format);
398 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
399 Serial.print(buffer);
402 Serial.print(F(": "));
403 while ((c = pgm_read_byte(tag)))
408 Serial.print(F(": "));
409 Serial.print(lineNum);
410 Serial.print(F(": "));
413 vsnprintf_P(buffer, sizeof(buffer), format, ap);
415 vsnprintf(buffer, sizeof(buffer), format, ap);
417 for (char *p = &buffer[0]; *p; p++)
419 // emulate cooked mode for newlines
430 * Output a variable argument list log string with the specified priority level.
431 * Only defined for Arduino as depicted below.
433 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
434 * @param tag - Module name
435 * @param format - variadic log string
437 void OCLogv(LogLevel level, const char *tag, const __FlashStringHelper *format, ...)
439 char buffer[LINE_BUFFER_SIZE];
441 va_start(ap, format);
442 // strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level])));
443 // Serial.print(buffer);
445 Serial.print(LEVEL[level]);
447 Serial.print(F(": "));
449 /*while ((c = pgm_read_byte(tag))) {
454 Serial.print(F(": "));
457 vsnprintf_P(buffer, sizeof(buffer), (const char *)format, ap); // progmem for AVR
459 vsnprintf(buffer, sizeof(buffer), (const char *)format, ap); // for the rest of the world
461 for (char *p = &buffer[0]; *p; p++)
463 // emulate cooked mode for newlines
466 // Serial.write('\r');