tizen_2.0 merged
[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 && prio<g_debug_level)
60         {
61                 return 0;
62         }
63         if( log_id < LOG_ID_MAX )
64                 log_fd = log_fds[log_id];
65         else
66                 return -1; // for TC
67
68         if (!tag)
69                   tag = "";
70
71         vec[0].iov_base = (unsigned char *) &prio;
72         vec[0].iov_len  = 1;
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;
77
78         ret = writev(log_fd, vec, 3);
79
80         return ret;
81 }
82
83 void init_debug_level(void)
84 {
85         char *debuglevel=getenv("TIZEN_DEBUG_LEVEL");
86         if(!debuglevel) {
87 #ifndef NDEBUG
88                 fprintf(stderr, "Not matched env. variable, TIZEN_DEBUG_LEVEL");
89 #endif
90                 return;
91         }
92         g_debug_level=atoi(debuglevel);
93 #ifndef NDEBUG
94         fprintf(stderr, "debug level init %d(%s) \n",g_debug_level,debuglevel);
95 #endif
96 }
97 static int __dlog_init(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
98 {
99 #ifdef HAVE_PTHREADS
100                 pthread_mutex_lock(&log_init_lock);
101 #endif
102         // get filtering info
103
104         // open device
105         if( write_to_log == __dlog_init)
106         {
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);
111
112                 init_debug_level();
113
114                 if( log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 )
115                 {
116                         fprintf(stderr, "open log dev is failed\n");
117                         write_to_log = __write_to_log_null;
118                 }
119                 else
120                         write_to_log = __write_to_log_kernel;
121
122                 if( log_fds[LOG_ID_SYSTEM] < 0 )
123                 {
124                         log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
125                 }
126
127                 if( log_fds[LOG_ID_APPS] < 0 )
128                 {
129                         log_fds[LOG_ID_APPS] = log_fds[LOG_ID_MAIN];
130                 }
131         }
132 #ifdef HAVE_PTHREADS
133     pthread_mutex_unlock(&log_init_lock);
134 #endif
135         return write_to_log(log_id, prio, tag, msg);
136 }
137
138 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
139 {
140     char buf[LOG_BUF_SIZE];
141
142     vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
143
144     return write_to_log(log_id, prio, tag, buf);
145 }
146
147 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
148 {
149     va_list ap;
150     char buf[LOG_BUF_SIZE];
151
152     va_start(ap, fmt);
153     vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
154     va_end(ap);
155
156     return write_to_log(log_id, prio, tag, buf);
157 }
158