#include <pthread.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <stdio.h>
#include <logcommon.h>
#define DEFAULT_CONFIG_DEBUGMODE 0
#define DEBUGMODE_FILE TZ_SYS_ETC"/.debugmode"
+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);
+int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = __write_to_log_null;
pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
extern void __dlog_init_pipe();
extern void __dlog_init_android();
* @param[in] msg The contents of the message.
* @return DLOG_ERROR_NOT_PERMITTED
*/
-int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
+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;
}
return;
failure:
- write_to_log = __write_to_log_null;
log_config_free(&config);
return;
}
* @brief DLog init
* @details Initializes the library
*/
-static void __dlog_init(void)
+static inline void __dlog_init(void)
{
static int is_initialized = 0;
+ if (is_initialized)
+ return;
+
pthread_mutex_lock(&log_init_lock);
if (!is_initialized) {
return DLOG_ERROR_NONE;
}
+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)
+{
+ char buf[LOG_MAX_SIZE];
+
+ __dlog_init();
+
+ int ret = check_should_log ? dlog_should_log(log_id, tag, prio) : 0;
+ if (ret < 0)
+ return ret;
+
+ vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
+
+ return write_to_log(log_id, prio, tag, buf);
+}
+
/**
* @brief Print log
* @details Print a log line
*/
int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
{
- int ret;
- char buf[LOG_MAX_SIZE];
-
- if (write_to_log == NULL)
- __dlog_init();
-
- ret = dlog_should_log(log_id, tag, prio);
-
- if (ret < 0)
- return ret;
-
- vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
- ret = write_to_log(log_id, prio, tag, buf);
+ int ret = __write_to_log(log_id, prio, tag, fmt, ap, true);
__dlog_fatal_assert(prio);
+
return ret;
}
*/
int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
{
- int ret;
va_list ap;
- char buf[LOG_MAX_SIZE];
-
- if (write_to_log == NULL)
- __dlog_init();
-
- ret = dlog_should_log(log_id, tag, prio);
-
- if (ret < 0)
- return ret;
va_start(ap, fmt);
- vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
+ int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
va_end(ap);
- ret = write_to_log(log_id, prio, tag, buf);
- __dlog_fatal_assert(prio);
return ret;
}
int dlog_vprint(log_priority prio, const char *tag, const char *fmt, va_list ap)
{
- char buf[LOG_MAX_SIZE];
-
- if (write_to_log == NULL)
- __dlog_init();
-
- vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
-
- return write_to_log(LOG_ID_APPS, prio, tag, buf);
+ return __write_to_log(LOG_ID_APPS, prio, tag, fmt, ap, false);
}
int dlog_print(log_priority prio, const char *tag, const char *fmt, ...)
{
va_list ap;
- char buf[LOG_MAX_SIZE];
-
- if (write_to_log == NULL)
- __dlog_init();
va_start(ap, fmt);
- vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
+ int ret = dlog_vprint(prio, tag, fmt, ap);
va_end(ap);
- return write_to_log(LOG_ID_APPS, prio, tag, buf);
+ return ret;
}
/**