1 /******************************************************************************
2 * ventoy_log.c ---- ventoy log
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
29 #include <ventoy_define.h>
31 static int g_ventoy_log_level
= VLOG_DEBUG
;
32 static pthread_mutex_t g_log_mutex
;
34 int ventoy_log_init(void)
36 pthread_mutex_init(&g_log_mutex
, NULL
);
40 void ventoy_log_exit(void)
42 pthread_mutex_destroy(&g_log_mutex
);
45 void ventoy_set_loglevel(int level
)
47 g_ventoy_log_level
= level
;
50 void ventoy_syslog_newline(int level
, const char *Fmt
, ...)
58 if (level
> g_ventoy_log_level
)
64 localtime_r(&stamp
, &ttm
);
67 vsnprintf(log
, 512, Fmt
, arg
);
70 pthread_mutex_lock(&g_log_mutex
);
71 fp
= fopen(VTOY_LOG_FILE
, "a+");
74 fprintf(fp
, "[%04u/%02u/%02u %02u:%02u:%02u] %s\n",
75 ttm
.tm_year
, ttm
.tm_mon
, ttm
.tm_mday
,
76 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
80 pthread_mutex_unlock(&g_log_mutex
);
83 void ventoy_syslog(int level
, const char *Fmt
, ...)
91 if (level
> g_ventoy_log_level
)
97 localtime_r(&stamp
, &ttm
);
100 vsnprintf(log
, 512, Fmt
, arg
);
103 pthread_mutex_lock(&g_log_mutex
);
104 fp
= fopen(VTOY_LOG_FILE
, "a+");
107 fprintf(fp
, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
108 ttm
.tm_year
, ttm
.tm_mon
, ttm
.tm_mday
,
109 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
113 pthread_mutex_unlock(&g_log_mutex
);