libdlog: construct with the maximum priority
[platform/core/system/dlog.git] / src / libdlog / log.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-
2  * DLOG
3  * Copyright (c) 2005-2008, The Android Open Source Project
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 // C
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 // POSIX
26 #include <pthread.h>
27 #include <unistd.h>
28
29 // DLog
30 #include <dynamic_config.h>
31 #include <libdlog.h>
32 #include <logcommon.h>
33 #include "logconfig.h"
34 #include "loglimiter.h"
35
36 #define DEFAULT_CONFIG_LIMITER false
37 #define DEFAULT_CONFIG_PLOG true
38 #define DEFAULT_CONFIG_DEBUGMODE 0
39 #define DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS 0
40
41 /**
42  * @brief Points to a function which writes a log message
43  * @details The function pointed to depends on the backend used
44  * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
45  * @param[in] prio Priority of the message.
46  * @param[in] tag The message tag, identifies the sender.
47  * @param[in] msg The contents of the message.
48  * @return Returns the number of bytes written on success and a negative error value on error.
49  * @see __dlog_init_backend
50  */
51 int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = NULL;
52 pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
53 extern void __dlog_init_pipe(const struct log_config *conf);
54 extern void __dlog_init_android(const struct log_config *conf);
55
56 bool limiter;
57 bool dynamic_config;
58 bool plog[LOG_ID_MAX];
59 bool plog_default_values[LOG_ID_MAX];
60
61 static int debugmode;
62 static int fatal_assert;
63 static int limiter_apply_to_all_buffers;
64
65 static void __configure_limiter(struct log_config *config)
66 {
67         assert(config);
68
69         if (!limiter)
70                 return;
71
72         limiter = __log_limiter_create(config);
73 }
74
75 static int __configure_backend(struct log_config *config)
76 {
77         assert(config);
78
79         const char *const backend = log_config_get(config, "backend");
80         if (!backend)
81                 return 0;
82
83         if (!strcmp(backend, "pipe"))
84                 __dlog_init_pipe(config);
85         else if (!strcmp(backend, "logger"))
86                 __dlog_init_android(config);
87         else
88                 return 0;
89
90         return 1;
91 }
92
93 static void __set_plog_default_values()
94 {
95         for (int i = 0; i < NELEMS(plog); ++i)
96                 plog_default_values[i] = plog[i];
97 }
98
99 static void __initialize_plog(const struct log_config *config)
100 {
101         assert(config);
102
103         const bool plog_default = log_config_get_boolean(config, "plog", DEFAULT_CONFIG_PLOG);
104         for (int i = 0; i < NELEMS(plog); ++i)
105                 plog[i] = plog_default;
106         plog[LOG_ID_APPS] = true; // the default does not apply here for backward compatibility reasons.
107         __set_plog_default_values();
108 }
109
110 static void __configure_parameters(struct log_config *config)
111 {
112         assert(config);
113
114         __initialize_plog(config);
115         __update_plog(config);
116         __set_plog_default_values();
117
118         debugmode = log_config_get_int(config, "debugmode", DEFAULT_CONFIG_DEBUGMODE);
119         fatal_assert = access(DEBUGMODE_FILE, F_OK) != -1;
120         limiter = log_config_get_boolean(config, "limiter", DEFAULT_CONFIG_LIMITER);
121         limiter_apply_to_all_buffers = log_config_get_int(config,
122                                                                         "limiter_apply_to_all_buffers",
123                                                                         DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS);
124 }
125
126 void __update_plog(const struct log_config *conf)
127 {
128         assert(conf);
129
130         for (int i = 0; i < NELEMS(plog); ++i) {
131                 char key[MAX_CONF_KEY_LEN];
132                 const int r = snprintf(key, sizeof key, "enable_%s", log_name_by_id((log_id_t)i));
133                 if (r < 0)
134                         continue;
135                 plog[i] = log_config_get_boolean(conf, key, plog_default_values[i]);
136         }
137 }
138
139 /**
140  * @brief Configure the library
141  * @details Reads relevant config values
142  * @remarks The constructor has the highest possible priority (100 and below are reserved).
143  *          This is because other libraries are likely to use libdlog and might want to
144  *          use it as early as in their own constructor. This means that libdlog must be
145  *          constructed as soon as possible.
146  */
147 #ifdef UNIT_TEST
148 void __configure(void)
149 #else
150 static void __attribute__((constructor(101))) __configure(void)
151 #endif
152 {
153         struct log_config config;
154
155         if (log_config_read(&config) < 0)
156                 goto out;
157
158         dynamic_config = __dynamic_config_create(&config);
159
160         __configure_parameters(&config);
161
162         if (!__configure_backend(&config))
163                 goto out;
164
165         __configure_limiter(&config);
166
167 out:
168         log_config_free(&config);
169         return;
170 }
171
172 /**
173  * @brief Fatal assertion
174  * @details Conditionally crash the sucka who sent the log
175  * @param[in] prio Priority of the log
176  */
177 static void __dlog_fatal_assert(int prio)
178 {
179         assert(!fatal_assert || (prio != DLOG_FATAL));
180 }
181
182 /**
183  * @brief Check log validity
184  * @details Checks whether the log is valid and eligible for printing
185  * @param[in] log_id The target buffer ID
186  * @param[in] prio The log's priority
187  * @param[in] tag The log's tag
188  * @return DLOG_ERROR_NONE on success, else an error code.
189  * @retval DLOG_ERROR_INVALID_PARAMETER Invalid parameter
190  */
191 static int dlog_check_validity(log_id_t log_id, int prio, const char *tag)
192 {
193         if (!tag)
194                 return DLOG_ERROR_INVALID_PARAMETER;
195
196         if (log_id <= LOG_ID_INVALID || LOG_ID_MAX <= log_id)
197                 return DLOG_ERROR_INVALID_PARAMETER;
198
199         return DLOG_ERROR_NONE;
200 }
201
202 /**
203  * @brief Check log against limiter rules
204  * @details Checks whether the log passes current limiter rules
205  * @param[in] log_id The target buffer ID
206  * @param[in] prio The log's priority
207  * @param[in] tag The log's tag
208  * @return DLOG_ERROR_NONE on success, else an error code.
209  * @retval DLOG_ERROR_NOT_PERMITTED Not permitted
210  */
211 static int dlog_check_limiter(log_id_t log_id, int prio, const char *tag)
212 {
213         if (!debugmode && prio <= DLOG_DEBUG)
214                 return DLOG_ERROR_NOT_PERMITTED;
215
216         if (dynamic_config)
217                 __dynamic_config_update();
218
219         if (limiter) {
220                 pthread_mutex_lock(&log_init_lock);
221                 int should_log = __log_limiter_pass_log(tag, prio);
222                 pthread_mutex_unlock(&log_init_lock);
223
224                 if (!should_log) {
225                         return DLOG_ERROR_NOT_PERMITTED;
226                 } else if (should_log < 0) {
227                         write_to_log(log_id, prio, tag,
228                                         "Your log has been blocked due to limit of log lines per minute.");
229                         return DLOG_ERROR_NOT_PERMITTED;
230                 }
231         }
232
233         if (!plog[log_id])
234                 return DLOG_ERROR_NOT_PERMITTED;
235
236         return DLOG_ERROR_NONE;
237 }
238
239 static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap, bool check_should_log)
240 {
241         char buf[LOG_MAX_PAYLOAD_SIZE];
242
243         int ret = dlog_check_validity(log_id, prio, tag);
244         if (ret < 0)
245                 return ret;
246
247         if (!write_to_log)
248                 return DLOG_ERROR_NOT_PERMITTED;
249
250         /* if limiter_apply_to_all_buffers config variable is set to 1,
251          * check_should_log value does not matter and the entry is always
252          * tested against all conditions, i.e. limiter rules
253          */
254         ret = (check_should_log || limiter_apply_to_all_buffers) ? dlog_check_limiter(log_id, prio, tag) : 0;
255
256         if (ret < 0)
257                 return DLOG_ERROR_NONE;
258
259         vsnprintf(buf, sizeof buf, fmt, ap);
260
261         return write_to_log(log_id, prio, tag, buf);
262 }
263
264 /**
265  * @brief Print log
266  * @details Print a log line
267  * @param[in] log_id The target buffer ID
268  * @param[in] prio Priority
269  * @param[in] tag tag
270  * @param[in] fmt Format (same as printf)
271  * @param[in] ap Argument list
272  * @return Bytes written, or negative error
273  */
274 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
275 {
276         int ret = __write_to_log(log_id, prio, tag, fmt, ap, true);
277         __dlog_fatal_assert(prio);
278
279         return ret;
280 }
281
282 /**
283  * @brief Print log
284  * @details Print a log line
285  * @param[in] log_id The target buffer ID
286  * @param[in] prio Priority
287  * @param[in] tag tag
288  * @param[in] fmt Format (same as printf)
289  * @return Bytes written, or negative error
290  */
291 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
292 {
293         va_list ap;
294
295         va_start(ap, fmt);
296         int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
297         va_end(ap);
298
299         return ret;
300 }
301
302 int dlog_vprint(log_priority prio, const char *tag, const char *fmt, va_list ap)
303 {
304         return __write_to_log(LOG_ID_APPS, prio, tag, fmt, ap, false);
305 }
306
307 int dlog_print(log_priority prio, const char *tag, const char *fmt, ...)
308 {
309         va_list ap;
310
311         va_start(ap, fmt);
312         int ret = dlog_vprint(prio, tag, fmt, ap);
313         va_end(ap);
314
315         return ret;
316 }
317
318 #ifdef UNIT_TEST
319 /**
320  * @brief Finalize DLog
321  * @details Finalizes and deallocates the library
322  * @notes Used in tests only
323  */
324 void __dlog_fini(void)
325 {
326         __log_limiter_destroy();
327         __dynamic_config_destroy();
328 }
329 #endif
330