enable app dlog macro
[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 <stdlib.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <fcntl.h>
24 #include <sys/uio.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <dlog.h>
28
29 #define LOG_BUF_SIZE    1024
30
31 #define LOG_MAIN        "log_main"
32 #define LOG_RADIO       "log_radio"
33 #define LOG_SYSTEM      "log_system"
34 #define LOG_APPS        "log_apps"
35
36
37 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
38
39 static int g_debug_level= DLOG_SILENT;
40
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 #ifdef HAVE_PTHREADS
44 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
45 #endif
46
47
48 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
49 {
50     return -1;
51 }
52
53 static int __write_to_log_kernel(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
54 {
55         ssize_t ret;
56         int log_fd;
57         struct iovec vec[3];
58
59         if (log_id < LOG_ID_APPS) {
60                 if(prio<g_debug_level) {
61                         return 0;
62                 }
63         } else if (LOG_ID_MAX <= log_id) {
64                 return 0;
65         }
66         if( log_id < LOG_ID_MAX )
67                 log_fd = log_fds[log_id];
68         else
69                 return -1; // for TC
70
71         if (!tag)
72                   tag = "";
73
74         vec[0].iov_base = (unsigned char *) &prio;
75         vec[0].iov_len  = 1;
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;
80
81         ret = writev(log_fd, vec, 3);
82
83         return ret;
84 }
85
86 void init_debug_level(void)
87 {
88         char *debuglevel=getenv("TIZEN_DEBUG_LEVEL");
89         if(!debuglevel) {
90 #ifndef NDEBUG
91                 fprintf(stderr, "Not matched env. variable, TIZEN_DEBUG_LEVEL");
92 #endif
93                 return;
94         }
95         g_debug_level=atoi(debuglevel);
96 #ifndef NDEBUG
97         fprintf(stderr, "debug level init %d(%s) \n",g_debug_level,debuglevel);
98 #endif
99 }
100 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
101 {
102 #ifdef HAVE_PTHREADS
103                 pthread_mutex_lock(&log_init_lock);
104 #endif
105         // get filtering info
106
107         // open device
108         if( write_to_log == __dlog_init)
109         {
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);
114
115                 init_debug_level();
116
117                 if( log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 )
118                 {
119                         fprintf(stderr, "open log dev is failed\n");
120                         write_to_log = __write_to_log_null;
121                 }
122                 else
123                         write_to_log = __write_to_log_kernel;
124
125                 if( log_fds[LOG_ID_SYSTEM] < 0 )
126                 {
127                         log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
128                 }
129
130                 if( log_fds[LOG_ID_APPS] < 0 )
131                 {
132                         log_fds[LOG_ID_APPS] = log_fds[LOG_ID_MAIN];
133                 }
134         }
135 #ifdef HAVE_PTHREADS
136     pthread_mutex_unlock(&log_init_lock);
137 #endif
138         return write_to_log(log_id, prio, tag, msg);
139 }
140
141 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
142 {
143     char buf[LOG_BUF_SIZE];
144
145     vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
146
147     return write_to_log(log_id, prio, tag, buf);
148 }
149
150 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
151 {
152     va_list ap;
153     char buf[LOG_BUF_SIZE];
154
155     va_start(ap, fmt);
156     vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
157     va_end(ap);
158
159     return write_to_log(log_id, prio, tag, buf);
160 }
161