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