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.
24 static FILE *log_fp = NULL;
25 static log_type ltype = LOG_TYPE_DLOG;
27 static const char log_prio_name[][DLOG_PRIO_MAX-1] = {
29 "DEFAULT", /**< Default */
30 "VERBOSE", /**< Verbose */
31 "DEBUG", /**< Debug */
33 "WARN", /**< Warning */
34 "ERROR", /**< Error */
35 "FATAL", /**< Fatal */
36 "SILENT" /**< Silent */
39 static inline char* getFormattedTime(void)
41 struct timespec time_s;
43 static char res_time[40] = {0, };
45 if (0 == clock_gettime(CLOCK_REALTIME, &time_s)) {
46 ptm = localtime((const time_t *)&time_s.tv_sec);
48 // format : YY-MM-DD hh:mm:ss:uuuuuu
49 snprintf(res_time, sizeof(res_time),
50 "%04d-%02d-%02d %02d:%02d:%02d:%06ld"
51 , ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday
52 , ptm->tm_hour, ptm->tm_min, ptm->tm_sec
59 static int __open_log_file(void)
61 log_fp = fopen("/var/log/ttd.log", "a");
62 /* TODO : splits log file, if log file size is exceeded specified max size */
64 dlog_print(DLOG_WARN, LOG_TAG, "%s\n", strerror(errno));
71 dlog_print(DLOG_WARN, LOG_TAG,
72 "error to use log file, so use dlog as log system\n");
73 ltype = LOG_TYPE_DLOG;
78 int log_type_set(log_type type)
81 dlog_print(DLOG_DEBUG, LOG_TAG, "setting log type : [%d]\n", type);
87 case LOG_TYPE_DLOG: /* nothing to do */
89 ltype = LOG_TYPE_DLOG;
95 void log_file_close(void)
100 dlog_print(DLOG_DEBUG, LOG_TAG, "close log file\n");
103 log_type_set(LOG_TYPE_DLOG);
108 int log_print(log_priority prio, const char *tag, const char *fmt, ...)
116 fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]);
117 vfprintf(log_fp, fmt, ap);
125 fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]);
126 vfprintf(log_fp, fmt, ap);
128 dlog_vprint(prio, tag, fmt, ap);
137 dlog_vprint(prio, tag, fmt, ap);