adds log module 12/176412/1
authorJeonghoon Park <jh1979.park@samsung.com>
Thu, 19 Apr 2018 06:21:42 +0000 (15:21 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Thu, 19 Apr 2018 06:21:42 +0000 (15:21 +0900)
Change-Id: Ia6afddc5d94a318fe876b88d959b2b19aebd8282

daemon/include/ttd-log.h [new file with mode: 0644]
daemon/src/ttd-log.c [new file with mode: 0644]

diff --git a/daemon/include/ttd-log.h b/daemon/include/ttd-log.h
new file mode 100644 (file)
index 0000000..f0815da
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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.
+ */
+
+#ifndef __TT_DAEMON_LOG_H__
+#define __TT_DAEMON_LOG_H__
+
+#include <dlog.h>
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "TTD"
+
+#if !defined(_V)
+#define _V(fmt, arg...) log_print(DLOG_VERBOSE, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#if !defined(_D)
+#define _D(fmt, arg...) log_print(DLOG_DEBUG, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#if !defined(_I)
+#define _I(fmt, arg...) log_print(DLOG_INFO, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#if !defined(_W)
+#define _W(fmt, arg...) log_print(DLOG_WARN, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#if !defined(_E)
+#define _E(fmt, arg...) log_print(DLOG_ERROR, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#define retvm_if(expr, val, fmt, arg...) do { \
+       if (expr) { \
+               _E(fmt, ##arg); \
+               _E("(%s) -> %s() return", #expr, __FUNCTION__); \
+               return val; \
+       } \
+} while (0)
+
+#define retv_if(expr, val) do { \
+       if (expr) { \
+               _E("(%s) -> %s() return", #expr, __FUNCTION__); \
+               return (val); \
+       } \
+} while (0)
+
+#define retm_if(expr, fmt, arg...) do { \
+       if (expr) { \
+               _E(fmt, ##arg); \
+               _E("(%s) -> %s() return", #expr, __FUNCTION__); \
+               return; \
+       } \
+} while (0)
+
+#define ret_if(expr) do { \
+       if (expr) { \
+               _E("(%s) -> %s() return", #expr, __FUNCTION__); \
+               return; \
+       } \
+} while (0)
+
+#define goto_if(expr, val) do { \
+       if (expr) { \
+               _E("(%s) -> goto", #expr); \
+               goto val; \
+       } \
+} while (0)
+
+#define break_if(expr) { \
+       if (expr) { \
+               _E("(%s) -> break", #expr); \
+               break; \
+       } \
+}
+
+#define continue_if(expr) { \
+       if (expr) { \
+               _E("(%s) -> continue", #expr); \
+               continue; \
+       } \
+}
+
+typedef enum {
+       LOG_TYPE_DLOG = 0,
+       LOG_TYPE_FILE,
+       LOG_TYPE_ALL,
+} log_type;
+
+int log_print(log_priority prio, const char *tag, const char *fmt, ...);
+int log_type_set(log_type type);
+void log_file_close(void);
+
+#endif /* __TT_DAEMON_LOG_H__ */
diff --git a/daemon/src/ttd-log.c b/daemon/src/ttd-log.c
new file mode 100644 (file)
index 0000000..2652e7c
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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 <stdarg.h>
+#include <stdlib.h>
+#include <time.h>
+#include <dlog.h>
+#include <tzplatform_config.h>
+#include "ttd-log.h"
+
+static FILE *log_fp = NULL;
+static log_type ltype = LOG_TYPE_DLOG;
+
+static const char log_prio_name[][DLOG_PRIO_MAX-1] = {
+       "UNKNOWN",
+       "DEFAULT", /**< Default */
+       "VERBOSE", /**< Verbose */
+       "DEBUG", /**< Debug */
+       "INFO", /**< Info */
+       "WARN", /**< Warning */
+       "ERROR", /**< Error */
+       "FATAL", /**< Fatal */
+       "SILENT" /**< Silent */
+};
+
+static inline char* getFormattedTime(void)
+{
+       struct timespec time_s;
+       struct tm *ptm;
+       static char res_time[40] = {0, };
+
+       if (0 == clock_gettime(CLOCK_REALTIME, &time_s)) {
+               ptm = localtime((const time_t *)&time_s.tv_sec);
+
+               // format : YY-MM-DD hh:mm:ss:uuuuuu
+               snprintf(res_time, sizeof(res_time),
+                       "%04d-%02d-%02d %02d:%02d:%02d:%06ld"
+                       , ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday
+                       , ptm->tm_hour, ptm->tm_min, ptm->tm_sec
+                       , time_s.tv_nsec);
+       }
+
+       return res_time;
+}
+
+static int __open_log_file(void)
+{
+       const char *log_file = tzplatform_mkpath(TZ_SYS_VAR, "log/ttd.log");
+       _D("log file - %s", log_file);
+       log_fp = fopen(log_file, "a");
+       /* TODO : splits log file, if log file size is exceeded specified max size */
+       if (log_fp == NULL) {
+               dlog_print(DLOG_WARN, LOG_TAG, "%s\n", strerror(errno));
+               goto error;
+       }
+
+       return 0;
+
+error:
+       dlog_print(DLOG_WARN, LOG_TAG,
+               "error to use log file, so use dlog as log system\n");
+       ltype = LOG_TYPE_DLOG;
+
+       return -1;
+}
+
+int log_type_set(log_type type)
+{
+       ltype = type;
+       dlog_print(DLOG_DEBUG, LOG_TAG, "setting log type : [%d]\n", type);
+       switch (type) {
+       case LOG_TYPE_FILE:
+       case LOG_TYPE_ALL:
+               __open_log_file();
+               break;
+       case LOG_TYPE_DLOG: /* nothing to do */
+       default:
+               ltype = LOG_TYPE_DLOG;
+               break;
+       }
+       return 0;
+}
+
+void log_file_close(void)
+{
+       if (log_fp) {
+               fclose(log_fp);
+               log_fp = NULL;
+               dlog_print(DLOG_DEBUG, LOG_TAG, "close log file\n");
+       }
+
+       log_type_set(LOG_TYPE_DLOG);
+
+       return;
+}
+
+int log_print(log_priority prio, const char *tag, const char *fmt, ...)
+{
+       va_list ap;
+
+       switch (ltype) {
+       case LOG_TYPE_FILE:
+               if (log_fp) {
+                       va_start(ap, fmt);
+                       fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]);
+                       vfprintf(log_fp, fmt, ap);
+                       va_end(ap);
+                       fflush(log_fp);
+               }
+               break;
+       case LOG_TYPE_ALL:
+               va_start(ap, fmt);
+               if (log_fp) {
+                       fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]);
+                       vfprintf(log_fp, fmt, ap);
+               }
+               dlog_vprint(prio, tag, fmt, ap);
+               va_end(ap);
+
+               if (log_fp)
+                       fflush(log_fp);
+               break;
+       case LOG_TYPE_DLOG:
+       default:
+               va_start(ap, fmt);
+               dlog_vprint(prio, tag, fmt, ap);
+               va_end(ap);
+               break;
+       }
+       return 0;
+}