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 "oc_logger.h"
52 #include "oc_console_logger.h"
54 static oc_log_ctx_t *logCtx = 0;
56 static oc_log_level LEVEL_XTABLE[] = {OC_LOG_DEBUG, OC_LOG_INFO, OC_LOG_WARNING, OC_LOG_ERROR, OC_LOG_FATAL};
58 static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
60 // Convert LogLevel to platform-specific severity level. Store in PROGMEM on Arduino
62 static android_LogPriority LEVEL[] = {ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
63 #elif defined(__linux__) || defined(__APPLE__)
64 static const char * LEVEL[] __attribute__ ((unused)) = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
68 PROGMEM const char level0[] = "DEBUG";
69 PROGMEM const char level1[] = "INFO";
70 PROGMEM const char level2[] = "WARNING";
71 PROGMEM const char level3[] = "ERROR";
72 PROGMEM const char level4[] = "FATAL";
74 PROGMEM const char * const LEVEL[] = {level0, level1, level2, level3, level4};
76 static void OCLogString(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr);
77 #ifdef ARDUINO_ARCH_AVR
78 //Mega2560 and other 8-bit AVR microcontrollers
79 #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_word(addr));}
80 #elif defined ARDUINO_ARCH_SAM
81 //Arduino Due and other 32-bit ARM micro-controllers
82 #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_dword(addr));}
84 #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';}
91 void OCLogConfig(oc_log_ctx_t *ctx) {
99 void OCLogShutdown() {
100 #if defined(__linux__) || defined(__APPLE__)
101 if (logCtx && logCtx->destroy)
103 logCtx->destroy(logCtx);
109 * Output a variable argument list log string with the specified priority level.
110 * Only defined for Linux and Android
112 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
113 * @param tag - Module name
114 * @param format - variadic log string
116 void OCLogv(LogLevel level, const char * tag, const char * format, ...) {
117 if (!format || !tag) {
120 char buffer[MAX_LOG_V_BUFFER_SIZE] = {};
122 va_start(args, format);
123 vsnprintf(buffer, sizeof buffer - 1, format, args);
125 OCLog(level, tag, buffer);
128 static void osalGetTime(int *min,int *sec, int *ms)
130 if (min && sec && ms)
132 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
133 struct timespec when = {};
134 clockid_t clk = CLOCK_REALTIME;
135 #ifdef CLOCK_REALTIME_COARSE
136 clk = CLOCK_REALTIME_COARSE;
138 if (!clock_gettime(clk, &when))
140 *min = (when.tv_sec / 60) % 60;
141 *sec = when.tv_sec % 60;
142 *ms = when.tv_nsec / 1000000;
146 if (!gettimeofday(&now, NULL))
148 *min = (now.tv_sec / 60) % 60;
149 *sec = now.tv_sec % 60;
150 *ms = now.tv_usec * 1000;
157 * Output a log string with the specified priority level.
158 * Only defined for Linux and Android
160 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
161 * @param tag - Module name
162 * @param logStr - log string
164 void OCLog(LogLevel level, const char * tag, const char * logStr) {
165 if (!logStr || !tag) {
170 __android_log_write(LEVEL[level], tag, logStr);
171 #elif defined(__linux__) || defined(__APPLE__)
172 if (logCtx && logCtx->write_level)
174 logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
182 osalGetTime(&min,&sec,&ms);
184 printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
190 * Output the contents of the specified buffer (in hex) with the specified priority level.
192 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
193 * @param tag - Module name
194 * @param buffer - pointer to buffer of bytes
195 * @param bufferSize - max number of byte in buffer
197 void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize) {
198 if (!buffer || !tag || (bufferSize == 0)) {
202 // No idea why the static initialization won't work here, it seems the compiler is convinced
203 // that this is a variable-sized object.
204 char lineBuffer[LINE_BUFFER_SIZE];
205 memset(lineBuffer, 0, sizeof lineBuffer);
208 for (i = 0; i < bufferSize; i++) {
209 // Format the buffer data into a line
210 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
212 // Output 16 values per line
213 if (((i+1)%16) == 0) {
214 OCLog(level, tag, lineBuffer);
215 memset(lineBuffer, 0, sizeof lineBuffer);
219 // Output last values in the line, if any
220 if (bufferSize % 16) {
221 OCLog(level, tag, lineBuffer);
227 * Initialize the serial logger for Arduino
228 * Only defined for Arduino
231 Serial.begin(115200);
235 * Output a log string with the specified priority level.
236 * Only defined for Arduino. Only uses PROGMEM strings
237 * for the tag parameter
239 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
240 * @param tag - Module name
241 * @param logStr - log string
243 void OCLogString(LogLevel level, PROGMEM const char * tag, const char * logStr) {
244 if (!logStr || !tag) {
248 char buffer[LINE_BUFFER_SIZE];
250 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
251 Serial.print(buffer);
254 Serial.print(F(": "));
255 while ((c = pgm_read_byte(tag))) {
259 Serial.print(F(": "));
261 Serial.println(logStr);
265 * Output the contents of the specified buffer (in hex) with the specified priority level.
267 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
268 * @param tag - Module name
269 * @param buffer - pointer to buffer of bytes
270 * @param bufferSize - max number of byte in buffer
272 void OCLogBuffer(LogLevel level, PROGMEM const char * tag, const uint8_t * buffer, uint16_t bufferSize) {
273 if (!buffer || !tag || (bufferSize == 0)) {
277 char lineBuffer[LINE_BUFFER_SIZE] = {0};
278 uint8_t lineIndex = 0;
279 for (uint8_t i = 0; i < bufferSize; i++) {
280 // Format the buffer data into a line
281 snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
283 // Output 16 values per line
284 if (((i+1)%16) == 0) {
285 OCLogString(level, tag, lineBuffer);
286 memset(lineBuffer, 0, sizeof lineBuffer);
290 // Output last values in the line, if any
291 if (bufferSize % 16) {
292 OCLogString(level, tag, lineBuffer);
297 * Output a log string with the specified priority level.
298 * Only defined for Arduino. Uses PROGMEM strings
300 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
301 * @param tag - Module name
302 * @param logStr - log string
304 void OCLog(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr) {
305 if (!logStr || !tag) {
309 char buffer[LINE_BUFFER_SIZE];
311 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
312 Serial.print(buffer);
315 Serial.print(F(": "));
316 while ((c = pgm_read_byte(tag))) {
320 Serial.print(F(": "));
322 while ((c = pgm_read_byte(logStr))) {
330 * Output a variable argument list log string with the specified priority level.
331 * Only defined for Arduino as depicted below.
333 * @param level - DEBUG, INFO, WARNING, ERROR, FATAL
334 * @param tag - Module name
335 * @param format - variadic log string
337 void OCLogv(LogLevel level, PROGMEM const char * tag, const char * format, ...)
339 char buffer[LINE_BUFFER_SIZE];
341 va_start(ap, format);
343 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
344 Serial.print(buffer);
347 Serial.print(F(": "));
349 while ((c = pgm_read_byte(tag))) {
353 Serial.print(F(": "));
355 vsnprintf(buffer, sizeof(buffer), format, ap);
356 for(char *p = &buffer[0]; *p; p++) // emulate cooked mode for newlines
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 __FlashStringHelper *format, ...)
377 char buffer[LINE_BUFFER_SIZE];
379 va_start(ap, format);
381 GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
382 Serial.print(buffer);
385 Serial.print(F(": "));
387 while ((c = pgm_read_byte(tag))) {
391 Serial.print(F(": "));
394 vsnprintf_P(buffer, sizeof(buffer), (const char *)format, ap); // progmem for AVR
396 vsnprintf(buffer, sizeof(buffer), (const char *)format, ap); // for the rest of the world
398 for(char *p = &buffer[0]; *p; p++) // emulate cooked mode for newlines