]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_log.c
VentoyPlugson: Add Windows duplicate file path check for different upper/lower case.
[Ventoy.git] / Plugson / src / Core / ventoy_log.c
1 /******************************************************************************
2 * ventoy_log.c ---- ventoy log
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <time.h>
25 #include <ventoy_define.h>
26 #include <ventoy_util.h>
27
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;
31
32 int ventoy_log_init(void)
33 {
34 if (ventoy_get_file_size(g_log_file) >= SIZE_1MB)
35 {
36 #if defined(_MSC_VER) || defined(WIN32)
37 DeleteFileA(g_log_file);
38 #else
39 remove(g_log_file);
40 #endif
41 }
42
43 pthread_mutex_init(&g_log_mutex, NULL);
44 return 0;
45 }
46
47 void ventoy_log_exit(void)
48 {
49 pthread_mutex_destroy(&g_log_mutex);
50 }
51
52 void ventoy_set_loglevel(int level)
53 {
54 g_ventoy_log_level = level;
55 }
56
57
58
59 void ventoy_syslog_printf(const char *Fmt, ...)
60 {
61 char log[512];
62 va_list arg;
63 time_t stamp;
64 struct tm ttm;
65 FILE *fp;
66
67 time(&stamp);
68 localtime_r(&stamp, &ttm);
69
70 va_start(arg, Fmt);
71 #if defined(_MSC_VER) || defined(WIN32)
72 vsnprintf_s(log, 512, _TRUNCATE, Fmt, arg);
73 #else
74 vsnprintf(log, 512, Fmt, arg);
75 #endif
76 va_end(arg);
77
78 pthread_mutex_lock(&g_log_mutex);
79
80 #if defined(_MSC_VER) || defined(WIN32)
81 fopen_s(&fp, g_log_file, "a+");
82 #else
83 fp = fopen(g_log_file, "a+");
84 #endif
85
86 if (fp)
87 {
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,
91 log);
92 fclose(fp);
93
94 #ifdef VENTOY_SIM
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,
98 log);
99 #endif
100 }
101 pthread_mutex_unlock(&g_log_mutex);
102 }
103
104 void ventoy_syslog(int level, const char *Fmt, ...)
105 {
106 char log[512];
107 va_list arg;
108 time_t stamp;
109 struct tm ttm;
110 FILE *fp;
111
112 if (level > g_ventoy_log_level)
113 {
114 return;
115 }
116
117 time(&stamp);
118 localtime_r(&stamp, &ttm);
119
120 va_start(arg, Fmt);
121 #if defined(_MSC_VER) || defined(WIN32)
122 vsnprintf_s(log, 512, _TRUNCATE, Fmt, arg);
123 #else
124 vsnprintf(log, 512, Fmt, arg);
125 #endif
126 va_end(arg);
127
128 pthread_mutex_lock(&g_log_mutex);
129 #if defined(_MSC_VER) || defined(WIN32)
130 fopen_s(&fp, g_log_file, "a+");
131 #else
132 fp = fopen(g_log_file, "a+");
133 #endif
134 if (fp)
135 {
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,
139 log);
140 fclose(fp);
141
142 #ifdef VENTOY_SIM
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,
146 log);
147 #endif
148 }
149 pthread_mutex_unlock(&g_log_mutex);
150 }
151