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