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 && prio<g_debug_level)
63 if( log_id < LOG_ID_MAX )
64 log_fd = log_fds[log_id];
71 vec[0].iov_base = (unsigned char *) &prio;
73 vec[1].iov_base = (void *) tag;
74 vec[1].iov_len = strlen(tag) + 1;
75 vec[2].iov_base = (void *) msg;
76 vec[2].iov_len = strlen(msg) + 1;
78 ret = writev(log_fd, vec, 3);
83 void init_debug_level(void)
85 char *debuglevel=getenv("TIZEN_DEBUG_LEVEL");
88 fprintf(stderr, "Not matched env. variable, TIZEN_DEBUG_LEVEL");
92 g_debug_level=atoi(debuglevel);
94 fprintf(stderr, "debug level init %d(%s) \n",g_debug_level,debuglevel);
97 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
100 pthread_mutex_lock(&log_init_lock);
102 // get filtering info
105 if( write_to_log == __dlog_init)
107 log_fds[LOG_ID_MAIN] = open("/dev/"LOG_MAIN, O_WRONLY);
108 log_fds[LOG_ID_RADIO] = open("/dev/"LOG_RADIO, O_WRONLY);
109 log_fds[LOG_ID_SYSTEM] = open("/dev/"LOG_SYSTEM, O_WRONLY);
110 log_fds[LOG_ID_APPS] = open("/dev/"LOG_APPS, O_WRONLY);
114 if( log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 )
116 fprintf(stderr, "open log dev is failed\n");
117 write_to_log = __write_to_log_null;
120 write_to_log = __write_to_log_kernel;
122 if( log_fds[LOG_ID_SYSTEM] < 0 )
124 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
127 if( log_fds[LOG_ID_APPS] < 0 )
129 log_fds[LOG_ID_APPS] = log_fds[LOG_ID_MAIN];
133 pthread_mutex_unlock(&log_init_lock);
135 return write_to_log(log_id, prio, tag, msg);
138 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
140 char buf[LOG_BUF_SIZE];
142 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
144 return write_to_log(log_id, prio, tag, buf);
147 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
150 char buf[LOG_BUF_SIZE];
153 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
156 return write_to_log(log_id, prio, tag, buf);