Git init
[framework/system/dlog.git] / log.c
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifdef HAVE_PTHREADS
18 #include <pthread.h>
19 #endif
20 #include <string.h>
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <sys/uio.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <dlog.h>
27
28 #define LOG_BUF_SIZE    1024
29
30 #define LOG_MAIN        "log_main"
31 #define LOG_RADIO       "log_radio"
32 #define LOG_SYSTEM      "log_system"
33
34
35 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1 };
36
37 static int default_log_prio= DLOG_DEFAULT;
38
39
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 #ifdef HAVE_PTHREADS
43 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
44 #endif
45
46
47 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
48 {
49     return -1;
50 }
51
52 static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
53 {
54         ssize_t ret;
55         int log_fd;
56         struct iovec vec[3];
57
58         if( log_id < LOG_ID_MAX )
59                 log_fd = log_fds[log_id];
60         else
61                 return -1; // for TC
62
63         if (!tag)
64                   tag = "";
65
66         vec[0].iov_base = (unsigned char *) &prio;
67         vec[0].iov_len  = 1;
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;
72
73         ret = writev(log_fd, vec, 3);
74
75         return ret;
76 }
77
78
79 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
80 {
81 #ifdef HAVE_PTHREADS
82                 pthread_mutex_lock(&log_init_lock);
83 #endif
84         // get filtering info
85
86         // open device
87         if( write_to_log == __dlog_init)
88         {
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);
92
93                 if( log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 )
94                 {
95                         fprintf(stderr, "open log dev is failed\n");
96                         write_to_log = __write_to_log_null;
97                 }
98                 else
99                         write_to_log = __write_to_log_kernel;
100
101                 if( log_fds[LOG_ID_SYSTEM] < 0 )
102                 {
103                         log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
104                 }
105         }
106 #ifdef HAVE_PTHREADS
107     pthread_mutex_unlock(&log_init_lock);
108 #endif
109         return write_to_log(log_id, prio, tag, msg);
110 }
111
112 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
113 {
114     char buf[LOG_BUF_SIZE];
115
116     vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
117
118     return write_to_log(log_id, prio, tag, buf);
119 }
120
121 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
122 {
123     va_list ap;
124     char buf[LOG_BUF_SIZE];
125
126     va_start(ap, fmt);
127     vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
128     va_end(ap);
129
130     return write_to_log(log_id, prio, tag, buf);
131 }
132