add smack rule
[framework/system/dlog.git] / log.c
diff --git a/log.c b/log.c
index 4abdcf9..314a42b 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>
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <dlog.h>
 #ifdef SD_JOURNAL_SUPPORT
+#include <syslog.h>
 #include <systemd/sd-journal.h>
 #endif
 #define LOG_BUF_SIZE   1024
 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
 
 static int g_logging_on = 1;
+static int g_dlog_level_init = 0;
 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;
 }
+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_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)
 {
@@ -81,78 +126,39 @@ 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) {
-                       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 = "";
-
-       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 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;
+       g_dlog_level_init = 1;
 }
 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_SUPPORT
-               write_to_log = __write_to_log_sd_journal_print;
-#else
-               init_dlog_level();
 
                log_fds[LOG_ID_MAIN] = open("/dev/"LOG_MAIN, O_WRONLY);
                log_fds[LOG_ID_RADIO] = open("/dev/"LOG_RADIO, O_WRONLY);
@@ -160,27 +166,37 @@ 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);
 }
 
 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
 {
        char buf[LOG_BUF_SIZE];
+       if (!g_dlog_level_init) {
+               init_dlog_level();
+       }
+       if (log_id < LOG_ID_APPS) {
+               if(prio < g_dlog_level) {
+                       return 0;
+               }
+       } else if (LOG_ID_MAX <= log_id) {
+               return 0;
+       }
 
        vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
 
@@ -192,6 +208,17 @@ int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ..
        va_list ap;
        char buf[LOG_BUF_SIZE];
 
+       if (!g_dlog_level_init) {
+               init_dlog_level();
+       }
+       if (log_id < LOG_ID_APPS) {
+               if(prio < g_dlog_level) {
+                       return 0;
+               }
+       } else if (LOG_ID_MAX <= log_id) {
+               return 0;
+       }
+
        va_start(ap, fmt);
        vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
        va_end(ap);