change dlog output format
[framework/system/dlog.git] / log.c
diff --git a/log.c b/log.c
index 6c5235f..ac14f05 100755 (executable)
--- a/log.c
+++ b/log.c
@@ -1,11 +1,13 @@
 /*
- * Copyright (C) 2007 The Android Open Source Project
+ * DLOG
+ * Copyright (c) 2005-2008, The Android Open Source Project
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
+ * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *      http://www.apache.org/licenses/LICENSE-2.0
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,9 +15,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifdef HAVE_PTHREADS
 #include <pthread.h>
-#endif
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
@@ -24,7 +24,8 @@
 #include <stdio.h>
 #include <errno.h>
 #include <dlog.h>
-#ifdef SD_JOURNAL_LOG
+#ifdef SD_JOURNAL_SUPPORT
+#include <syslog.h>
 #include <systemd/sd-journal.h>
 #endif
 #define LOG_BUF_SIZE   1024
@@ -41,14 +42,64 @@ static int g_dlog_level = DLOG_SILENT;
 
 static int __dlog_init(log_id_t, log_priority, const char *tag, const char *msg);
 static int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg) = __dlog_init;
-#ifdef HAVE_PTHREADS
 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
-#endif
 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
 {
        return -1;
 }
-#ifdef SD_JOURNAL_LOG
+static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
+{
+       ssize_t ret;
+       int log_fd;
+       struct iovec vec[3];
+
+       if (log_id < LOG_ID_APPS) {
+               if(prio < g_dlog_level) {
+                       return 0;
+               }
+       } else if (LOG_ID_MAX <= log_id) {
+               return 0;
+       }
+       if (log_id < LOG_ID_MAX)
+               log_fd = log_fds[log_id];
+       else
+               return -1; // for TC
+
+       if (!tag)
+               tag = "";
+
+       if (!msg)
+               return -1;
+
+       vec[0].iov_base = (unsigned char *) &prio;
+       vec[0].iov_len  = 1;
+       vec[1].iov_base = (void *) tag;
+       vec[1].iov_len  = strlen(tag) + 1;
+       vec[2].iov_base = (void *) msg;
+       vec[2].iov_len  = strlen(msg) + 1;
+
+       ret = writev(log_fd, vec, 3);
+
+       return ret;
+}
+static char dlog_pri_to_char (log_priority pri)
+{
+       switch (pri) {
+               case DLOG_VERBOSE:       return 'V';
+               case DLOG_DEBUG:         return 'D';
+               case DLOG_INFO:          return 'I';
+               case DLOG_WARN:          return 'W';
+               case DLOG_ERROR:         return 'E';
+               case DLOG_FATAL:         return 'F';
+               case DLOG_SILENT:        return 'S';
+
+               case DLOG_DEFAULT:
+               case DLOG_UNKNOWN:
+               default:
+                                                                return '?';
+       }
+}
+#ifdef SD_JOURNAL_SUPPORT
 static int dlog_pri_to_journal_pri(log_priority prio)
 {
        int journal_prio = LOG_DEBUG;
@@ -81,17 +132,10 @@ static int dlog_pri_to_journal_pri(log_priority prio)
        }
        return journal_prio;
 }
-static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
-{
-       sd_journal_print(dlog_pri_to_journal_pri(prio), "%s %s", tag, msg);
-       return 1;
-}
-#else
-static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
+static int __write_to_log_sd_journal_print(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
 {
        ssize_t ret;
        int log_fd;
-       struct iovec vec[3];
 
        if (log_id < LOG_ID_APPS) {
                if(prio < g_dlog_level) {
@@ -103,55 +147,37 @@ static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char
        if (log_id < LOG_ID_MAX)
                log_fd = log_fds[log_id];
        else
-               return -1; // for TC
-
-       if (!tag)
-               tag = "";
-
-       vec[0].iov_base = (unsigned char *) &prio;
-       vec[0].iov_len  = 1;
-       vec[1].iov_base = (void *) tag;
-       vec[1].iov_len  = strlen(tag) + 1;
-       vec[2].iov_base = (void *) msg;
-       vec[2].iov_len  = strlen(msg) + 1;
-
-       ret = writev(log_fd, vec, 3);
-
-       return ret;
+               return -1;
+       return sd_journal_print(dlog_pri_to_journal_pri(prio), "%c %s: %s",dlog_pri_to_char(prio), tag, msg);
 }
 #endif
 void init_dlog_level(void)
 {
-       char *dlog_level;
-       char *logging_mode;
+       char *dlog_level_env;
+       char *logging_mode_env;
        if (g_logging_on) {
-               logging_mode = getenv("TIZEN_PLATFORMLOGGING_MODE");
-               if (!logging_mode)
+               logging_mode_env = getenv("TIZEN_PLATFORMLOGGING_MODE");
+               if (!logging_mode_env)
                                g_logging_on = 0;
                        else
-                               g_logging_on = atoi(logging_mode);
+                               g_logging_on = atoi(logging_mode_env);
        }
        if (g_logging_on) {
-               dlog_level = getenv("TIZEN_DLOG_LEVEL");
-               if (!dlog_level) {
+               dlog_level_env = getenv("TIZEN_DLOG_LEVEL");
+               if (!dlog_level_env) {
                        g_dlog_level = 8;
                } else {
-                       g_dlog_level = atoi(dlog_level);
+                       g_dlog_level = atoi(dlog_level_env);
                }
        } else
                g_dlog_level = 8;
 }
 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
 {
-#ifdef HAVE_PTHREADS
        pthread_mutex_lock(&log_init_lock);
-#endif
        // get filtering info
        // open device
        if (write_to_log == __dlog_init) {
-#ifdef SD_JOURNAL_LOG
-               write_to_log = __write_to_log_sd_journal_print;
-#else
                init_dlog_level();
 
                log_fds[LOG_ID_MAIN] = open("/dev/"LOG_MAIN, O_WRONLY);
@@ -160,21 +186,22 @@ static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, cons
                log_fds[LOG_ID_APPS] = open("/dev/"LOG_APPS, O_WRONLY);
 
                if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0) {
-                       fprintf(stderr, "open log dev is failed\n");
                        write_to_log = __write_to_log_null;
-               } else
+               } else {
+#ifdef SD_JOURNAL_SUPPORT
+                       write_to_log = __write_to_log_sd_journal_print;
+#else
                        write_to_log = __write_to_log_kernel;
+#endif
+               }
 
                if (log_fds[LOG_ID_SYSTEM] < 0)
                        log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
 
                if (log_fds[LOG_ID_APPS] < 0)
                        log_fds[LOG_ID_APPS] = log_fds[LOG_ID_MAIN];
-#endif
        }
-#ifdef HAVE_PTHREADS
        pthread_mutex_unlock(&log_init_lock);
-#endif
        return write_to_log(log_id, prio, tag, msg);
 }
 
@@ -198,7 +225,7 @@ int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ..
 
        return write_to_log(log_id, prio, tag, buf);
 }
-int _get_logging_on()
+int _get_logging_on(void)
 {
        return g_logging_on;
 }