+#include <stdio.h>
#include <dlog.h>
#include "hal-tbm-types.h"
+#include "common.h"
+
+#define TBM_PATH_LEN 1024
+
+#define COLOR_RED "\x1b[31m" /* for error */
+#define COLOR_YELLOW "\x1b[33m" /* for warning */
+#define COLOR_GREEN "\x1b[32m" /* for info */
+#define COLOR_RESET "\x1b[0m"
+
+#if ENABLE_DLOG
+static unsigned int dlog_enable = 1;
+#else
+static unsigned int dlog_enable = 0;
+#endif
+static unsigned int color_enable = 1;
unsigned int tbm_log_debug_level = HAL_TBM_LOG_LEVEL_INFO;
+static int stdout_fd = -1;
+
+void
+tbm_log_set_path(const char *path)
+{
+ if (!path) {
+ if (stdout_fd != -1) {
+ fflush(stdout);
+ close(STDOUT_FILENO);
+ dup2(stdout_fd, STDOUT_FILENO);
+ close(stdout_fd);
+ stdout_fd = -1;
+ }
+ } else {
+ char fd_name[TBM_PATH_LEN];
+ int log_fd = -1;
+ FILE *log_fl;
+
+ snprintf(fd_name, TBM_PATH_LEN, "%s", path);
+
+ log_fl = fopen(fd_name, "a");
+ if (!log_fl) {
+ HAL_TBM_ERR("failed: open file(%s)\n", fd_name);
+ return;
+ }
+
+ if (stdout_fd == -1) {
+ fflush(stdout);
+ stdout_fd = dup(STDOUT_FILENO);
+ if (stdout_fd < 0) {
+ HAL_TBM_ERR("dup failed: %m\n");
+ fclose(log_fl);
+ return;
+ }
+ }
+
+ setvbuf(log_fl, NULL, _IOLBF, 512);
+ log_fd = fileno(log_fl);
+
+ close(STDOUT_FILENO);
+ dup2(log_fd, STDOUT_FILENO);
+ fclose(log_fl);
+ }
+}
+
+static void
+_tbm_log_vprint_stdout(int level, const char *fmt, va_list arg)
+{
+ char *lvl_str[] = {"HAL_TBM_NON", "HAL_TBM_ERR", "HAL_TBM_WRN", "HAL_TBM_INF", "HAL_TBM_DBG"};
+ char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET};
+
+ if (level > tbm_log_debug_level)
+ return;
+
+ if (color_enable)
+ printf("%s", color[level]);
+ printf("[%s]", lvl_str[level]);
+ if (color_enable)
+ printf(COLOR_RESET);
+ vprintf(fmt, arg);
+ printf("\n");
+}
+
+void
+tbm_log_print_stdout(int level, const char *fmt, ...)
+{
+ va_list arg;
+ va_start(arg, fmt);
+ _tbm_log_vprint_stdout(level, fmt, arg);
+ va_end(arg);
+}
+
static void
_tbm_log_dlog_print(int level, const char *fmt, va_list arg)
{
if (level > tbm_log_debug_level)
return;
- va_start(arg, fmt);
- _tbm_log_dlog_print(level, fmt, arg);
- va_end(arg);
+ if (dlog_enable) {
+ va_start(arg, fmt);
+ _tbm_log_dlog_print(level, fmt, arg);
+ va_end(arg);
+ } else {
+ va_start(arg, fmt);
+ _tbm_log_vprint_stdout(level, fmt, arg);
+ va_end(arg);
+ }
}
\ No newline at end of file