2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #ifdef SD_JOURNAL_SUPPORT
26 #include <systemd/sd-journal.h>
28 #define LOG_BUF_SIZE 1024
30 #define LOG_MAIN "log_main"
31 #define LOG_RADIO "log_radio"
32 #define LOG_SYSTEM "log_system"
33 #define LOG_APPS "log_apps"
35 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
37 static int g_logging_on = 1;
38 static int g_dlog_level = DLOG_SILENT;
40 static int __dlog_init(log_id_t, log_priority, const char *tag, const char *msg);
41 static int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg) = __dlog_init;
42 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
43 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
47 #ifdef SD_JOURNAL_SUPPORT
48 static int dlog_pri_to_journal_pri(log_priority prio)
50 int journal_prio = LOG_DEBUG;
56 journal_prio = LOG_DEBUG;
59 journal_prio = LOG_DEBUG;
62 journal_prio = LOG_INFO;
65 journal_prio = LOG_WARNING;
68 journal_prio = LOG_ERR;
71 journal_prio = LOG_CRIT;
80 static int __write_to_log_sd_journal_print(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
82 sd_journal_print(dlog_pri_to_journal_pri(prio), "%s %s", tag, msg);
86 static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
92 if (log_id < LOG_ID_APPS) {
93 if(prio < g_dlog_level) {
96 } else if (LOG_ID_MAX <= log_id) {
99 if (log_id < LOG_ID_MAX)
100 log_fd = log_fds[log_id];
107 vec[0].iov_base = (unsigned char *) &prio;
109 vec[1].iov_base = (void *) tag;
110 vec[1].iov_len = strlen(tag) + 1;
111 vec[2].iov_base = (void *) msg;
112 vec[2].iov_len = strlen(msg) + 1;
114 ret = writev(log_fd, vec, 3);
119 void init_dlog_level(void)
121 char *dlog_level_env;
122 char *logging_mode_env;
124 logging_mode_env = getenv("TIZEN_PLATFORMLOGGING_MODE");
125 if (!logging_mode_env)
128 g_logging_on = atoi(logging_mode_env);
131 dlog_level_env = getenv("TIZEN_DLOG_LEVEL");
132 if (!dlog_level_env) {
135 g_dlog_level = atoi(dlog_level_env);
140 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
142 pthread_mutex_lock(&log_init_lock);
143 #ifdef SD_JOURNAL_SUPPORT
144 write_to_log = __write_to_log_sd_journal_print;
146 // get filtering info
148 if (write_to_log == __dlog_init) {
151 log_fds[LOG_ID_MAIN] = open("/dev/"LOG_MAIN, O_WRONLY);
152 log_fds[LOG_ID_RADIO] = open("/dev/"LOG_RADIO, O_WRONLY);
153 log_fds[LOG_ID_SYSTEM] = open("/dev/"LOG_SYSTEM, O_WRONLY);
154 log_fds[LOG_ID_APPS] = open("/dev/"LOG_APPS, O_WRONLY);
156 if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0) {
157 fprintf(stderr, "open log dev is failed\n");
158 write_to_log = __write_to_log_null;
160 write_to_log = __write_to_log_kernel;
162 if (log_fds[LOG_ID_SYSTEM] < 0)
163 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
165 if (log_fds[LOG_ID_APPS] < 0)
166 log_fds[LOG_ID_APPS] = log_fds[LOG_ID_MAIN];
169 pthread_mutex_unlock(&log_init_lock);
170 return write_to_log(log_id, prio, tag, msg);
173 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
175 char buf[LOG_BUF_SIZE];
177 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
179 return write_to_log(log_id, prio, tag, buf);
182 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
185 char buf[LOG_BUF_SIZE];
188 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
191 return write_to_log(log_id, prio, tag, buf);
193 int _get_logging_on(void)