added syslog.h in dlog.c and dlogutil bug fix
[framework/system/dlog.git] / log.c
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include <pthread.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include <fcntl.h>
21 #include <sys/uio.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <dlog.h>
25 #ifdef SD_JOURNAL_SUPPORT
26 #include <syslog.h>
27 #include <systemd/sd-journal.h>
28 #endif
29 #define LOG_BUF_SIZE    1024
30
31 #define LOG_MAIN        "log_main"
32 #define LOG_RADIO       "log_radio"
33 #define LOG_SYSTEM      "log_system"
34 #define LOG_APPS        "log_apps"
35
36 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
37
38 static int g_logging_on = 1;
39 static int g_dlog_level = DLOG_SILENT;
40
41 static int __dlog_init(log_id_t, log_priority, const char *tag, const char *msg);
42 static int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg) = __dlog_init;
43 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
44 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
45 {
46         return -1;
47 }
48 #ifdef SD_JOURNAL_SUPPORT
49 static int dlog_pri_to_journal_pri(log_priority prio)
50 {
51         int journal_prio = LOG_DEBUG;
52
53         switch(prio) {
54         case DLOG_UNKNOWN:
55         case DLOG_DEFAULT:
56         case DLOG_VERBOSE:
57                 journal_prio = LOG_DEBUG;
58                 break;
59         case DLOG_DEBUG:
60                 journal_prio = LOG_DEBUG;
61                 break;
62         case DLOG_INFO:
63                 journal_prio = LOG_INFO;
64                 break;
65         case DLOG_WARN:
66                 journal_prio = LOG_WARNING;
67                 break;
68         case DLOG_ERROR:
69                 journal_prio = LOG_ERR;
70                 break;
71         case DLOG_FATAL:
72                 journal_prio = LOG_CRIT;
73                 break;
74         case DLOG_SILENT:
75         default:
76                 journal_prio = -1;
77                 break;
78         }
79         return journal_prio;
80 }
81 static int __write_to_log_sd_journal_print(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
82 {
83         sd_journal_print(dlog_pri_to_journal_pri(prio), "%s %s", tag, msg);
84         return 1;
85 }
86 #else
87 static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
88 {
89         ssize_t ret;
90         int log_fd;
91         struct iovec vec[3];
92
93         if (log_id < LOG_ID_APPS) {
94                 if(prio < g_dlog_level) {
95                         return 0;
96                 }
97         } else if (LOG_ID_MAX <= log_id) {
98                 return 0;
99         }
100         if (log_id < LOG_ID_MAX)
101                 log_fd = log_fds[log_id];
102         else
103                 return -1; // for TC
104
105         if (!tag)
106                 tag = "";
107
108         vec[0].iov_base = (unsigned char *) &prio;
109         vec[0].iov_len  = 1;
110         vec[1].iov_base = (void *) tag;
111         vec[1].iov_len  = strlen(tag) + 1;
112         vec[2].iov_base = (void *) msg;
113         vec[2].iov_len  = strlen(msg) + 1;
114
115         ret = writev(log_fd, vec, 3);
116
117         return ret;
118 }
119 #endif
120 void init_dlog_level(void)
121 {
122         char *dlog_level_env;
123         char *logging_mode_env;
124         if (g_logging_on) {
125                 logging_mode_env = getenv("TIZEN_PLATFORMLOGGING_MODE");
126                 if (!logging_mode_env)
127                                 g_logging_on = 0;
128                         else
129                                 g_logging_on = atoi(logging_mode_env);
130         }
131         if (g_logging_on) {
132                 dlog_level_env = getenv("TIZEN_DLOG_LEVEL");
133                 if (!dlog_level_env) {
134                         g_dlog_level = 8;
135                 } else {
136                         g_dlog_level = atoi(dlog_level_env);
137                 }
138         } else
139                 g_dlog_level = 8;
140 }
141 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
142 {
143         pthread_mutex_lock(&log_init_lock);
144 #ifdef SD_JOURNAL_SUPPORT
145         write_to_log = __write_to_log_sd_journal_print;
146 #else
147         // get filtering info
148         // open device
149         if (write_to_log == __dlog_init) {
150                 init_dlog_level();
151
152                 log_fds[LOG_ID_MAIN] = open("/dev/"LOG_MAIN, O_WRONLY);
153                 log_fds[LOG_ID_RADIO] = open("/dev/"LOG_RADIO, O_WRONLY);
154                 log_fds[LOG_ID_SYSTEM] = open("/dev/"LOG_SYSTEM, O_WRONLY);
155                 log_fds[LOG_ID_APPS] = open("/dev/"LOG_APPS, O_WRONLY);
156
157                 if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0) {
158                         fprintf(stderr, "open log dev is failed\n");
159                         write_to_log = __write_to_log_null;
160                 } else
161                         write_to_log = __write_to_log_kernel;
162
163                 if (log_fds[LOG_ID_SYSTEM] < 0)
164                         log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
165
166                 if (log_fds[LOG_ID_APPS] < 0)
167                         log_fds[LOG_ID_APPS] = log_fds[LOG_ID_MAIN];
168         }
169 #endif
170         pthread_mutex_unlock(&log_init_lock);
171         return write_to_log(log_id, prio, tag, msg);
172 }
173
174 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
175 {
176         char buf[LOG_BUF_SIZE];
177
178         vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
179
180         return write_to_log(log_id, prio, tag, buf);
181 }
182
183 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
184 {
185         va_list ap;
186         char buf[LOG_BUF_SIZE];
187
188         va_start(ap, fmt);
189         vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
190         va_end(ap);
191
192         return write_to_log(log_id, prio, tag, buf);
193 }
194 int _get_logging_on(void)
195 {
196         return g_logging_on;
197 }