Refactor: split logutil and libdlog backend handling into separate files. 36/67436/8
authorMichal Bloch <m.bloch@samsung.com>
Mon, 30 May 2016 14:47:28 +0000 (16:47 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Mon, 30 May 2016 15:07:56 +0000 (17:07 +0200)
Change-Id: I511c42eea8e0f624402ff0d456e9ee41317bb3cc
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
Signed-off-by: Kazimierz Krosman <k.krosman@samsung.com>
Makefile.am
configure.ac
include/logcommon.h
packaging/dlog.spec
src/libdlog/log.c
src/libdlog/log_android.c [new file with mode: 0644]
src/libdlog/log_journal.c [new file with mode: 0644]
src/libdlog/log_kmsg.c [new file with mode: 0644]
src/logutil/logutil_journal.c [new file with mode: 0755]
src/logutil/logutil_kmsg_logger.c [moved from src/logutil/logutil.c with 79% similarity]

index f840b49..7b30c61 100755 (executable)
@@ -26,6 +26,21 @@ libdlog_la_SOURCES =  \
        src/libdlog/loglimiter.c \
        include/loglimiter.h
 
+if WITH_ANDROID_LOGGER
+libdlog_la_SOURCES += \
+       src/libdlog/log_android.c
+endif
+
+if WITH_KMSG
+libdlog_la_SOURCES += \
+       src/libdlog/log_kmsg.c
+endif
+
+if WITH_SYSTEMD_JOURNAL
+libdlog_la_SOURCES += \
+       src/libdlog/log_journal.c
+endif
+
 libdlog_la_LIBADD = -lpthread \
        $(CAPI_BASE_COMMON_LIBS)
 
@@ -53,7 +68,6 @@ dlogutil_SOURCES = \
        src/shared/dlog_ioctl.c \
        src/shared/logcommon.c \
        src/shared/logprint.c \
-       src/logutil/logutil.c \
        src/shared/queued_entry.c \
        src/shared/log_file.c \
        include/dlog_ioctl.h \
@@ -62,6 +76,18 @@ dlogutil_SOURCES = \
        include/log_file.h \
        include/logprint.h
 
+if WITH_ANDROID_LOGGER
+dlogutil_SOURCES += src/logutil/logutil_kmsg_logger.c
+endif
+
+if WITH_KMSG
+dlogutil_SOURCES += src/logutil/logutil_kmsg_logger.c
+endif
+
+if WITH_SYSTEMD_JOURNAL
+dlogutil_SOURCES += src/logutil/logutil_journal.c
+endif
+
 sbin_PROGRAMS = dloginit
 
 if !WITH_SYSTEMD_JOURNAL
index d7df302..8172eae 100755 (executable)
@@ -19,6 +19,16 @@ AC_PROG_LIBTOOL
 AC_ARG_ENABLE(journal, AS_HELP_STRING([--enable-journal], [enable systemd journal]),
     [with_systemd_journal=yes],
     with_systemd_journal=no)
+
+#check for backends
+AC_ARG_ENABLE(kmsg, AS_HELP_STRING([--enable-kmsg], [enable kmsg]),
+    [with_kmsg=yes],
+    with_kmsg=no)
+
+AC_ARG_ENABLE(android_logger, AS_HELP_STRING([--enable-android-logger], [enable android logger]),
+    [with_android_logger=yes],
+    with_android_logger=no)
+
 if test "x$with_systemd_journal" != "xno"; then
        PKG_CHECK_MODULES(systemd_journal, [libsystemd-journal],
                [AC_DEFINE(HAVE_SYSTEMD_JOURNAL, 1, [Define if systemd journal is available])
@@ -30,6 +40,9 @@ if test "x$with_systemd_journal" != "xno"; then
 fi
 AM_CONDITIONAL(HAVE_SYSTEMD_JOURNAL, [test "x$have_systemd_journal" = "xyes"])
 AM_CONDITIONAL(WITH_SYSTEMD_JOURNAL, [test "x$with_systemd_journal" = "xyes"])
+AM_CONDITIONAL(WITH_ANDROID_LOGGER, [test "x$with_android_logger" = "xyes"])
+AM_CONDITIONAL(WITH_KMSG, [test "x$with_kmsg" = "xyes"])
+
 if test "x$have_systemd_journal" = "xyes" ; then
        systemd_journal=systemd-journal
 fi
index 724188e..5e53104 100644 (file)
 #define LOG_APPS_CONF_PREFIX    "LOG_APPS="
 #define LOG_TYPE_CONF_PREFIX   "LOG_TYPE="
 
+/*
+ * LOG_ATOMIC_SIZE is calculated according to kernel value
+ * 976 = 1024(size of log line) - 48(size of max prefix length)
+ */
+#define LOG_INFO_SIZE   16
+#define LOG_ATOMIC_SIZE 976
+#define LOG_BUF_SIZE    1024
+#define LOG_MAX_SIZE    4076
+
+#define LOG_CONFIG_FILE TZ_SYS_ETC"/dlog.conf"
+
+#define gettid() syscall(SYS_gettid)
+
 int get_log_dev_names(char devs[LOG_ID_MAX][PATH_MAX]);
 
 log_id_t log_id_by_name(const char *name);
index e0ecbc7..f903cd0 100755 (executable)
@@ -91,6 +91,12 @@ cp %{SOURCE102} .
                %if %{?backend_journal} == ON
                        --enable-journal \
                %endif
+               %if %{?backend_kmsg} == ON
+                       --enable-kmsg \
+               %endif
+               %if %{?backend_logger} == ON
+                       --enable-android-logger \
+               %endif
                        --enable-debug_mode \
                        TZ_SYS_ETC=%{TZ_SYS_ETC}
 make %{?jobs:-j%jobs} \
index 0755e9c..e866ec3 100755 (executable)
  * limitations under the License.
  */
 
-#include "config.h"
 #include <pthread.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-#include <fcntl.h>
-#include <sys/uio.h>
-#include <sys/syscall.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
+
 #include <dlog.h>
 #include <logcommon.h>
 #include "loglimiter.h"
 #include "logconfig.h"
-#ifdef DLOG_BACKEND_JOURNAL
-#define SD_JOURNAL_SUPPRESS_LOCATION 1
-#include <syslog.h>
-#include <systemd/sd-journal.h>
-#endif
 #ifdef FATAL_ON
 #include <assert.h>
 #endif
 
-#define VALUE_MAX 2
-#define LOG_CONFIG_FILE TZ_SYS_ETC"/dlog.conf"
-
-#define LOG_MAX_SIZE   4076
+/*
+ * @brief Points to a function which writes a log message
+ * @details The function pointed to depends on the backend used
+ * @param log_id ID of the buffer to log to. Belongs to [0, LOG_ID_MAX)
+ * @param prio Priority of the message.
+ * @param tag The message tag, identifies the sender.
+ * @param msg The contents of the message.
+ * @retval Returns the number of bytes written on success and a negative error value on error.
+ * @seealso __dlog_init_backend
+ */
+int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg);
 
-static int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg) = NULL;
 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
 static struct log_config config;
-
-#if DLOG_BACKEND_JOURNAL
-#define LOG_BUF_SIZE 1024
-#define LOG_INFO_SIZE 16
-
-static inline int dlog_pri_to_journal_pri(log_priority prio)
-{
-       static int pri_table[DLOG_PRIO_MAX] = {
-               [DLOG_UNKNOWN] = LOG_DEBUG,
-               [DLOG_DEFAULT] = LOG_DEBUG,
-               [DLOG_VERBOSE] = LOG_DEBUG,
-               [DLOG_DEBUG] = LOG_DEBUG,
-               [DLOG_INFO] = LOG_INFO,
-               [DLOG_WARN] = LOG_WARNING,
-               [DLOG_ERROR] = LOG_ERR,
-               [DLOG_FATAL] = LOG_CRIT,
-               [DLOG_SILENT] = -1,
-       };
-
-       if (prio < 0 || prio >= DLOG_PRIO_MAX)
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       return pri_table[prio];
-}
-
-static inline const char* dlog_id_to_string(log_id_t log_id)
-{
-       static const char* id_table[LOG_ID_MAX] = {
-               [LOG_ID_MAIN]   = "log_main",
-               [LOG_ID_RADIO]  = "log_radio",
-               [LOG_ID_SYSTEM] = "log_system",
-               [LOG_ID_APPS]   = "log_apps",
-       };
-
-       if (log_id == LOG_ID_INVALID || log_id >= LOG_ID_MAX || !id_table[log_id])
-               return "UNKNOWN";
-
-       return id_table[log_id];
-}
-
-static int __write_to_log_sd_journal(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
-{
-       const char *lid_str = dlog_id_to_string(log_id);
-       int ret;
-
-       pid_t tid = (pid_t)syscall(SYS_gettid);
-
-       if (!msg)
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       if (strncmp(lid_str, "UNKNOWN", 7) == 0)
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       if (prio < DLOG_VERBOSE || prio >= DLOG_PRIO_MAX)
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       if (!tag)
-               tag = "";
-
-       struct iovec vec[5];
-       char _msg[LOG_MAX_SIZE + 8];
-       char _prio[LOG_INFO_SIZE + 9];
-       char _tag[LOG_BUF_SIZE + 8];
-       char _log_id[LOG_INFO_SIZE + 7];
-       char _tid[LOG_INFO_SIZE + 4];
-
-       snprintf(_msg, LOG_MAX_SIZE + 8, "MESSAGE=%s", msg);
-       vec[0].iov_base = (void *)_msg;
-       vec[0].iov_len = strlen(vec[0].iov_base);
-
-       snprintf(_prio, LOG_INFO_SIZE + 9, "PRIORITY=%i", dlog_pri_to_journal_pri(prio));
-       vec[1].iov_base = (void *)_prio;
-       vec[1].iov_len = strlen(vec[1].iov_base);
-
-       snprintf(_tag, LOG_BUF_SIZE + 8, "LOG_TAG=%s", tag);
-       vec[2].iov_base = (void *)_tag;
-       vec[2].iov_len = strlen(vec[2].iov_base);
-
-       snprintf(_log_id, LOG_INFO_SIZE + 7, "LOG_ID=%s", lid_str);
-       vec[3].iov_base = (void *)_log_id;
-       vec[3].iov_len = strlen(vec[3].iov_base);
-
-       snprintf(_tid, LOG_INFO_SIZE + 4, "TID=%d", tid);
-       vec[4].iov_base = (void *)_tid;
-       vec[4].iov_len = strlen(vec[4].iov_base);
-
-       ret = sd_journal_sendv(vec, 5);
-
-       if (ret == 0)
-               return (vec[0].iov_len + vec[1].iov_len + vec[2].iov_len + vec[3].iov_len + vec[4].iov_len);
-       else
-               return ret;
-}
-
-#else
-
-static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
-static char log_devs[LOG_ID_MAX][PATH_MAX];
+extern void __dlog_init_backend();
 
 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
 {
        return DLOG_ERROR_NOT_PERMITTED;
 }
 
-#if DLOG_BACKEND_KMSG
-
-/*
- * LOG_ATOMIC_SIZE is calculated according to kernel value
- * 976 = 1024(size of log line) - 48(size of max prefix length)
- */
-#define LOG_ATOMIC_SIZE        976
-
-static int __write_to_log_kmsg(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
-{
-       ssize_t ret;
-       int log_fd;
-       ssize_t prefix_len, written_len, last_msg_len;
-       char buf[LOG_ATOMIC_SIZE];
-       const char *msg_ptr = msg;
-
-       if (log_id > LOG_ID_INVALID && log_id < LOG_ID_MAX)
-               log_fd = log_fds[log_id];
-       else
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       if (prio < DLOG_VERBOSE || prio >= DLOG_PRIO_MAX)
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       if (!tag)
-               tag = "";
-
-       if (!msg)
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       ret = 0;
-       prefix_len = strlen(tag) + 3;
-       while (1) {
-               last_msg_len = snprintf(buf, LOG_ATOMIC_SIZE, "%s;%d;%s", tag, prio, msg_ptr);
-               written_len = write(log_fd, buf,
-                               last_msg_len < LOG_ATOMIC_SIZE - 1 ? last_msg_len : LOG_ATOMIC_SIZE - 1);
-
-               if (written_len < 0)
-                       return -errno;
-
-               ret += (written_len - prefix_len);
-               msg_ptr += (written_len - prefix_len);
-
-               if (*(msg_ptr) == '\0')
-                       break;
-       }
-
-       return ret;
-}
-#elif DLOG_BACKEND_LOGGER
-
-static int __write_to_log_logger(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_INVALID && log_id < LOG_ID_MAX)
-               log_fd = log_fds[log_id];
-       else
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       if (prio < DLOG_VERBOSE || prio >= DLOG_PRIO_MAX)
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       if (!tag)
-               tag = "";
-
-       if (!msg)
-               return DLOG_ERROR_INVALID_PARAMETER;
-
-       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);
-       if (ret < 0)
-           ret = -errno;
-
-       return ret;
-}
-
-#endif
-#endif
-
 static void __configure(void)
 {
        if (0 > __log_config_read(LOG_CONFIG_FILE, &config)) {
@@ -256,34 +63,10 @@ static void __configure(void)
 static void __dlog_init(void)
 {
        pthread_mutex_lock(&log_init_lock);
+       write_to_log = __write_to_log_null;
        __configure();
-
-#ifdef DLOG_BACKEND_JOURNAL
-       write_to_log = __write_to_log_sd_journal;
-#else
-       if (0 == get_log_dev_names(log_devs)) {
-                       log_fds[LOG_ID_MAIN] = open(log_devs[LOG_ID_MAIN], O_WRONLY);
-                       log_fds[LOG_ID_SYSTEM] = open(log_devs[LOG_ID_SYSTEM], O_WRONLY);
-                       log_fds[LOG_ID_RADIO] = open(log_devs[LOG_ID_RADIO], O_WRONLY);
-                       log_fds[LOG_ID_APPS] = open(log_devs[LOG_ID_APPS], O_WRONLY);
-               }
-               if (log_fds[LOG_ID_MAIN] < 0) {
-                       write_to_log = __write_to_log_null;
-               } else {
-#if DLOG_BACKEND_KMSG
-                       write_to_log = __write_to_log_kmsg;
-#elif DLOG_BACKEND_LOGGER
-                       write_to_log = __write_to_log_logger;
-#endif
-               }
-               if (log_fds[LOG_ID_RADIO] < 0)
-                       log_fds[LOG_ID_RADIO] = log_fds[LOG_ID_MAIN];
-               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
-               pthread_mutex_unlock(&log_init_lock);
+       __dlog_init_backend();
+       pthread_mutex_unlock(&log_init_lock);
 }
 
 void __dlog_fatal_assert(int prio)
diff --git a/src/libdlog/log_android.c b/src/libdlog/log_android.c
new file mode 100644 (file)
index 0000000..4df1921
--- /dev/null
@@ -0,0 +1,64 @@
+#include <fcntl.h>
+#include <sys/socket.h>
+
+#include <dlog.h>
+#include <logcommon.h>
+
+extern int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg);
+
+static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
+static char log_devs[LOG_ID_MAX][PATH_MAX];
+
+static int __write_to_log_android(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_INVALID && log_id < LOG_ID_MAX)
+               log_fd = log_fds[log_id];
+       else
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       if (prio < DLOG_VERBOSE || prio >= DLOG_PRIO_MAX)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       if (!tag)
+               tag = "";
+
+       if (!msg)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       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);
+       if (ret < 0)
+               ret = -errno;
+
+       return ret;
+}
+
+void __dlog_init_backend()
+{
+       if (0 == get_log_dev_names(log_devs)) {
+               log_fds[LOG_ID_MAIN]   = open(log_devs[LOG_ID_MAIN],   O_WRONLY);
+               log_fds[LOG_ID_SYSTEM] = open(log_devs[LOG_ID_SYSTEM], O_WRONLY);
+               log_fds[LOG_ID_RADIO]  = open(log_devs[LOG_ID_RADIO],  O_WRONLY);
+               log_fds[LOG_ID_APPS]   = open(log_devs[LOG_ID_APPS],   O_WRONLY);
+       }
+       if (log_fds[LOG_ID_MAIN] >= 0)
+               write_to_log = __write_to_log_android;
+
+       if (log_fds[LOG_ID_RADIO]  < 0)
+               log_fds[LOG_ID_RADIO]  = log_fds[LOG_ID_MAIN];
+       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];
+}
+
diff --git a/src/libdlog/log_journal.c b/src/libdlog/log_journal.c
new file mode 100644 (file)
index 0000000..68c4f15
--- /dev/null
@@ -0,0 +1,111 @@
+#include <unistd.h>
+#include <sys/syscall.h>
+
+#include <dlog.h>
+#include <logcommon.h>
+
+#define SD_JOURNAL_SUPPRESS_LOCATION 1
+#include <syslog.h>
+#include <systemd/sd-journal.h>
+
+extern int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg);
+
+static inline int dlog_pri_to_journal_pri(log_priority prio)
+{
+       static int pri_table[DLOG_PRIO_MAX] = {
+               [DLOG_UNKNOWN] = LOG_DEBUG,
+               [DLOG_DEFAULT] = LOG_DEBUG,
+               [DLOG_VERBOSE] = LOG_DEBUG,
+               [DLOG_DEBUG]   = LOG_DEBUG,
+               [DLOG_INFO]    = LOG_INFO,
+               [DLOG_WARN]    = LOG_WARNING,
+               [DLOG_ERROR]   = LOG_ERR,
+               [DLOG_FATAL]   = LOG_CRIT,
+               [DLOG_SILENT]  = -1,
+       };
+
+       if (prio < 0 || prio >= DLOG_PRIO_MAX)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       return pri_table[prio];
+}
+
+static inline const char* dlog_id_to_string(log_id_t log_id)
+{
+       static const char* id_table[LOG_ID_MAX] = {
+               [LOG_ID_MAIN]   = "log_main",
+               [LOG_ID_RADIO]  = "log_radio",
+               [LOG_ID_SYSTEM] = "log_system",
+               [LOG_ID_APPS]   = "log_apps",
+       };
+
+       if (log_id <= LOG_ID_INVALID || log_id >= LOG_ID_MAX || !id_table[log_id])
+               return "UNKNOWN";
+
+       return id_table[log_id];
+}
+
+/*
+ * Returns -errno on failure. Return 0 on success.
+ * NOTE: if the journal daemon is not alive, the function does nothing but still returns success.
+ */
+static int __write_to_log_journal(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
+{
+       const char *lid_str = dlog_id_to_string(log_id);
+       int ret;
+
+       pid_t tid = (pid_t)gettid();
+
+       if(!msg)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       if(strncmp(lid_str, "UNKNOWN", 7) == 0)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       if(prio < DLOG_VERBOSE || prio >= DLOG_PRIO_MAX)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       if(!tag)
+               tag = "";
+
+       struct iovec vec[5];
+       char _msg   [LOG_MAX_SIZE + 8];
+       char _prio  [LOG_INFO_SIZE + 9];
+       char _tag   [LOG_BUF_SIZE + 8];
+       char _log_id[LOG_INFO_SIZE + 7];
+       char _tid   [LOG_INFO_SIZE + 4];
+
+       snprintf(_msg, LOG_MAX_SIZE + 8, "MESSAGE=%s", msg);
+       vec[0].iov_base = (void *)_msg;
+       vec[0].iov_len = strlen(vec[0].iov_base);
+
+       snprintf(_prio, LOG_INFO_SIZE + 9, "PRIORITY=%i", dlog_pri_to_journal_pri(prio));
+       vec[1].iov_base = (void *)_prio;
+       vec[1].iov_len = strlen(vec[1].iov_base);
+
+       snprintf(_tag, LOG_BUF_SIZE + 8, "LOG_TAG=%s", tag);
+       vec[2].iov_base = (void *)_tag;
+       vec[2].iov_len = strlen(vec[2].iov_base);
+
+       snprintf(_log_id, LOG_INFO_SIZE + 7, "LOG_ID=%s", lid_str);
+       vec[3].iov_base = (void *)_log_id;
+       vec[3].iov_len = strlen(vec[3].iov_base);
+
+       snprintf(_tid, LOG_INFO_SIZE + 4, "TID=%d", tid);
+       vec[4].iov_base = (void *)_tid;
+       vec[4].iov_len = strlen(vec[4].iov_base);
+
+       /* NOTE: this returns 0 (success) even if the journal daemon is not alive as long as no other failures occured. */
+       ret = sd_journal_sendv(vec, 5);
+
+       if (ret == 0)
+               return (vec[0].iov_len + vec[1].iov_len + vec[2].iov_len + vec[3].iov_len + vec[4].iov_len);
+       else
+               return ret;
+}
+
+void __dlog_init_backend()
+{
+       write_to_log = __write_to_log_journal;
+}
+
diff --git a/src/libdlog/log_kmsg.c b/src/libdlog/log_kmsg.c
new file mode 100644 (file)
index 0000000..afe3712
--- /dev/null
@@ -0,0 +1,72 @@
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <dlog.h>
+#include <logcommon.h>
+
+extern int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg);
+
+static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
+static char log_devs[LOG_ID_MAX][PATH_MAX];
+
+static int __write_to_log_kmsg(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
+{
+       ssize_t ret;
+       int log_fd;
+       ssize_t prefix_len, written_len, last_msg_len;
+       char buf[LOG_ATOMIC_SIZE];
+       const char *msg_ptr = msg;
+
+       if (log_id > LOG_ID_INVALID && log_id < LOG_ID_MAX)
+               log_fd = log_fds[log_id];
+       else
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       if (prio < DLOG_VERBOSE || prio >= DLOG_PRIO_MAX)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       if (!tag)
+               tag = "";
+
+       if (!msg)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       ret = 0;
+       prefix_len = strlen(tag) + 3;
+       while (1) {
+               last_msg_len = snprintf(buf, LOG_ATOMIC_SIZE, "%s;%d;%s", tag, prio, msg_ptr);
+               written_len = write(log_fd, buf,
+                               last_msg_len < LOG_ATOMIC_SIZE - 1 ? last_msg_len : LOG_ATOMIC_SIZE - 1);
+
+               if (written_len < 0)
+                       return -errno;
+
+               ret += (written_len - prefix_len);
+               msg_ptr += (written_len - prefix_len);
+
+               if (*(msg_ptr) == '\0')
+                       break;
+       }
+
+       return ret;
+}
+
+void __dlog_init_backend()
+{
+       if (0 == get_log_dev_names(log_devs)) {
+               log_fds[LOG_ID_MAIN]   = open(log_devs[LOG_ID_MAIN],   O_WRONLY);
+               log_fds[LOG_ID_SYSTEM] = open(log_devs[LOG_ID_SYSTEM], O_WRONLY);
+               log_fds[LOG_ID_RADIO]  = open(log_devs[LOG_ID_RADIO],  O_WRONLY);
+               log_fds[LOG_ID_APPS]   = open(log_devs[LOG_ID_APPS],   O_WRONLY);
+       }
+       if (log_fds[LOG_ID_MAIN] >= 0)
+               write_to_log = __write_to_log_kmsg;
+
+       if (log_fds[LOG_ID_RADIO]  < 0)
+               log_fds[LOG_ID_RADIO]  = log_fds[LOG_ID_MAIN];
+       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];
+}
+
diff --git a/src/logutil/logutil_journal.c b/src/logutil/logutil_journal.c
new file mode 100755 (executable)
index 0000000..0def4e2
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2005-2008, The Android Open Source Project
+ * Copyright (c) 2009-2015, Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "dlog.h"
+
+#include <systemd/sd-journal.h>
+
+int main (int argc, char **argv)
+{
+       int r;
+       sd_journal *j;
+
+       static const char pri_table[DLOG_PRIO_MAX] = {
+               [LOG_DEBUG] = 'D',
+               [LOG_INFO] = 'I',
+               [LOG_WARNING] = 'W',
+               [LOG_ERR] = 'E',
+               [LOG_CRIT] = 'F',
+       };
+
+       r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
+       if (r < 0) {
+               fprintf(stderr, "Failed to open journal: %d\n", -r);
+
+               return 1;
+       }
+
+       fprintf(stderr, "read\n");
+       SD_JOURNAL_FOREACH(j) {
+               const char *priority, *log_tag, *tid,  *message;
+               size_t l;
+               int prio_idx;
+
+               r = sd_journal_get_data(j, "PRIORITY", (const void **)&priority, &l);
+               if (r < 0)
+                       continue;
+
+               prio_idx = atoi(priority+9);
+               if (prio_idx < LOG_CRIT || prio_idx > LOG_DEBUG) {
+                       fprintf(stdout, "Wrong priority");
+                       continue;
+               }
+
+               r = sd_journal_get_data(j, "LOG_TAG", (const void **)&log_tag, &l);
+               if (r < 0)
+                       continue;
+
+               r = sd_journal_get_data(j, "TID", (const void **)&tid, &l);
+               if (r < 0)
+                       continue;
+
+               r = sd_journal_get_data(j, "MESSAGE", (const void **)&message, &l);
+               if (r < 0)
+                       continue;
+
+               fprintf(stdout, "%c/%s(%5d): %s\n", pri_table[prio_idx], log_tag+8, atoi(tid+4), message+8);
+       }
+
+       fprintf(stderr, "wait\n");
+       for (;;) {
+               const char *log_tag, *priority, *tid, *message;
+               size_t l;
+               int prio_idx;
+
+               if (sd_journal_seek_tail(j) < 0) {
+                       fprintf(stderr, "Couldn't find journal");
+               } else if (sd_journal_previous(j) > 0) {
+                       r = sd_journal_get_data(j, "PRIORITY", (const void **)&priority, &l);
+                       if (r < 0)
+                               continue;
+
+                       prio_idx = atoi(priority+9);
+                       if (prio_idx < LOG_CRIT || prio_idx > LOG_DEBUG) {
+                               fprintf(stdout, "Wrong priority");
+                               continue;
+                       }
+
+                       r = sd_journal_get_data(j, "LOG_TAG", (const void **)&log_tag, &l);
+                       if (r < 0)
+                               continue;
+
+                       r = sd_journal_get_data(j, "TID", (const void **)&tid, &l);
+                       if (r < 0)
+                               continue;
+
+                       r = sd_journal_get_data(j, "MESSAGE", (const void **)&message, &l);
+                       if (r < 0)
+                               continue;
+
+                       fprintf(stdout, "%c/%s(%5d): %s", pri_table[prio_idx], log_tag+8, atoi(tid+4), message+8);
+                       fprintf(stdout, "\n");
+
+                       r = sd_journal_wait(j, (uint64_t) -1);
+                       if (r < 0) {
+                               fprintf(stderr, "Couldn't wait for journal event");
+                               break;
+                       }
+               }
+       }
+
+       sd_journal_close(j);
+
+       return 0;
+}
+
similarity index 79%
rename from src/logutil/logutil.c
rename to src/logutil/logutil_kmsg_logger.c
index 6f8a15c..0c84b53 100755 (executable)
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include <ctype.h>
+#include <fcntl.h>
 #include <stdbool.h>
-#include <stdarg.h>
-#include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <time.h>
-#include <sys/time.h>
-#include <ctype.h>
-#include <errno.h>
-#include <assert.h>
+
 #include <sys/stat.h>
-#include <arpa/inet.h>
 
 #include "dlog.h"
 
-#if DLOG_BACKEND_JOURNAL
-
-#include <syslog.h>
-#include <systemd/sd-journal.h>
-
-#elif (DLOG_BACKEND_KMSG) || (DLOG_BACKEND_LOGGER)
-
 #include <logcommon.h>
 #include <queued_entry.h>
 #include <log_file.h>
@@ -124,17 +110,16 @@ static void chooseFirst(struct log_device_t* dev, struct log_device_t** firstdev
 {
        for (*firstdev = NULL; dev != NULL; dev = dev->next) {
                if (dev->queue != NULL && (*firstdev == NULL ||
-                                       cmp(dev->queue, (*firstdev)->queue) < 0)) {
+                                       cmp(dev->queue, (*firstdev)->queue) < 0)) {
                        *firstdev = dev;
                }
        }
 }
 
-static void maybePrintStart(struct log_device_t* dev)
-{
+static void maybePrintStart(struct log_device_t* dev) {
        if (!dev->printed) {
                dev->printed = true;
-               if (g_dev_count > 1) {
+               if (g_dev_count > 1 ) {
                        char buf[1024];
                        snprintf(buf, sizeof(buf), "--------- beginning of %s\n", dev->device);
                        if (write(g_log_file.fd, buf, strlen(buf)) < 0) {
@@ -145,8 +130,7 @@ static void maybePrintStart(struct log_device_t* dev)
        }
 }
 
-static void skipNextEntry(struct log_device_t* dev)
-{
+static void skipNextEntry(struct log_device_t* dev) {
        maybePrintStart(dev);
        struct queued_entry_t* entry = pop_queued_entry(&dev->queue);
        free_queued_entry(entry);
@@ -170,7 +154,7 @@ static void read_log_lines(struct log_device_t* devices)
        int result;
        fd_set readset;
 
-       for (dev = devices; dev; dev = dev->next) {
+       for (dev=devices; dev; dev = dev->next) {
                if (dev->fd > max) {
                        max = dev->fd;
                }
@@ -180,14 +164,14 @@ static void read_log_lines(struct log_device_t* devices)
                do {
                        struct timeval timeout = { 0, 5000 /* 5ms */ }; // If we oversleep it's ok, i.e. ignore EINTR.
                        FD_ZERO(&readset);
-                       for (dev = devices; dev; dev = dev->next) {
+                       for (dev=devices; dev; dev = dev->next) {
                                FD_SET(dev->fd, &readset);
                        }
                        result = select(max + 1, &readset, NULL, NULL, sleep ? NULL : &timeout);
                } while (result == -1 && errno == EINTR);
 
                if (result >= 0) {
-                       for (dev = devices; dev; dev = dev->next) {
+                       for (dev=devices; dev; dev = dev->next) {
                                if (FD_ISSET(dev->fd, &readset)) {
                                        int ret;
                                        struct queued_entry_t *entry =
@@ -199,13 +183,13 @@ static void read_log_lines(struct log_device_t* devices)
                                                goto next;
                                        else if (ret == RQER_EAGAIN)
                                                break;
-                                       else if (ret == RQER_PARSE) {
+                                       else if(ret == RQER_PARSE) {
                                                printf("Wrong formatted message is written.\n");
                                                continue;
                                        }
                                        /* EPIPE is not an error: it signals the cyclic buffer having
                                         * made a full turn and overwritten previous data */
-                                       else if (ret == RQER_EPIPE)
+                                       else if(ret == RQER_EPIPE)
                                                continue;
 
                                        enqueue(&dev->queue, entry);
@@ -301,7 +285,7 @@ static int set_log_format(const char * formatString)
 
 static void show_help(const char *cmd)
 {
-       fprintf(stderr, "Usage: %s [options] [filterspecs]\n", cmd);
+       fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
 
        fprintf(stderr, "options include:\n"
                        "  -s              Set default filter to silent.\n"
@@ -319,7 +303,7 @@ static void show_help(const char *cmd)
                        "                  ('main' (default), 'radio', 'system')");
 
 
-       fprintf(stderr, "\nfilterspecs are a series of \n"
+       fprintf(stderr,"\nfilterspecs are a series of \n"
                        "  <tag>[:priority]\n\n"
                        "where <tag> is a log component tag (or * for all) and priority is:\n"
                        "  V    Verbose\n"
@@ -419,111 +403,7 @@ static int log_devices_add_to_tail(struct log_device_t *devices, struct log_devi
        return 0;
 }
 
-#endif
-
-#if DLOG_BACKEND_JOURNAL
-
-int main_journal(int argc, char **argv)
-{
-       int r;
-       sd_journal *j;
-
-       static const char pri_table[DLOG_PRIO_MAX] = {
-               [LOG_DEBUG] = 'D',
-               [LOG_INFO] = 'I',
-               [LOG_WARNING] = 'W',
-               [LOG_ERR] = 'E',
-               [LOG_CRIT] = 'F',
-       };
-
-       r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
-       if (r < 0) {
-               fprintf(stderr, "Failed to open journal (%d)\n", -r);
-
-               return 1;
-       }
-
-       fprintf(stderr, "read\n");
-       SD_JOURNAL_FOREACH(j) {
-               const char *priority, *log_tag, *tid,  *message;
-               size_t l;
-               int prio_idx;
-
-               r = sd_journal_get_data(j, "PRIORITY", (const void **)&priority, &l);
-               if (r < 0)
-                       continue;
-
-               prio_idx = atoi(priority+9);
-               if (prio_idx < LOG_CRIT || prio_idx > LOG_DEBUG) {
-                       fprintf(stdout, "Wrong priority");
-                       continue;
-               }
-
-               r = sd_journal_get_data(j, "LOG_TAG", (const void **)&log_tag, &l);
-               if (r < 0)
-                       continue;
-
-               r = sd_journal_get_data(j, "TID", (const void **)&tid, &l);
-               if (r < 0)
-                       continue;
-
-               r = sd_journal_get_data(j, "MESSAGE", (const void **)&message, &l);
-               if (r < 0)
-                       continue;
-
-               fprintf(stdout, "%c/%s(%5d): %s\n", pri_table[prio_idx], log_tag+8, atoi(tid+4), message+8);
-       }
-
-       fprintf(stderr, "wait\n");
-       for (;;) {
-               const char *log_tag, *priority, *tid, *message;
-               size_t l;
-               int prio_idx;
-
-               if (sd_journal_seek_tail(j) < 0) {
-                       fprintf(stderr, "Couldn't find journal");
-               } else if (sd_journal_previous(j) > 0) {
-                       r = sd_journal_get_data(j, "PRIORITY", (const void **)&priority, &l);
-                       if (r < 0)
-                               continue;
-
-                       prio_idx = atoi(priority+9);
-                       if (prio_idx < LOG_CRIT || prio_idx > LOG_DEBUG) {
-                               fprintf(stdout, "Wrong priority");
-                               continue;
-                       }
-
-                       r = sd_journal_get_data(j, "LOG_TAG", (const void **)&log_tag, &l);
-                       if (r < 0)
-                               continue;
-
-                       r = sd_journal_get_data(j, "TID", (const void **)&tid, &l);
-                       if (r < 0)
-                               continue;
-
-                       r = sd_journal_get_data(j, "MESSAGE", (const void **)&message, &l);
-                       if (r < 0)
-                               continue;
-
-                       fprintf(stdout, "%c/%s(%5d): %s", pri_table[prio_idx], log_tag+8, atoi(tid+4), message+8);
-                       fprintf(stdout, "\n");
-
-                       r = sd_journal_wait(j, (uint64_t) -1);
-                       if (r < 0) {
-                               fprintf(stderr, "Couldn't wait for journal event");
-                               break;
-                       }
-               }
-       }
-
-       sd_journal_close(j);
-
-       return 0;
-}
-
-#elif (DLOG_BACKEND_KMSG) || (DLOG_BACKEND_LOGGER)
-
-int main_others(int argc, char **argv)
+int main (int argc, char **argv)
 {
        int err;
        int has_set_log_format = 0;
@@ -589,7 +469,7 @@ int main_others(int argc, char **argv)
 
                case 'b': {
                                          id = log_id_by_name(optarg);
-                                         if (id == LOG_ID_INVALID) {
+                                         if (id <= LOG_ID_INVALID) {
                                                  _E("Unknown log buffer %s\n", optarg);
                                                  exit(-1);
                                          }
@@ -723,7 +603,7 @@ int main_others(int argc, char **argv)
        while (dev) {
                dev->fd = open(dev->device, mode);
                if (dev->fd < 0) {
-                       _E("Unable to open log device %s (%d)",
+                       _E("Unable to open log device '%s': errno %d\n",
                                        dev->device, errno);
                        exit(EXIT_FAILURE);
                }
@@ -738,7 +618,8 @@ int main_others(int argc, char **argv)
                        off_t off;
                        off = lseek(dev->fd, 0, SEEK_DATA);
                        if (off == -1) {
-                               _E("Unable to lseek device %s. (%d)\n", dev->device, errno);
+                               _E("Unable to lseek device %s, errno %d\n",
+                                               dev->device, errno);
                                exit(EXIT_FAILURE);
                        }
                }
@@ -772,16 +653,3 @@ int main_others(int argc, char **argv)
        return 0;
 }
 
-#endif
-
-int main(int argc, char **argv)
-{
-#if DLOG_BACKEND_JOURNAL
-       return main_journal(argc, argv);
-#elif (DLOG_BACKEND_KMSG) || (DLOG_BACKEND_LOGGER)
-       return main_others(argc, argv);
-#else
-       printf("Backend isn't set at the build time.\n");
-       return 1;
-#endif
-}