1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-
3 * Copyright (c) 2005-2008, The Android Open Source Project
4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <logcommon.h>
25 #include <log_filters.h>
26 #include "loglimiter.h"
27 #include "logconfig.h"
31 #define DEFAULT_CONFIG_LIMITER 0
32 #define DEFAULT_CONFIG_PLOG 1
33 #define DEFAULT_CONFIG_DEBUGMODE 0
34 #define DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS 0
36 static int __write_to_log_null(log_id_t, log_priority, const char *, const char *);
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
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();
51 extern void __dlog_init_android();
56 static int fatal_assert;
57 static int limiter_apply_to_all_buffers;
61 * @details Ignores a log
62 * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
63 * @param[in] prio Priority of the message.
64 * @param[in] tag The message tag, identifies the sender.
65 * @param[in] msg The contents of the message.
66 * @return DLOG_ERROR_NOT_PERMITTED
68 static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)// LCOV_EXCL_LINE
70 return DLOG_ERROR_NOT_PERMITTED; // LCOV_EXCL_LINE
73 static void __configure_limiter(struct log_config *config)
80 __dynamic_filters_create(config);
81 limiter = __log_limiter_create(config);
84 __dynamic_filters_destroy();// LCOV_EXCL_LINE
87 static int __configure_backend(struct log_config *config)
91 const char *const backend = log_config_get(config, "backend");
95 if (!strcmp(backend, "pipe"))
97 else if (!strcmp(backend, "logger"))
98 __dlog_init_android();
105 static void __configure_parameters(struct log_config *config)
109 plog = log_config_get_int(config, "plog", DEFAULT_CONFIG_PLOG);
110 debugmode = log_config_get_int(config, "debugmode", DEFAULT_CONFIG_DEBUGMODE);
111 fatal_assert = access(DEBUGMODE_FILE, F_OK) != -1;
112 limiter = log_config_get_int(config, "limiter", DEFAULT_CONFIG_LIMITER);
113 limiter_apply_to_all_buffers = log_config_get_int(config,
114 "limiter_apply_to_all_buffers",
115 DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS);
119 * @brief Configure the library
120 * @details Reads relevant config values
122 static void __configure(void)
124 struct log_config config;
126 if (log_config_read(&config) < 0)
129 __configure_parameters(&config);
131 if (!__configure_backend(&config))
134 __configure_limiter(&config);
136 log_config_free(&config);
140 log_config_free(&config); // LCOV_EXCL_LINE
146 * @details Initializes the library
148 static inline void __dlog_init(void)
150 static int is_initialized = 0;
155 pthread_mutex_lock(&log_init_lock);
157 if (!is_initialized) {
162 pthread_mutex_unlock(&log_init_lock);
166 * @brief Fatal assertion
167 * @details Conditionally crash the sucka who sent the log
168 * @param[in] prio Priority of the log
170 static void __dlog_fatal_assert(int prio)
172 assert(!fatal_assert || (prio != DLOG_FATAL));
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 0 on success, else an error code.
182 * @retval DLOG_ERROR_INVALID_PARAMETER Invalid parameter
183 * @retval DLOG_ERROR_NOT_PERMITTED Not permitted
185 static int dlog_should_log(log_id_t log_id, int prio, const char *tag)
187 if (!debugmode && prio <= DLOG_DEBUG)
188 return DLOG_ERROR_INVALID_PARAMETER;
191 return DLOG_ERROR_INVALID_PARAMETER;
193 if (log_id == LOG_ID_INVALID || LOG_ID_MAX <= log_id)
194 return DLOG_ERROR_INVALID_PARAMETER;
196 if (log_id != LOG_ID_APPS && !plog)
197 return DLOG_ERROR_NOT_PERMITTED;
200 __dynamic_filters_update();
202 // LCOV_EXCL_START : disabled feature (limiter)
203 pthread_mutex_lock(&log_init_lock);
204 int should_log = __log_limiter_pass_log(tag, prio);
205 pthread_mutex_unlock(&log_init_lock);
208 return DLOG_ERROR_NOT_PERMITTED;
209 } else if (should_log < 0) {
210 write_to_log(log_id, prio, tag,
211 "Your log has been blocked due to limit of log lines per minute.");
212 return DLOG_ERROR_NOT_PERMITTED;
217 return DLOG_ERROR_NONE;
220 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)
222 char buf[LOG_MAX_PAYLOAD_SIZE];
226 /* if limiter_apply_to_all_buffers config variable is set to 1,
227 * check_should_log value does not matter and the entry is always
228 * tested against all conditions, i.e. limiter rules
230 int ret = (limiter_apply_to_all_buffers ? true : check_should_log) ? dlog_should_log(log_id, prio, tag) : 0;
234 vsnprintf(buf, sizeof buf, fmt, ap);
236 return write_to_log(log_id, prio, tag, buf);
241 * @details Print a log line
242 * @param[in] log_id The target buffer ID
243 * @param[in] prio Priority
245 * @param[in] fmt Format (same as printf)
246 * @param[in] ap Argument list
247 * @return Bytes written, or negative error
249 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
251 int ret = __write_to_log(log_id, prio, tag, fmt, ap, true);
252 __dlog_fatal_assert(prio);
259 * @details Print a log line
260 * @param[in] log_id The target buffer ID
261 * @param[in] prio Priority
263 * @param[in] fmt Format (same as printf)
264 * @return Bytes written, or negative error
266 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
271 int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
277 int dlog_vprint(log_priority prio, const char *tag, const char *fmt, va_list ap)
279 return __write_to_log(LOG_ID_APPS, prio, tag, fmt, ap, false);
282 int dlog_print(log_priority prio, const char *tag, const char *fmt, ...)
287 int ret = dlog_vprint(prio, tag, fmt, ap);
294 * @brief Finalize DLog
295 * @details Finalizes and deallocates the library
296 * @notes Assumes it has exclusive thread access,
297 * i.e. no other library function can run in parallel
299 void __attribute__((destructor)) __dlog_fini(void)
301 __log_limiter_destroy();
302 __dynamic_filters_destroy();