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 #include "iotivity_config.h"
35 // Pull in _POSIX_TIMERS feature test macro to check for
36 // clock_gettime() support.
41 #ifdef HAVE_ARDUINO_TIME_H
47 #ifdef HAVE_SYS_TIME_H
56 #include "logger_types.h"
59 static oc_log_ctx_t *logCtx = 0;
63 #define LINE_BUFFER_SIZE (16 * 2) + 16 + 1 // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
65 static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
66 #endif //defined(_MSC_VER)
69 #elif defined __linux__ || defined __APPLE__ || defined _WIN32
70 static oc_log_level LEVEL_XTABLE[] = {OC_LOG_DEBUG, OC_LOG_INFO,
71 OC_LOG_WARNING, OC_LOG_ERROR, OC_LOG_FATAL};
74 // Convert LogLevel to platform-specific severity level. Store in PROGMEM on Arduino
77 static const char *LEVEL[] =
78 {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
81 static android_LogPriority LEVEL[] =
82 {ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
84 #elif defined(__linux__) || defined(__APPLE__) || defined(__msys_nt__)
85 static const char * LEVEL[] __attribute__ ((unused)) = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
86 #elif defined(_MSC_VER)
87 static const char * LEVEL[] = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
91 #include "oic_string.h"
93 PROGMEM const char level0[] = "DEBUG";
94 PROGMEM const char level1[] = "INFO";
95 PROGMEM const char level2[] = "WARNING";
96 PROGMEM const char level3[] = "ERROR";
97 PROGMEM const char level4[] = "FATAL";
99 PROGMEM const char * const LEVEL[] = {level0, level1, level2, level3, level4};
101 static void OCLogString(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr);
102 #ifdef ARDUINO_ARCH_AVR
103 //Mega2560 and other 8-bit AVR microcontrollers
104 #define GET_PROGMEM_BUFFER(buffer, addr) { OICStrcpy(buffer, sizeof(buffer), (char*)pgm_read_word(addr));}
105 #elif defined ARDUINO_ARCH_SAM
106 //Arduino Due and other 32-bit ARM micro-controllers
107 #define GET_PROGMEM_BUFFER(buffer, addr) { OICStrcpy(buffer, sizeof(buffer), (char*)pgm_read_dword(addr));}
109 #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';}
111 #else // !defined(__ANDROID__) && !defined(ARDUINO)
112 static const char *LEVEL[] __attribute__ ((unused)) =
113 {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
119 * Output the contents of the specified buffer (in hex) with the specified priority level.
121 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
122 * @param tag - Module name
123 * @param buffer - pointer to buffer of bytes
124 * @param bufferSize - max number of byte in buffer
126 void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize)
128 if (!buffer || !tag || (bufferSize == 0))
133 // No idea why the static initialization won't work here, it seems the compiler is convinced
134 // that this is a variable-sized object.
135 char lineBuffer[LINE_BUFFER_SIZE];
136 memset(lineBuffer, 0, sizeof lineBuffer);
139 for (i = 0; i < bufferSize; i++)
141 // Format the buffer data into a line
142 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
144 // Output 16 values per line
147 OCLogv(level, tag, "%s", lineBuffer);
148 memset(lineBuffer, 0, sizeof lineBuffer);
152 // Output last values in the line, if any
155 OCLogv(level, tag, "%s", lineBuffer);
159 void OCLogConfig(oc_log_ctx_t *ctx)
171 #if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
172 if (logCtx && logCtx->destroy)
174 logCtx->destroy(logCtx);
180 * Output a variable argument list log string with the specified priority level.
181 * Only defined for Linux and Android
183 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
184 * @param tag - Module name
185 * @param format - variadic log string
187 void OCLogv(LogLevel level, const char * tag, const char * format, ...)
189 if (!format || !tag) {
192 char buffer[MAX_LOG_V_BUFFER_SIZE] = {0};
194 va_start(args, format);
195 vsnprintf(buffer, sizeof buffer - 1, format, args);
197 OCLog(level, tag, buffer);
201 * Output a log string with the specified priority level.
202 * Only defined for Linux and Android
204 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
205 * @param tag - Module name
206 * @param logStr - log string
208 void OCLog(LogLevel level, const char * tag, const char * logStr)
218 printf("%s: %s: %s\n", LEVEL[level], tag, logStr);
220 __android_log_write(LEVEL[level], tag, logStr);
224 if (logCtx && logCtx->write_level)
226 logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
234 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
235 struct timespec when = { .tv_sec = 0, .tv_nsec = 0 };
236 clockid_t clk = CLOCK_REALTIME;
237 #ifdef CLOCK_REALTIME_COARSE
238 clk = CLOCK_REALTIME_COARSE;
240 if (!clock_gettime(clk, &when))
242 min = (when.tv_sec / 60) % 60;
243 sec = when.tv_sec % 60;
244 ms = when.tv_nsec / 1000000;
246 #elif defined(_WIN32)
247 SYSTEMTIME systemTime = {0};
248 GetLocalTime(&systemTime);
249 min = (int)systemTime.wMinute;
250 sec = (int)systemTime.wSecond;
251 ms = (int)systemTime.wMilliseconds;
254 if (!gettimeofday(&now, NULL))
256 min = (now.tv_sec / 60) % 60;
257 sec = now.tv_sec % 60;
258 ms = now.tv_usec * 1000;
261 printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
269 * Initialize the serial logger for Arduino
270 * Only defined for Arduino
274 Serial.begin(115200);
278 * Output a log string with the specified priority level.
279 * Only defined for Arduino. Only uses PROGMEM strings
280 * for the tag parameter
282 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
283 * @param tag - Module name
284 * @param logStr - log string
286 void OCLogString(LogLevel level, PROGMEM const char * tag, const char * logStr)
293 char buffer[LINE_BUFFER_SIZE];
295 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
296 Serial.print(buffer);
299 Serial.print(F(": "));
300 while ((c = pgm_read_byte(tag)))
305 Serial.print(F(": "));
307 Serial.println(logStr);
311 * Output the contents of the specified buffer (in hex) with the specified
314 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
315 * @param tag - Module name
316 * @param buffer - pointer to buffer of bytes
317 * @param bufferSize - max number of byte in buffer
319 void OCLogBuffer(LogLevel level, PROGMEM const char * tag,
320 const uint8_t * buffer, size_t bufferSize)
322 if (!buffer || !tag || (bufferSize == 0))
327 char lineBuffer[LINE_BUFFER_SIZE] = {0};
328 uint8_t lineIndex = 0;
329 for (uint8_t i = 0; i < bufferSize; i++)
331 // Format the buffer data into a line
332 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
334 // Output 16 values per line
337 OCLogString(level, tag, lineBuffer);
338 memset(lineBuffer, 0, sizeof lineBuffer);
342 // Output last values in the line, if any
345 OCLogString(level, tag, lineBuffer);
350 * Output a log string with the specified priority level.
351 * Only defined for Arduino. Uses PROGMEM strings
353 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
354 * @param tag - Module name
355 * @param logStr - log string
357 void OCLog(LogLevel level, PROGMEM const char *tag, const int lineNum,
358 PROGMEM const char *logStr)
364 char buffer[LINE_BUFFER_SIZE] = {0};
365 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
366 Serial.print(buffer);
368 Serial.print(F(": "));
369 while ((c = pgm_read_byte(tag)))
374 Serial.print(F(": "));
375 Serial.print(lineNum);
376 Serial.print(F(": "));
377 while ((c = pgm_read_byte(logStr)))
386 * Output a variable argument list log string with the specified priority level.
387 * Only defined for Arduino as depicted below.
389 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
390 * @param tag - Module name
391 * @param format - variadic log string
393 void OCLogv(LogLevel level, PROGMEM const char *tag, const int lineNum,
394 PROGMEM const char *format, ...)
396 char buffer[LINE_BUFFER_SIZE];
398 va_start(ap, format);
399 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
400 Serial.print(buffer);
403 Serial.print(F(": "));
404 while ((c = pgm_read_byte(tag)))
409 Serial.print(F(": "));
410 Serial.print(lineNum);
411 Serial.print(F(": "));
414 vsnprintf_P(buffer, sizeof(buffer), format, ap);
416 vsnprintf(buffer, sizeof(buffer), format, ap);
418 for (char *p = &buffer[0]; *p; p++)
420 // emulate cooked mode for newlines
431 * Output a variable argument list log string with the specified priority level.
432 * Only defined for Arduino as depicted below.
434 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
435 * @param tag - Module name
436 * @param format - variadic log string
438 void OCLogv(LogLevel level, const char *tag, const __FlashStringHelper *format, ...)
440 char buffer[LINE_BUFFER_SIZE];
442 va_start(ap, format);
443 // strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level])));
444 // Serial.print(buffer);
446 Serial.print(LEVEL[level]);
448 Serial.print(F(": "));
450 /*while ((c = pgm_read_byte(tag))) {
455 Serial.print(F(": "));
458 vsnprintf_P(buffer, sizeof(buffer), (const char *)format, ap); // progmem for AVR
460 vsnprintf(buffer, sizeof(buffer), (const char *)format, ap); // for the rest of the world
462 for (char *p = &buffer[0]; *p; p++)
464 // emulate cooked mode for newlines
467 // Serial.write('\r');