Merge "limiter: do not treat limiter restrictions as an error" into tizen
[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 #include <pthread.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23
24 #include <dynamic_config.h>
25 #include <logcommon.h>
26 #include "loglimiter.h"
27 #include "logconfig.h"
28 #include <assert.h>
29 #include <unistd.h>
30
31 #define DEFAULT_CONFIG_LIMITER false
32 #define DEFAULT_CONFIG_PLOG true
33 #define DEFAULT_CONFIG_DEBUGMODE 0
34 #define DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS 0
35
36 static int __write_to_log_null(log_id_t, log_priority, const char *, const char *);
37
38 /**
39  * @brief Points to a function which writes a log message
40  * @details The function pointed to depends on the backend used
41  * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
42  * @param[in] prio Priority of the message.
43  * @param[in] tag The message tag, identifies the sender.
44  * @param[in] msg The contents of the message.
45  * @return Returns the number of bytes written on success and a negative error value on error.
46  * @see __dlog_init_backend
47  */
48 int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = __write_to_log_null;
49 pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
50 extern void __dlog_init_pipe(const struct log_config *conf);
51 extern void __dlog_init_android(const struct log_config *conf);
52
53 bool limiter;
54 bool dynamic_config;
55 bool plog[LOG_ID_MAX];
56 bool plog_default_values[LOG_ID_MAX];
57
58 void __update_plog(const struct log_config *conf);
59
60 static int debugmode;
61 static int fatal_assert;
62 static int limiter_apply_to_all_buffers;
63
64 /**
65  * @brief Null handler
66  * @details Ignores a log
67  * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
68  * @param[in] prio Priority of the message.
69  * @param[in] tag The message tag, identifies the sender.
70  * @param[in] msg The contents of the message.
71  * @return DLOG_ERROR_NOT_PERMITTED
72  */
73 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)// LCOV_EXCL_LINE
74 {
75         return DLOG_ERROR_NOT_PERMITTED; // LCOV_EXCL_LINE
76 }
77
78 static void __configure_limiter(struct log_config *config)
79 {
80         assert(config);
81
82         if (!limiter)
83                 return;
84
85         limiter = __log_limiter_create(config);
86 }
87
88 static int __configure_backend(struct log_config *config)
89 {
90         assert(config);
91
92         const char *const backend = log_config_get(config, "backend");
93         if (!backend)
94                 return 0;
95
96         if (!strcmp(backend, "pipe"))
97                 __dlog_init_pipe(config);
98         else if (!strcmp(backend, "logger"))
99                 __dlog_init_android(config);
100         else
101                 return 0;
102
103         return 1;
104 }
105
106 static void __set_plog_default_values()
107 {
108         for (int i = 0; i < NELEMS(plog); ++i)
109                 plog_default_values[i] = plog[i];
110 }
111
112 static void __initialize_plog(const struct log_config *config)
113 {
114         assert(config);
115
116         const bool plog_default = log_config_get_boolean(config, "plog", DEFAULT_CONFIG_PLOG);
117         for (int i = 0; i < NELEMS(plog); ++i)
118                 plog[i] = plog_default;
119         plog[LOG_ID_APPS] = true; // the default does not apply here for backward compatibility reasons.
120         __set_plog_default_values();
121 }
122
123 static void __configure_parameters(struct log_config *config)
124 {
125         assert(config);
126
127         __initialize_plog(config);
128         __update_plog(config);
129         __set_plog_default_values();
130
131         debugmode = log_config_get_int(config, "debugmode", DEFAULT_CONFIG_DEBUGMODE);
132         fatal_assert = access(DEBUGMODE_FILE, F_OK) != -1;
133         limiter = log_config_get_boolean(config, "limiter", DEFAULT_CONFIG_LIMITER);
134         limiter_apply_to_all_buffers = log_config_get_int(config,
135                                                                         "limiter_apply_to_all_buffers",
136                                                                         DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS);
137 }
138
139 void __update_plog(const struct log_config *conf)
140 {
141         assert(conf);
142
143         for (int i = 0; i < NELEMS(plog); ++i) {
144                 char key[MAX_CONF_KEY_LEN];
145                 const int r = snprintf(key, sizeof key, "enable_%s", log_name_by_id((log_id_t)i));
146                 if (r < 0)
147                         continue;
148                 plog[i] = log_config_get_boolean(conf, key, plog_default_values[i]);
149         }
150 }
151
152 /**
153  * @brief Configure the library
154  * @details Reads relevant config values
155  */
156 static void __configure(void)
157 {
158         struct log_config config;
159
160         if (log_config_read(&config) < 0)
161                 goto failure;
162
163         dynamic_config = __dynamic_config_create(&config);
164
165         __configure_parameters(&config);
166
167         if (!__configure_backend(&config))
168                 goto failure;
169
170         __configure_limiter(&config);
171
172         log_config_free(&config);
173         return;
174
175 failure:
176         log_config_free(&config); // LCOV_EXCL_LINE
177         return;
178 }
179
180 /**
181  * @brief DLog init
182  * @details Initializes the library
183  */
184 static inline void __dlog_init(void)
185 {
186         static int is_initialized = 0;
187
188         if (is_initialized)
189                 return;
190
191         pthread_mutex_lock(&log_init_lock);
192
193         if (!is_initialized) {
194                 __configure();
195                 is_initialized = 1;
196         }
197
198         pthread_mutex_unlock(&log_init_lock);
199 }
200
201 /**
202  * @brief Fatal assertion
203  * @details Conditionally crash the sucka who sent the log
204  * @param[in] prio Priority of the log
205  */
206 static void __dlog_fatal_assert(int prio)
207 {
208         assert(!fatal_assert || (prio != DLOG_FATAL));
209 }
210
211 /**
212  * @brief Check log validity
213  * @details Checks whether the log is valid and eligible for printing
214  * @param[in] log_id The target buffer ID
215  * @param[in] prio The log's priority
216  * @param[in] tag The log's tag
217  * @return DLOG_ERROR_NONE on success, else an error code.
218  * @retval DLOG_ERROR_INVALID_PARAMETER Invalid parameter
219  */
220 static int dlog_check_validity(log_id_t log_id, int prio, const char *tag)
221 {
222         if (!tag)
223                 return DLOG_ERROR_INVALID_PARAMETER;
224
225         if (log_id <= LOG_ID_INVALID || LOG_ID_MAX <= log_id)
226                 return DLOG_ERROR_INVALID_PARAMETER;
227
228         return DLOG_ERROR_NONE;
229 }
230
231 /**
232  * @brief Check log against limiter rules
233  * @details Checks whether the log passes current limiter rules
234  * @param[in] log_id The target buffer ID
235  * @param[in] prio The log's priority
236  * @param[in] tag The log's tag
237  * @return DLOG_ERROR_NONE on success, else an error code.
238  * @retval DLOG_ERROR_NOT_PERMITTED Not permitted
239  */
240 static int dlog_check_limiter(log_id_t log_id, int prio, const char *tag)
241 {
242         if (!debugmode && prio <= DLOG_DEBUG)
243                 return DLOG_ERROR_NOT_PERMITTED;
244
245         if (dynamic_config)
246                 __dynamic_config_update();
247
248         // LCOV_EXCL_START : disabled feature (limiter)
249         if (limiter) {
250                 pthread_mutex_lock(&log_init_lock);
251                 int should_log = __log_limiter_pass_log(tag, prio);
252                 pthread_mutex_unlock(&log_init_lock);
253
254                 if (!should_log) {
255                         return DLOG_ERROR_NOT_PERMITTED;
256                 } else if (should_log < 0) {
257                         write_to_log(log_id, prio, tag,
258                                         "Your log has been blocked due to limit of log lines per minute.");
259                         return DLOG_ERROR_NOT_PERMITTED;
260                 }
261         }
262         // LCOV_EXCL_STOP
263
264         if (!plog[log_id])
265                 return DLOG_ERROR_NOT_PERMITTED;
266
267         return DLOG_ERROR_NONE;
268 }
269
270 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)
271 {
272         char buf[LOG_MAX_PAYLOAD_SIZE];
273
274         int ret = dlog_check_validity(log_id, prio, tag);
275         if (ret < 0)
276                 return ret;
277
278         __dlog_init();
279
280         /* if limiter_apply_to_all_buffers config variable is set to 1,
281          * check_should_log value does not matter and the entry is always
282          * tested against all conditions, i.e. limiter rules
283          */
284         ret = (check_should_log || limiter_apply_to_all_buffers) ? dlog_check_limiter(log_id, prio, tag) : 0;
285
286         if (ret < 0)
287                 return DLOG_ERROR_NONE;
288
289         vsnprintf(buf, sizeof buf, fmt, ap);
290
291         return write_to_log(log_id, prio, tag, buf);
292 }
293
294 /**
295  * @brief Print log
296  * @details Print a log line
297  * @param[in] log_id The target buffer ID
298  * @param[in] prio Priority
299  * @param[in] tag tag
300  * @param[in] fmt Format (same as printf)
301  * @param[in] ap Argument list
302  * @return Bytes written, or negative error
303  */
304 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
305 {
306         int ret = __write_to_log(log_id, prio, tag, fmt, ap, true);
307         __dlog_fatal_assert(prio);
308
309         return ret;
310 }
311
312 /**
313  * @brief Print log
314  * @details Print a log line
315  * @param[in] log_id The target buffer ID
316  * @param[in] prio Priority
317  * @param[in] tag tag
318  * @param[in] fmt Format (same as printf)
319  * @return Bytes written, or negative error
320  */
321 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
322 {
323         va_list ap;
324
325         va_start(ap, fmt);
326         int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
327         va_end(ap);
328
329         return ret;
330 }
331
332 int dlog_vprint(log_priority prio, const char *tag, const char *fmt, va_list ap)
333 {
334         return __write_to_log(LOG_ID_APPS, prio, tag, fmt, ap, false);
335 }
336
337 int dlog_print(log_priority prio, const char *tag, const char *fmt, ...)
338 {
339         va_list ap;
340
341         va_start(ap, fmt);
342         int ret = dlog_vprint(prio, tag, fmt, ap);
343         va_end(ap);
344
345         return ret;
346 }
347
348 /**
349  * @brief Finalize DLog
350  * @details Finalizes and deallocates the library
351  * @notes Assumes it has exclusive thread access,
352  *        i.e. no other library function can run in parallel
353  */
354 void __attribute__((destructor)) __dlog_fini(void)
355 {
356         __log_limiter_destroy();
357         __dynamic_config_destroy();
358 }