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/>.
25 #include <ventoy_define.h>
26 #include <ventoy_util.h>
28 extern char g_log_file
[MAX_PATH
];
29 static int g_ventoy_log_level
= VLOG_DEBUG
;
30 static pthread_mutex_t g_log_mutex
;
32 int ventoy_log_init(void)
34 if (ventoy_get_file_size(g_log_file
) >= SIZE_1MB
)
36 #if defined(_MSC_VER) || defined(WIN32)
37 DeleteFileA(g_log_file
);
43 pthread_mutex_init(&g_log_mutex
, NULL
);
47 void ventoy_log_exit(void)
49 pthread_mutex_destroy(&g_log_mutex
);
52 void ventoy_set_loglevel(int level
)
54 g_ventoy_log_level
= level
;
59 void ventoy_syslog_printf(const char *Fmt
, ...)
68 localtime_r(&stamp
, &ttm
);
71 #if defined(_MSC_VER) || defined(WIN32)
72 vsnprintf_s(log
, 512, _TRUNCATE
, Fmt
, arg
);
74 vsnprintf(log
, 512, Fmt
, arg
);
78 pthread_mutex_lock(&g_log_mutex
);
80 #if defined(_MSC_VER) || defined(WIN32)
81 fopen_s(&fp
, g_log_file
, "a+");
83 fp
= fopen(g_log_file
, "a+");
88 fprintf(fp
, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
89 ttm
.tm_year
, ttm
.tm_mon
+ 1, ttm
.tm_mday
,
90 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
95 printf("[%04u/%02u/%02u %02u:%02u:%02u] %s",
96 ttm
.tm_year
, ttm
.tm_mon
+ 1, ttm
.tm_mday
,
97 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
101 pthread_mutex_unlock(&g_log_mutex
);
104 void ventoy_syslog(int level
, const char *Fmt
, ...)
112 if (level
> g_ventoy_log_level
)
118 localtime_r(&stamp
, &ttm
);
121 #if defined(_MSC_VER) || defined(WIN32)
122 vsnprintf_s(log
, 512, _TRUNCATE
, Fmt
, arg
);
124 vsnprintf(log
, 512, Fmt
, arg
);
128 pthread_mutex_lock(&g_log_mutex
);
129 #if defined(_MSC_VER) || defined(WIN32)
130 fopen_s(&fp
, g_log_file
, "a+");
132 fp
= fopen(g_log_file
, "a+");
136 fprintf(fp
, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
137 ttm
.tm_year
+ 1900, ttm
.tm_mon
+ 1, ttm
.tm_mday
,
138 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
143 printf("[%04u/%02u/%02u %02u:%02u:%02u] %s",
144 ttm
.tm_year
+ 1900, ttm
.tm_mon
+ 1, ttm
.tm_mday
,
145 ttm
.tm_hour
, ttm
.tm_min
, ttm
.tm_sec
,
149 pthread_mutex_unlock(&g_log_mutex
);