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 <linux/limits.h>
30 #include <ventoy_define.h>
32 extern char g_log_file
[PATH_MAX
];
33 static int g_ventoy_log_level
= VLOG_DEBUG
;
34 static pthread_mutex_t g_log_mutex
;
36 int ventoy_log_init(void)
38 pthread_mutex_init(&g_log_mutex
, NULL
);
42 void ventoy_log_exit(void)
44 pthread_mutex_destroy(&g_log_mutex
);
47 void ventoy_set_loglevel(int level
)
49 g_ventoy_log_level
= level
;
52 void ventoy_syslog_newline(int level
, const char *Fmt
, ...)
60 if (level
> g_ventoy_log_level
)
66 localtime_r(&stamp
, &ttm
);
69 vsnprintf(log
, 512, Fmt
, arg
);
72 pthread_mutex_lock(&g_log_mutex
);
73 fp
= fopen(g_log_file
, "a+");
76 fprintf(fp
, "[%04u/%02u/%02u %02u:%02u:%02u] %s\n",
77 ttm
.tm_year
, ttm
.tm_mon
, ttm
.tm_mday
,
78 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
82 pthread_mutex_unlock(&g_log_mutex
);
85 void ventoy_syslog_printf(const char *Fmt
, ...)
94 localtime_r(&stamp
, &ttm
);
97 vsnprintf(log
, 512, Fmt
, arg
);
100 pthread_mutex_lock(&g_log_mutex
);
101 fp
= fopen(g_log_file
, "a+");
104 fprintf(fp
, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
105 ttm
.tm_year
, ttm
.tm_mon
, ttm
.tm_mday
,
106 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
110 pthread_mutex_unlock(&g_log_mutex
);
113 void ventoy_syslog(int level
, const char *Fmt
, ...)
121 if (level
> g_ventoy_log_level
)
127 localtime_r(&stamp
, &ttm
);
130 vsnprintf(log
, 512, Fmt
, arg
);
133 pthread_mutex_lock(&g_log_mutex
);
134 fp
= fopen(g_log_file
, "a+");
137 fprintf(fp
, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
138 ttm
.tm_year
+ 1900, ttm
.tm_mon
, ttm
.tm_mday
,
139 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
143 pthread_mutex_unlock(&g_log_mutex
);