]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_log.c
1.0.96 release
[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 void ventoy_syslog_printf(const char *Fmt, ...)
58 {
59 char log[512];
60 va_list arg;
61 time_t stamp;
62 struct tm ttm;
63 FILE *fp;
64
65 time(&stamp);
66 localtime_r(&stamp, &ttm);
67
68 va_start(arg, Fmt);
69 #if defined(_MSC_VER) || defined(WIN32)
70 vsnprintf_s(log, 512, _TRUNCATE, Fmt, arg);
71 #else
72 vsnprintf(log, 512, Fmt, arg);
73 #endif
74 va_end(arg);
75
76 pthread_mutex_lock(&g_log_mutex);
77
78 #if defined(_MSC_VER) || defined(WIN32)
79 fopen_s(&fp, g_log_file, "a+");
80 #else
81 fp = fopen(g_log_file, "a+");
82 #endif
83
84 if (fp)
85 {
86 fprintf(fp, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
87 ttm.tm_year, ttm.tm_mon + 1, ttm.tm_mday,
88 ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
89 log);
90 fclose(fp);
91
92 #ifdef VENTOY_SIM
93 printf("[%04u/%02u/%02u %02u:%02u:%02u] %s",
94 ttm.tm_year, ttm.tm_mon + 1, ttm.tm_mday,
95 ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
96 log);
97 #endif
98 }
99 pthread_mutex_unlock(&g_log_mutex);
100 }
101
102 void ventoy_syslog(int level, const char *Fmt, ...)
103 {
104 char log[512];
105 va_list arg;
106 time_t stamp;
107 struct tm ttm;
108 FILE *fp;
109
110 if (level > g_ventoy_log_level)
111 {
112 return;
113 }
114
115 time(&stamp);
116 localtime_r(&stamp, &ttm);
117
118 va_start(arg, Fmt);
119 #if defined(_MSC_VER) || defined(WIN32)
120 vsnprintf_s(log, 512, _TRUNCATE, Fmt, arg);
121 #else
122 vsnprintf(log, 512, Fmt, arg);
123 #endif
124 va_end(arg);
125
126 pthread_mutex_lock(&g_log_mutex);
127 #if defined(_MSC_VER) || defined(WIN32)
128 fopen_s(&fp, g_log_file, "a+");
129 #else
130 fp = fopen(g_log_file, "a+");
131 #endif
132 if (fp)
133 {
134 fprintf(fp, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
135 ttm.tm_year + 1900, ttm.tm_mon + 1, ttm.tm_mday,
136 ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
137 log);
138 fclose(fp);
139
140 #ifdef VENTOY_SIM
141 printf("[%04u/%02u/%02u %02u:%02u:%02u] %s",
142 ttm.tm_year + 1900, ttm.tm_mon + 1, ttm.tm_mday,
143 ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
144 log);
145 #endif
146 }
147 pthread_mutex_unlock(&g_log_mutex);
148 }
149