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