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.
28 #define LOG_BUF_SIZE 1024
30 #define LOG_MAIN "log_main"
31 #define LOG_RADIO "log_radio"
32 #define LOG_SYSTEM "log_system"
35 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1 };
37 static int default_log_prio= DLOG_DEFAULT;
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;
43 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
47 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
52 static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
58 if( log_id < LOG_ID_MAX )
59 log_fd = log_fds[log_id];
66 vec[0].iov_base = (unsigned char *) &prio;
68 vec[1].iov_base = (void *) tag;
69 vec[1].iov_len = strlen(tag) + 1;
70 vec[2].iov_base = (void *) msg;
71 vec[2].iov_len = strlen(msg) + 1;
73 ret = writev(log_fd, vec, 3);
79 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
82 pthread_mutex_lock(&log_init_lock);
87 if( write_to_log == __dlog_init)
89 log_fds[LOG_ID_MAIN] = open("/dev/"LOG_MAIN, O_WRONLY);
90 log_fds[LOG_ID_RADIO] = open("/dev/"LOG_RADIO, O_WRONLY);
91 log_fds[LOG_ID_SYSTEM] = open("/dev/"LOG_SYSTEM, O_WRONLY);
93 if( log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 )
95 fprintf(stderr, "open log dev is failed\n");
96 write_to_log = __write_to_log_null;
99 write_to_log = __write_to_log_kernel;
101 if( log_fds[LOG_ID_SYSTEM] < 0 )
103 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
107 pthread_mutex_unlock(&log_init_lock);
109 return write_to_log(log_id, prio, tag, msg);
112 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
114 char buf[LOG_BUF_SIZE];
116 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
118 return write_to_log(log_id, prio, tag, buf);
121 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
124 char buf[LOG_BUF_SIZE];
127 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
130 return write_to_log(log_id, prio, tag, buf);