X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fcommon%2Fsrc%2Flogger.c;h=9772f992bc76d01dc52f2ce854335c07a8f2f13b;hb=9015fb1a75f3b43b57153b840f73a2ebc4bb8a63;hp=03b6f8591938b1e898006a3b31fda2b8f846f0be;hpb=af9f5813db40c69283b1d25e3aeade2924e2022e;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/connectivity/common/src/logger.c b/resource/csdk/connectivity/common/src/logger.c index 03b6f85..9772f99 100644 --- a/resource/csdk/connectivity/common/src/logger.c +++ b/resource/csdk/connectivity/common/src/logger.c @@ -18,27 +18,66 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value +// causes header files to expose definitions +// corresponding to the POSIX.1b, Real-time extensions +// (IEEE Std 1003.1b-1993) specification +// +// For this specific file, see use of clock_gettime, +// Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html +// and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif + +// Platform check can be extended to check and/or define more, or could be +// moved into a config.h +#if !defined(__ARDUINO__) && !defined(ARDUINO) +#define HAVE_UNISTD_H 1 +#endif + +// Pull in _POSIX_TIMERS feature test macro to check for +// clock_gettime() support. +#ifdef HAVE_UNISTD_H +#include + +// if we have unistd.h, we're a Unix system +#include +#include +#endif + #include "logger.h" #include "string.h" #include "oic_logger.h" #include "oic_console_logger.h" +#ifndef __TIZEN__ static oic_log_ctx_t *logCtx = 0; static oic_log_level LEVEL_XTABLE[] = { OIC_LOG_DEBUG, OIC_LOG_INFO, OIC_LOG_WARNING, OIC_LOG_ERROR, OIC_LOG_FATAL }; -static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination +#endif + +static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + + 1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination // Convert LogLevel to platform-specific severity level. Store in PROGMEM on Arduino #ifdef __ANDROID__ +#ifdef ADB_SHELL +static const char *LEVEL[] = +{ "DEBUG", "INFO", "WARNING", "ERROR", "FATAL"}; + +#else static android_LogPriority LEVEL[] = { ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL}; +#endif #elif defined __linux__ -static const char * LEVEL[] __attribute__ ((unused)) = +static const char *LEVEL[] __attribute__ ((unused)) = { "DEBUG", "INFO", "WARNING", "ERROR", "FATAL"}; #elif defined ARDUINO #include +#include "Arduino.h" PROGMEM const char level0[] = "DEBUG"; PROGMEM const char level1[] = "INFO"; @@ -46,10 +85,10 @@ PROGMEM const char level2[] = "WARNING"; PROGMEM const char level3[] = "ERROR"; PROGMEM const char level4[] = "FATAL"; -PROGMEM const char * const LEVEL[] = +PROGMEM const char *const LEVEL[] = { level0, level1, level2, level3, level4}; -static void OICLogString(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr); +static void OICLogString(LogLevel level, PROGMEM const char *tag, PROGMEM const char *logStr); #ifdef ARDUINO_ARCH_AVR //Mega2560 and other 8-bit AVR microcontrollers #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_word(addr));} @@ -60,8 +99,9 @@ static void OICLogString(LogLevel level, PROGMEM const char * tag, PROGMEM const #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';} #endif #endif // __ANDROID__ -#ifndef ARDUINO +#ifndef ARDUINO +#ifndef __TIZEN__ void OICLogConfig(oic_log_ctx_t *ctx) { logCtx = ctx; @@ -90,7 +130,7 @@ void OICLogShutdown() * @param tag - Module name * @param logStr - log string */ -void OICLog(LogLevel level, const char * tag, const char * logStr) +void OICLog(LogLevel level, const char *tag, const char *logStr) { if (!logStr || !tag) { @@ -98,7 +138,13 @@ void OICLog(LogLevel level, const char * tag, const char * logStr) } #ifdef __ANDROID__ + +#ifdef ADB_SHELL + printf("%s: %s: %s\n", LEVEL[level], tag, logStr); +#else __android_log_write(LEVEL[level], tag, logStr); +#endif + #elif defined __linux__ if (logCtx && logCtx->write_level) { @@ -107,7 +153,31 @@ void OICLog(LogLevel level, const char * tag, const char * logStr) } else { - printf("%s: %s: %s\n", LEVEL[level], tag, logStr); + int min = 0; + int sec = 0; + int ms = 0; +#ifdef _POSIX_TIMERS + struct timespec when = {}; + clockid_t clk = CLOCK_REALTIME; +#ifdef CLOCK_REALTIME_COARSE + clk = CLOCK_REALTIME_COARSE; +#endif + if (!clock_gettime(clk, &when)) + { + min = (when.tv_sec / 60) % 60; + sec = when.tv_sec % 60; + ms = when.tv_nsec / 1000000; + } +#else + struct timeval now; + if (!gettimeofday(&now, NULL)) + { + min = (now.tv_sec / 60) % 60; + sec = now.tv_sec % 60; + ms = now.tv_usec * 1000; + } +#endif + printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr); } #endif } @@ -120,7 +190,7 @@ void OICLog(LogLevel level, const char * tag, const char * logStr) * @param tag - Module name * @param format - variadic log string */ -void OICLogv(LogLevel level, const char * tag, const char * format, ...) +void OICLogv(LogLevel level, const char *tag, const char *format, ...) { if (!format || !tag) { @@ -143,7 +213,7 @@ void OICLogv(LogLevel level, const char * tag, const char * format, ...) * @param buffer - pointer to buffer of bytes * @param bufferSize - max number of byte in buffer */ -void OICLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize) +void OICLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, uint16_t bufferSize) { if (!buffer || !tag || (bufferSize == 0)) { @@ -173,8 +243,9 @@ void OICLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint OICLog(level, tag, lineBuffer); } } - -#else +#endif //__TIZEN__ +#endif //ARDUINO +#ifdef ARDUINO /** * Initialize the serial logger for Arduino * Only defined for Arduino @@ -193,67 +264,68 @@ void OICLogInit() * @param tag - Module name * @param logStr - log string */ -void OICLogString(LogLevel level, PROGMEM const char * tag, const char * logStr) -{ - if (!logStr || !tag) - { - return; - } - - char buffer[LINE_BUFFER_SIZE]; - - GET_PROGMEM_BUFFER(buffer, &(LEVEL[level])); - Serial.print(buffer); - - char c; - Serial.print(F(": ")); - while ((c = pgm_read_byte(tag))) - { - Serial.write(c); - tag++; - } - Serial.print(F(": ")); - - Serial.println(logStr); -} + void OICLogString(LogLevel level, PROGMEM const char * tag, + const char * logStr) + { + if (!logStr || !tag) + { + return; + } + + char buffer[LINE_BUFFER_SIZE] = {0}; + strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level]))); + Serial.print(buffer); + + char c = NULL; + Serial.print(F(": ")); + while ((c = pgm_read_byte(tag))) + { + Serial.write(c); + tag++; + } + Serial.print(F(": ")); + + Serial.println(logStr); + } /** - * Output the contents of the specified buffer (in hex) with the specified priority level. + * Output the contents of the specified buffer (in hex) with the specified + * priority level. * * @param level - DEBUG, INFO, WARNING, ERROR, FATAL * @param tag - Module name * @param buffer - pointer to buffer of bytes * @param bufferSize - max number of byte in buffer */ -void OICLogBuffer(LogLevel level, PROGMEM const char * tag, const uint8_t * buffer, uint16_t bufferSize) -{ - if (!buffer || !tag || (bufferSize == 0)) - { - return; - } - - char lineBuffer[LINE_BUFFER_SIZE] = - { 0}; - uint8_t lineIndex = 0; - for (uint8_t i = 0; i < bufferSize; i++) - { + void OICLogBuffer(LogLevel level, PROGMEM const char * tag, + const uint8_t * buffer, uint16_t bufferSize) + { + if (!buffer || !tag || (bufferSize == 0)) + { + return; + } + + char lineBuffer[LINE_BUFFER_SIZE] = {0}; + uint8_t lineIndex = 0; + for (uint8_t i = 0; i < bufferSize; i++) + { // Format the buffer data into a line snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]); lineIndex++; - // Output 16 values per line - if (((i+1)%16) == 0) - { - OICLogString(level, tag, lineBuffer); - memset(lineBuffer, 0, sizeof lineBuffer); - lineIndex = 0; - } - } - // Output last values in the line, if any - if (bufferSize % 16) - { - OICLogString(level, tag, lineBuffer); - } -} + // Output 16 values per line + if (((i+1)%16) == 0) + { + OICLogString(level, tag, lineBuffer); + memset(lineBuffer, 0, sizeof lineBuffer); + lineIndex = 0; + } + } + // Output last values in the line, if any + if (bufferSize % 16) + { + OICLogString(level, tag, lineBuffer); + } + } /** * Output a log string with the specified priority level. @@ -263,18 +335,16 @@ void OICLogBuffer(LogLevel level, PROGMEM const char * tag, const uint8_t * buff * @param tag - Module name * @param logStr - log string */ -void OICLog(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logStr) +void OICLog(LogLevel level, PROGMEM const char *tag, const int16_t lineNum, + PROGMEM const char *logStr) { if (!logStr || !tag) { return; } - - char buffer[LINE_BUFFER_SIZE]; - + char buffer[LINE_BUFFER_SIZE] = {0}; GET_PROGMEM_BUFFER(buffer, &(LEVEL[level])); Serial.print(buffer); - char c; Serial.print(F(": ")); while ((c = pgm_read_byte(tag))) @@ -283,7 +353,8 @@ void OICLog(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logSt tag++; } Serial.print(F(": ")); - + Serial.print(lineNum); + Serial.print(F(": ")); while ((c = pgm_read_byte(logStr))) { Serial.write(c); @@ -300,29 +371,34 @@ void OICLog(LogLevel level, PROGMEM const char * tag, PROGMEM const char * logSt * @param tag - Module name * @param format - variadic log string */ -void OICLogv(LogLevel level, PROGMEM const char * tag, const char * format, ...) +void OICLogv(LogLevel level, PROGMEM const char *tag, const int16_t lineNum, + PROGMEM const char *format, ...) { char buffer[LINE_BUFFER_SIZE]; va_list ap; va_start(ap, format); - GET_PROGMEM_BUFFER(buffer, &(LEVEL[level])); Serial.print(buffer); char c; Serial.print(F(": ")); - - while ((c = pgm_read_byte(tag))) - { - Serial.write(c); - tag++; - } + while ((c = pgm_read_byte(tag))) { + Serial.write(c); + tag++; + } + Serial.print(F(": ")); + Serial.print(lineNum); Serial.print(F(": ")); +#ifdef __AVR__ + vsnprintf_P(buffer, sizeof(buffer), format, ap); +#else vsnprintf(buffer, sizeof(buffer), format, ap); - for(char *p = &buffer[0]; *p; p++) // emulate cooked mode for newlines +#endif + for (char *p = &buffer[0]; *p; p++) { - if(*p == '\n') + // emulate cooked mode for newlines + if (*p == '\n') { Serial.write('\r'); } @@ -339,23 +415,23 @@ void OICLogv(LogLevel level, PROGMEM const char * tag, const char * format, ...) * @param tag - Module name * @param format - variadic log string */ -void OICLogv(LogLevel level, PROGMEM const char * tag, const __FlashStringHelper *format, ...) +void OICLogv(LogLevel level, const char *tag, const __FlashStringHelper *format, ...) { char buffer[LINE_BUFFER_SIZE]; va_list ap; va_start(ap, format); + // strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level]))); + // Serial.print(buffer); - GET_PROGMEM_BUFFER(buffer, &(LEVEL[level])); - Serial.print(buffer); - - char c; + Serial.print(LEVEL[level]); + // char c; Serial.print(F(": ")); - while ((c = pgm_read_byte(tag))) - { - Serial.write(c); - tag++; - } + /*while ((c = pgm_read_byte(tag))) { + Serial.write(c); + tag++; + }*/ + Serial.print(tag); Serial.print(F(": ")); #ifdef __AVR__ @@ -363,17 +439,20 @@ void OICLogv(LogLevel level, PROGMEM const char * tag, const __FlashStringHelper #else vsnprintf(buffer, sizeof(buffer), (const char *)format, ap); // for the rest of the world #endif - for(char *p = &buffer[0]; *p; p++) // emulate cooked mode for newlines + for (char *p = &buffer[0]; *p; p++) { - if(*p == '\n') + // emulate cooked mode for newlines + if (*p == '\n') { - Serial.write('\r'); + // Serial.write('\r'); + Serial.print('\r'); } - Serial.write(*p); + //Serial.write(*p); + Serial.print(p); } Serial.println(); va_end(ap); } -#endif +#endif //ARDUINO