#define DEFAULT_CONFIG_DEBUGMODE 0
#define DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS 0
-static int __write_to_log_null(log_id_t, log_priority, const char *, const char *);
-
/**
* @brief Points to a function which writes a log message
* @details The function pointed to depends on the backend used
* @return Returns the number of bytes written on success and a negative error value on error.
* @see __dlog_init_backend
*/
-int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = __write_to_log_null;
+int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = NULL;
pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
extern void __dlog_init_pipe(const struct log_config *conf);
extern void __dlog_init_android(const struct log_config *conf);
static int fatal_assert;
static int limiter_apply_to_all_buffers;
-/**
- * @brief Null handler
- * @details Ignores a log
- * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
- * @param[in] prio Priority of the message.
- * @param[in] tag The message tag, identifies the sender.
- * @param[in] msg The contents of the message.
- * @return DLOG_ERROR_NOT_PERMITTED
- */
-static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
-{
- return DLOG_ERROR_NOT_PERMITTED;
-}
-
static void __configure_limiter(struct log_config *config)
{
assert(config);
* @brief Configure the library
* @details Reads relevant config values
*/
-static void __configure(void)
+#ifdef UNIT_TEST
+void __configure(void)
+#else
+static void __attribute__((constructor)) __configure(void)
+#endif
{
struct log_config config;
}
/**
- * @brief DLog init
- * @details Initializes the library
- */
-static bool is_initialized = false;
-static inline void __dlog_init(void)
-{
- if (is_initialized)
- return;
-
- pthread_mutex_lock(&log_init_lock);
-
- if (!is_initialized) {
- __configure();
- is_initialized = true;
- }
-
- pthread_mutex_unlock(&log_init_lock);
-}
-
-/**
* @brief Fatal assertion
* @details Conditionally crash the sucka who sent the log
* @param[in] prio Priority of the log
if (ret < 0)
return ret;
- __dlog_init();
+ if (!write_to_log)
+ return DLOG_ERROR_NOT_PERMITTED;
/* if limiter_apply_to_all_buffers config variable is set to 1,
* check_should_log value does not matter and the entry is always
{
__log_limiter_destroy();
__dynamic_config_destroy();
- write_to_log = __write_to_log_null;
- is_initialized = false;
}
#endif
int main()
{
- __dlog_fini();
- __dlog_fini();
-
fail_conf_read = true;
+ __configure();
assert(dlog_print(DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NOT_PERMITTED);
assert(write_to_log != wtl_android);
assert(write_to_log != wtl_pipe);
fail_conf_read = false;
__dlog_fini();
+ __configure();
assert(dlog_print(DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NOT_PERMITTED);
assert(write_to_log != wtl_android);
assert(write_to_log != wtl_pipe);
__dlog_fini();
log_config_set(&CONFIG, "backend", "Hej, zamawiam Bentoye. Czekam do 11:15.");
+ __configure();
assert(dlog_print(DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NOT_PERMITTED);
assert(write_to_log != wtl_pipe);
assert(write_to_log != wtl_android);
__dlog_fini();
+
log_config_set(&CONFIG, "backend", "pipe");
log_config_set(&CONFIG, "limiter", "0");
+ __configure();
assert(dlog_print(DLOG_ERROR, "tag", "msg") == 0xDECC16);
assert(!dynamic_config_called);
assert(write_to_log == wtl_pipe);
log_config_set(&CONFIG, "debugmode", "0");
use_dynamic_conf = true;
limiter_ret = 1;
+ __configure();
assert(dlog_print(DLOG_ERROR, "tag", "msg") == 45835705);
assert(__dlog_print(LOG_ID_MAIN, DLOG_ERROR, "tag", "msg") == 45835705);
assert(write_to_log != wtl_pipe); // ceci n'est pas une pipe
__dlog_fini();
log_config_set(&CONFIG, "enable_system", "0");
+ __configure();
assert(__dlog_print(LOG_ID_SYSTEM, DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NONE);
__dlog_fini();
fail_snprintf = true;
+ __configure();
assert(__dlog_print(LOG_ID_SYSTEM, DLOG_ERROR, "tag", "msg") == 45835705);
fail_snprintf = false;
__dlog_fini();
+ __configure();
tct_tests();
}