Add log files
[apps/native/ttsd-worker-package.git] / src / log.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <dlog.h>
22 #include "log.h"
23
24 static FILE *log_fp = NULL;
25 static log_type ltype = LOG_TYPE_DLOG;
26
27 static const char log_prio_name[][DLOG_PRIO_MAX-1] = {
28         "UNKNOWN",
29         "DEFAULT", /**< Default */
30         "VERBOSE", /**< Verbose */
31         "DEBUG", /**< Debug */
32         "INFO", /**< Info */
33         "WARN", /**< Warning */
34         "ERROR", /**< Error */
35         "FATAL", /**< Fatal */
36         "SILENT" /**< Silent */
37 };
38
39 static inline char* getFormattedTime(void)
40 {
41         struct timespec time_s;
42         struct tm *ptm;
43         static char res_time[40] = {0, };
44
45         if (0 == clock_gettime(CLOCK_REALTIME, &time_s)) {
46                 ptm = localtime((const time_t *)&time_s.tv_sec);
47
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
53                         , time_s.tv_nsec);
54         }
55
56         return res_time;
57 }
58
59 static int __open_log_file(void)
60 {
61         log_fp = fopen("/var/log/ttd.log", "a");
62         /* TODO : splits log file, if log file size is exceeded specified max size */
63         if (log_fp == NULL) {
64                 dlog_print(DLOG_WARN, LOG_TAG, "%s\n", strerror(errno));
65                 goto error;
66         }
67
68         return 0;
69
70 error:
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;
74
75         return -1;
76 }
77
78 int log_type_set(log_type type)
79 {
80         ltype = type;
81         dlog_print(DLOG_DEBUG, LOG_TAG, "setting log type : [%d]\n", type);
82         switch (type) {
83         case LOG_TYPE_FILE:
84         case LOG_TYPE_ALL:
85                 __open_log_file();
86                 break;
87         case LOG_TYPE_DLOG: /* nothing to do */
88         default:
89                 ltype = LOG_TYPE_DLOG;
90                 break;
91         }
92         return 0;
93 }
94
95 void log_file_close(void)
96 {
97         if (log_fp) {
98                 fclose(log_fp);
99                 log_fp = NULL;
100                 dlog_print(DLOG_DEBUG, LOG_TAG, "close log file\n");
101         }
102
103         log_type_set(LOG_TYPE_DLOG);
104
105         return;
106 }
107
108 int log_print(log_priority prio, const char *tag, const char *fmt, ...)
109 {
110         va_list ap;
111
112         switch (ltype) {
113         case LOG_TYPE_FILE:
114                 if (log_fp) {
115                         va_start(ap, fmt);
116                         fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]);
117                         vfprintf(log_fp, fmt, ap);
118                         va_end(ap);
119                         fflush(log_fp);
120                 }
121                 break;
122         case LOG_TYPE_ALL:
123                 va_start(ap, fmt);
124                 if (log_fp) {
125                         fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]);
126                         vfprintf(log_fp, fmt, ap);
127                 }
128                 dlog_vprint(prio, tag, fmt, ap);
129                 va_end(ap);
130
131                 if (log_fp)
132                         fflush(log_fp);
133                 break;
134         case LOG_TYPE_DLOG:
135         default:
136                 va_start(ap, fmt);
137                 dlog_vprint(prio, tag, fmt, ap);
138                 va_end(ap);
139                 break;
140         }
141         return 0;
142 }