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"};
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[] = {level0, level1, level2, level3, level4};
90 static void OCLogString(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr);
91 #ifdef ARDUINO_ARCH_AVR
92 //Mega2560 and other 8-bit AVR microcontrollers
93 #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_word(addr));}
94 #elif defined ARDUINO_ARCH_SAM
95 //Arduino Due and other 32-bit ARM micro-controllers
96 #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_dword(addr));}
98 #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';}
100 #endif // __ANDROID__
105 void OCLogConfig(oc_log_ctx_t *ctx)
117 #if defined(__linux__) || defined(__APPLE__)
118 if (logCtx && logCtx->destroy)
120 logCtx->destroy(logCtx);
126 * Output a variable argument list 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 format - variadic log string
133 void OCLogv(LogLevel level, const char * tag, const char * format, ...)
135 if (!format || !tag) {
138 char buffer[MAX_LOG_V_BUFFER_SIZE] = {};
140 va_start(args, format);
141 vsnprintf(buffer, sizeof buffer - 1, format, args);
143 OCLog(level, tag, buffer);
147 * Output a log string with the specified priority level.
148 * Only defined for Linux and Android
150 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
151 * @param tag - Module name
152 * @param logStr - log string
154 void OCLog(LogLevel level, const char * tag, const char * logStr)
164 printf("%s: %s: %s\n", LEVEL[level], tag, logStr);
166 __android_log_write(LEVEL[level], tag, logStr);
169 #elif defined __linux__ || defined __APPLE__
170 if (logCtx && logCtx->write_level)
172 logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
180 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
181 struct timespec when = { .tv_sec = 0, .tv_nsec = 0 };
182 clockid_t clk = CLOCK_REALTIME;
183 #ifdef CLOCK_REALTIME_COARSE
184 clk = CLOCK_REALTIME_COARSE;
186 if (!clock_gettime(clk, &when))
188 min = (when.tv_sec / 60) % 60;
189 sec = when.tv_sec % 60;
190 ms = when.tv_nsec / 1000000;
194 if (!gettimeofday(&now, NULL))
196 min = (now.tv_sec / 60) % 60;
197 sec = now.tv_sec % 60;
198 ms = now.tv_usec * 1000;
201 printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
207 * Output the contents of the specified buffer (in hex) with the specified priority level.
209 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
210 * @param tag - Module name
211 * @param buffer - pointer to buffer of bytes
212 * @param bufferSize - max number of byte in buffer
214 void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize)
216 if (!buffer || !tag || (bufferSize == 0))
221 // No idea why the static initialization won't work here, it seems the compiler is convinced
222 // that this is a variable-sized object.
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 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
232 // Output 16 values per line
235 OCLog(level, tag, lineBuffer);
236 memset(lineBuffer, 0, sizeof lineBuffer);
240 // Output last values in the line, if any
243 OCLog(level, tag, lineBuffer);
250 * Initialize the serial logger for Arduino
251 * Only defined for Arduino
255 Serial.begin(115200);
259 * Output a log string with the specified priority level.
260 * Only defined for Arduino. Only uses PROGMEM strings
261 * for the tag parameter
263 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
264 * @param tag - Module name
265 * @param logStr - log string
267 void OCLogString(LogLevel level, PROGMEM const char * tag, const char * logStr)
274 char buffer[LINE_BUFFER_SIZE];
276 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
277 Serial.print(buffer);
280 Serial.print(F(": "));
281 while ((c = pgm_read_byte(tag)))
286 Serial.print(F(": "));
288 Serial.println(logStr);
292 * Output the contents of the specified buffer (in hex) with the specified
295 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
296 * @param tag - Module name
297 * @param buffer - pointer to buffer of bytes
298 * @param bufferSize - max number of byte in buffer
300 void OCLogBuffer(LogLevel level, PROGMEM const char * tag,
301 const uint8_t * buffer, size_t bufferSize)
303 if (!buffer || !tag || (bufferSize == 0))
308 char lineBuffer[LINE_BUFFER_SIZE] = {0};
309 uint8_t lineIndex = 0;
310 for (uint8_t i = 0; i < bufferSize; i++)
312 // Format the buffer data into a line
313 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
315 // Output 16 values per line
318 OCLogString(level, tag, lineBuffer);
319 memset(lineBuffer, 0, sizeof lineBuffer);
323 // Output last values in the line, if any
326 OCLogString(level, tag, lineBuffer);
331 * Output a log string with the specified priority level.
332 * Only defined for Arduino. Uses PROGMEM strings
334 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
335 * @param tag - Module name
336 * @param logStr - log string
338 void OCLog(LogLevel level, PROGMEM const char *tag, const int lineNum,
339 PROGMEM const char *logStr)
345 char buffer[LINE_BUFFER_SIZE] = {0};
346 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
347 Serial.print(buffer);
349 Serial.print(F(": "));
350 while ((c = pgm_read_byte(tag)))
355 Serial.print(F(": "));
356 Serial.print(lineNum);
357 Serial.print(F(": "));
358 while ((c = pgm_read_byte(logStr)))
367 * Output a variable argument list log string with the specified priority level.
368 * Only defined for Arduino as depicted below.
370 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
371 * @param tag - Module name
372 * @param format - variadic log string
374 void OCLogv(LogLevel level, PROGMEM const char *tag, const int lineNum,
375 PROGMEM const char *format, ...)
377 char buffer[LINE_BUFFER_SIZE];
379 va_start(ap, format);
380 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
381 Serial.print(buffer);
384 Serial.print(F(": "));
385 while ((c = pgm_read_byte(tag)))
390 Serial.print(F(": "));
391 Serial.print(lineNum);
392 Serial.print(F(": "));
395 vsnprintf_P(buffer, sizeof(buffer), format, ap);
397 vsnprintf(buffer, sizeof(buffer), format, ap);
399 for (char *p = &buffer[0]; *p; p++)
401 // emulate cooked mode for newlines
412 * Output a variable argument list log string with the specified priority level.
413 * Only defined for Arduino as depicted below.
415 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
416 * @param tag - Module name
417 * @param format - variadic log string
419 void OCLogv(LogLevel level, const char *tag, const __FlashStringHelper *format, ...)
421 char buffer[LINE_BUFFER_SIZE];
423 va_start(ap, format);
424 // strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level])));
425 // Serial.print(buffer);
427 Serial.print(LEVEL[level]);
429 Serial.print(F(": "));
431 /*while ((c = pgm_read_byte(tag))) {
436 Serial.print(F(": "));
439 vsnprintf_P(buffer, sizeof(buffer), (const char *)format, ap); // progmem for AVR
441 vsnprintf(buffer, sizeof(buffer), (const char *)format, ap); // for the rest of the world
443 for (char *p = &buffer[0]; *p; p++)
445 // emulate cooked mode for newlines
448 // Serial.write('\r');