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
27 #include <systemd/sd-journal.h>
29 #define LOG_BUF_SIZE 1024
31 #define LOG_MAIN "log_main"
32 #define LOG_RADIO "log_radio"
33 #define LOG_SYSTEM "log_system"
34 #define LOG_APPS "log_apps"
36 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
38 static int g_logging_on = 1;
39 static int g_dlog_level = DLOG_SILENT;
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)
48 #ifdef SD_JOURNAL_SUPPORT
49 static int dlog_pri_to_journal_pri(log_priority prio)
51 int journal_prio = LOG_DEBUG;
57 journal_prio = LOG_DEBUG;
60 journal_prio = LOG_DEBUG;
63 journal_prio = LOG_INFO;
66 journal_prio = LOG_WARNING;
69 journal_prio = LOG_ERR;
72 journal_prio = LOG_CRIT;
81 static int __write_to_log_sd_journal_print(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
83 sd_journal_print(dlog_pri_to_journal_pri(prio), "%s %s", tag, msg);
87 static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
93 if (log_id < LOG_ID_APPS) {
94 if(prio < g_dlog_level) {
97 } else if (LOG_ID_MAX <= log_id) {
100 if (log_id < LOG_ID_MAX)
101 log_fd = log_fds[log_id];
108 vec[0].iov_base = (unsigned char *) &prio;
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;
115 ret = writev(log_fd, vec, 3);
120 void init_dlog_level(void)
122 char *dlog_level_env;
123 char *logging_mode_env;
125 logging_mode_env = getenv("TIZEN_PLATFORMLOGGING_MODE");
126 if (!logging_mode_env)
129 g_logging_on = atoi(logging_mode_env);
132 dlog_level_env = getenv("TIZEN_DLOG_LEVEL");
133 if (!dlog_level_env) {
136 g_dlog_level = atoi(dlog_level_env);
141 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
143 pthread_mutex_lock(&log_init_lock);
144 #ifdef SD_JOURNAL_SUPPORT
145 write_to_log = __write_to_log_sd_journal_print;
147 // get filtering info
149 if (write_to_log == __dlog_init) {
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);
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;
161 write_to_log = __write_to_log_kernel;
163 if (log_fds[LOG_ID_SYSTEM] < 0)
164 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
166 if (log_fds[LOG_ID_APPS] < 0)
167 log_fds[LOG_ID_APPS] = log_fds[LOG_ID_MAIN];
170 pthread_mutex_unlock(&log_init_lock);
171 return write_to_log(log_id, prio, tag, msg);
174 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
176 char buf[LOG_BUF_SIZE];
178 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
180 return write_to_log(log_id, prio, tag, buf);
183 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
186 char buf[LOG_BUF_SIZE];
189 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
192 return write_to_log(log_id, prio, tag, buf);
194 int _get_logging_on(void)