2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/syscall.h>
25 #include <tzplatform_config.h>
27 #include "common-util.h"
31 * [time][log_level][pid-tid] message([func : line] log)
34 static FILE *log_fp = NULL;
35 static GMutex log_mutex;
36 static ttd_log_type_e ltype = TTD_LOG_TYPE_DLOG;
38 static const char log_prio_name[][TTD_LOG_LEVEL_MAX] = {
39 "U", /**< Unknown - DO NOT use */
47 static inline long int __get_tid(void)
49 return (long int)syscall(__NR_gettid);
52 static log_priority __level_to_dlog_prio(ttd_log_level_e level)
54 log_priority prio = DLOG_UNKNOWN;
72 case TTD_LOG_LEVEL_MAX:
79 static const char* getFormattedTime(void)
81 struct timespec time_s;
83 static char res_time[40] = {0, };
88 if (0 == common_get_epoch_coarse_timespec(&time_s)) {
89 ptm = localtime((const time_t *)&time_s.tv_sec);
91 // format : YY-MM-DD hh:mm:ss:uuuuuu
92 g_snprintf(res_time, sizeof(res_time),
93 "%04d-%02d-%02d %02d:%02d:%02d:%06ld"
94 , ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday
95 , ptm->tm_hour, ptm->tm_min, ptm->tm_sec
96 , (long int)(time_s.tv_nsec / 1000));
97 } /* if failed, return last time */
102 static int __open_log_file(void)
104 const char *log_file = tzplatform_mkpath(TZ_SYS_VAR, "log/ttd.log");
105 dlog_print(DLOG_DEBUG, LOG_TAG, "log file - %s\n", log_file);
107 /* TODO : splits log file, if log file size is exceeded specified max size */
108 g_mutex_lock(&log_mutex);
109 log_fp = fopen(log_file, "a");
111 g_mutex_unlock(&log_mutex);
112 dlog_print(DLOG_ERROR,
113 LOG_TAG, "error[%s] to use log file, so use dlog as log system\n",
116 ltype = TTD_LOG_TYPE_DLOG;
119 setvbuf(log_fp, (char *)NULL, _IOLBF, 0);
120 g_mutex_unlock(&log_mutex);
124 static void __close_log_file(void)
126 g_mutex_lock(&log_mutex);
131 g_mutex_unlock(&log_mutex);
133 dlog_print(DLOG_DEBUG, LOG_TAG, "close log file\n");
137 __write_log_to_file(const char *msg, ttd_log_level_e level, const char *tag)
139 /* TODO : we need to logging tag ??? */
141 char *log_msg = NULL;
146 log_msg = g_strdup_printf("[%s][%s][%ld-%ld] %s",
148 log_prio_name[level],
149 (long int)getpid(), __get_tid(),
153 dlog_print(DLOG_ERROR, LOG_TAG, "failed to create log msg\n");
154 dlog_print(DLOG_ERROR, LOG_TAG, "%s", msg);
157 g_mutex_lock(&log_mutex);
159 fprintf(log_fp, "%s", log_msg);
160 g_mutex_unlock(&log_mutex);
164 int ttd_log_init(ttd_log_type_e type)
167 g_mutex_init(&log_mutex);
168 dlog_print(DLOG_DEBUG, LOG_TAG, "initializing log type : [%d]\n", type);
170 case TTD_LOG_TYPE_FILE:
171 case TTD_LOG_TYPE_ALL:
174 case TTD_LOG_TYPE_DLOG: /* nothing to do */
176 ltype = TTD_LOG_TYPE_DLOG;
182 void ttd_log_fini(void)
185 g_mutex_clear(&log_mutex);
186 ltype = TTD_LOG_TYPE_DLOG;
191 int ttd_log_print(ttd_log_level_e level, const char *tag, const char *fmt, ...)
196 case TTD_LOG_TYPE_FILE:
201 msg = g_strdup_vprintf(fmt, ap);
204 __write_log_to_file(msg, level, tag);
208 case TTD_LOG_TYPE_ALL:
213 msg = g_strdup_vprintf(fmt, ap);
216 dlog_print(__level_to_dlog_prio(level), tag, "%s", msg);
217 __write_log_to_file(msg, level, tag);
221 case TTD_LOG_TYPE_DLOG:
224 dlog_vprint(__level_to_dlog_prio(level), tag, fmt, ap);