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.
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"
37 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
39 static int g_debug_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;
44 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
48 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
53 static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
59 if (log_id < LOG_ID_APPS) {
60 if(prio<g_debug_level) {
63 } else if (LOG_ID_MAX <= log_id) {
66 if( log_id < LOG_ID_MAX )
67 log_fd = log_fds[log_id];
74 vec[0].iov_base = (unsigned char *) &prio;
76 vec[1].iov_base = (void *) tag;
77 vec[1].iov_len = strlen(tag) + 1;
78 vec[2].iov_base = (void *) msg;
79 vec[2].iov_len = strlen(msg) + 1;
81 ret = writev(log_fd, vec, 3);
86 void init_debug_level(void)
88 char *debuglevel=getenv("TIZEN_DEBUG_LEVEL");
91 fprintf(stderr, "Not matched env. variable, TIZEN_DEBUG_LEVEL");
95 g_debug_level=atoi(debuglevel);
97 fprintf(stderr, "debug level init %d(%s) \n",g_debug_level,debuglevel);
100 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
103 pthread_mutex_lock(&log_init_lock);
105 // get filtering info
108 if( write_to_log == __dlog_init)
110 log_fds[LOG_ID_MAIN] = open("/dev/"LOG_MAIN, O_WRONLY);
111 log_fds[LOG_ID_RADIO] = open("/dev/"LOG_RADIO, O_WRONLY);
112 log_fds[LOG_ID_SYSTEM] = open("/dev/"LOG_SYSTEM, O_WRONLY);
113 log_fds[LOG_ID_APPS] = open("/dev/"LOG_APPS, O_WRONLY);
117 if( log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 )
119 fprintf(stderr, "open log dev is failed\n");
120 write_to_log = __write_to_log_null;
123 write_to_log = __write_to_log_kernel;
125 if( log_fds[LOG_ID_SYSTEM] < 0 )
127 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
130 if( log_fds[LOG_ID_APPS] < 0 )
132 log_fds[LOG_ID_APPS] = log_fds[LOG_ID_MAIN];
136 pthread_mutex_unlock(&log_init_lock);
138 return write_to_log(log_id, prio, tag, msg);
141 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
143 char buf[LOG_BUF_SIZE];
145 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
147 return write_to_log(log_id, prio, tag, buf);
150 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
153 char buf[LOG_BUF_SIZE];
156 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
159 return write_to_log(log_id, prio, tag, buf);