replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / logger / src / logger.c
index edeb167..6758d09 100644 (file)
 #include "string.h"
 #include "logger_types.h"
 
+// log level
+LogLevel g_level = DEBUG;
+// privacy log
+bool g_hidePrivateLogEntries = false;
+
 #ifndef __TIZEN__
 static oc_log_ctx_t *logCtx = 0;
 #endif
 
 #if defined(_MSC_VER)
 #define LINE_BUFFER_SIZE (16 * 2) + 16 + 1  // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
+#define S_LINE_BUFFER_SIZE (50 * 2) + 50 + 1  // Show 50 bytes, 2 chars/byte, spaces between bytes, null termination
 #else
 static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1;  // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
+static const uint16_t S_LINE_BUFFER_SIZE = (50 * 2) + 50 + 1;  // Show 50 bytes, 2 chars/byte, spaces between bytes, null termination
 #endif //defined(_MSC_VER)
 
 #ifdef __ANDROID__
-#elif defined __linux__ || defined __APPLE__ || defined _WIN32
+#elif defined __linux__ || defined __APPLE__ || defined _WIN32 || defined(__TIZENRT__)
 static oc_log_level LEVEL_XTABLE[] = {OC_LOG_DEBUG, OC_LOG_INFO,
                                       OC_LOG_WARNING, OC_LOG_ERROR, OC_LOG_FATAL};
 #endif
@@ -81,7 +88,7 @@ static oc_log_level LEVEL_XTABLE[] = {OC_LOG_DEBUG, OC_LOG_INFO,
     static android_LogPriority LEVEL[] =
     {ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
 #endif
-#elif defined(__linux__) || defined(__APPLE__) || defined(__msys_nt__)
+#elif defined(__linux__) || defined(__APPLE__) || defined(__msys_nt__)|| defined(__TIZENRT__)
     static const char * LEVEL[] __attribute__ ((unused)) = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
 #elif defined(_MSC_VER)
     static const char * LEVEL[] = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
@@ -155,6 +162,65 @@ void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint1
         OCLogv(level, tag, "%s", lineBuffer);
     }
 }
+
+void OCPrintCALogBuffer(LogLevel level, const char *tag, const uint8_t *buffer,
+                        uint16_t bufferSize, uint8_t isHeader)
+{
+    if (!buffer || !tag || (bufferSize == 0))
+    {
+        return;
+    }
+
+    // No idea why the static initialization won't work here, it seems the compiler is convinced
+    // that this is a variable-sized object.
+    char lineBuffer[S_LINE_BUFFER_SIZE];
+    int lineIndex = 0;
+    int i;
+    for (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 50 values per line
+        if (((i+1)%50) == 0)
+        {
+            if (1 == isHeader)
+            {
+                OCLogv(level, tag, "| Analyzer(Header) | %s", lineBuffer);
+            }
+            else
+            {
+                OCLogv(level, tag, "| Analyzer(Body) | %s", lineBuffer);
+            }
+            memset(lineBuffer, 0, sizeof lineBuffer);
+            lineIndex = 0;
+        }
+    }
+
+    if (bufferSize % 50)
+    {
+        if (1 == isHeader)
+        {
+            OCLogv(level, tag, "| Analyzer(Header) | %s", lineBuffer);
+        }
+        else
+        {
+            OCLogv(level, tag, "| Analyzer(Body) | %s", lineBuffer);
+        }
+    }
+}
+
+void OCSetLogLevel(LogLevel level, bool hidePrivateLogEntries)
+{
+    g_level = level;
+    g_hidePrivateLogEntries = hidePrivateLogEntries;
+}
+
+bool OCGetPrivateLogLevel()
+{
+    return g_hidePrivateLogEntries;
+}
+
 #ifndef __TIZEN__
 void OCLogConfig(oc_log_ctx_t *ctx)
 {
@@ -168,7 +234,7 @@ void OCLogInit()
 
 void OCLogShutdown()
 {
-#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
+#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)|| defined(__TIZENRT__)
     if (logCtx && logCtx->destroy)
     {
         logCtx->destroy(logCtx);
@@ -189,6 +255,17 @@ void OCLogv(LogLevel level, const char * tag, const char * format, ...)
     if (!format || !tag) {
         return;
     }
+
+    if (g_level > level && ERROR != level && WARNING != level && FATAL != level)
+    {
+        return;
+    }
+
+    if (true == g_hidePrivateLogEntries && INFO_PRIVATE == level)
+    {
+        return;
+    }
+
     char buffer[MAX_LOG_V_BUFFER_SIZE] = {0};
     va_list args;
     va_start(args, format);
@@ -212,6 +289,31 @@ void OCLog(LogLevel level, const char * tag, const char * logStr)
        return;
     }
 
+    if (g_level > level && ERROR != level && WARNING != level && FATAL != level)
+    {
+        return;
+    }
+
+    if (true == g_hidePrivateLogEntries && INFO_PRIVATE == level)
+    {
+        return;
+    }
+
+    switch(level)
+    {
+        case DEBUG_LITE:
+            level = DEBUG;
+            break;
+        case INFO_LITE:
+            level = INFO;
+            break;
+        case INFO_PRIVATE:
+            level = INFO;
+            break;
+        default:
+            break;
+    }
+
    #ifdef __ANDROID__
 
    #ifdef ADB_SHELL