]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Core/ventoy_log.c
Disable Fn/L/Ctrl hotkeys on select WIM menu.
[Ventoy.git] / LinuxGUI / Ventoy2Disk / 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 <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <pthread.h>
28 #include <time.h>
29 #include <linux/limits.h>
30 #include <ventoy_define.h>
31
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;
35
36 int ventoy_log_init(void)
37 {
38 pthread_mutex_init(&g_log_mutex, NULL);
39 return 0;
40 }
41
42 void ventoy_log_exit(void)
43 {
44 pthread_mutex_destroy(&g_log_mutex);
45 }
46
47 void ventoy_set_loglevel(int level)
48 {
49 g_ventoy_log_level = level;
50 }
51
52 void ventoy_syslog_newline(int level, const char *Fmt, ...)
53 {
54 char log[512];
55 va_list arg;
56 time_t stamp;
57 struct tm ttm;
58 FILE *fp;
59
60 if (level > g_ventoy_log_level)
61 {
62 return;
63 }
64
65 time(&stamp);
66 localtime_r(&stamp, &ttm);
67
68 va_start(arg, Fmt);
69 vsnprintf(log, 512, Fmt, arg);
70 va_end(arg);
71
72 pthread_mutex_lock(&g_log_mutex);
73 fp = fopen(g_log_file, "a+");
74 if (fp)
75 {
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,
79 log);
80 fclose(fp);
81 }
82 pthread_mutex_unlock(&g_log_mutex);
83 }
84
85 void ventoy_syslog_printf(const char *Fmt, ...)
86 {
87 char log[512];
88 va_list arg;
89 time_t stamp;
90 struct tm ttm;
91 FILE *fp;
92
93 time(&stamp);
94 localtime_r(&stamp, &ttm);
95
96 va_start(arg, Fmt);
97 vsnprintf(log, 512, Fmt, arg);
98 va_end(arg);
99
100 pthread_mutex_lock(&g_log_mutex);
101 fp = fopen(g_log_file, "a+");
102 if (fp)
103 {
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,
107 log);
108 fclose(fp);
109 }
110 pthread_mutex_unlock(&g_log_mutex);
111 }
112
113 void ventoy_syslog(int level, const char *Fmt, ...)
114 {
115 char log[512];
116 va_list arg;
117 time_t stamp;
118 struct tm ttm;
119 FILE *fp;
120
121 if (level > g_ventoy_log_level)
122 {
123 return;
124 }
125
126 time(&stamp);
127 localtime_r(&stamp, &ttm);
128
129 va_start(arg, Fmt);
130 vsnprintf(log, 512, Fmt, arg);
131 va_end(arg);
132
133 pthread_mutex_lock(&g_log_mutex);
134 fp = fopen(g_log_file, "a+");
135 if (fp)
136 {
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,
140 log);
141 fclose(fp);
142 }
143 pthread_mutex_unlock(&g_log_mutex);
144 }
145