]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Core/ventoy_log.c
1.0.36 release
[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 <ventoy_define.h>
30
31 static int g_ventoy_log_level = VLOG_DEBUG;
32 static pthread_mutex_t g_log_mutex;
33
34 int ventoy_log_init(void)
35 {
36 pthread_mutex_init(&g_log_mutex, NULL);
37 return 0;
38 }
39
40 void ventoy_log_exit(void)
41 {
42 pthread_mutex_destroy(&g_log_mutex);
43 }
44
45 void ventoy_set_loglevel(int level)
46 {
47 g_ventoy_log_level = level;
48 }
49
50 void ventoy_syslog_newline(int level, const char *Fmt, ...)
51 {
52 char log[512];
53 va_list arg;
54 time_t stamp;
55 struct tm ttm;
56 FILE *fp;
57
58 if (level > g_ventoy_log_level)
59 {
60 return;
61 }
62
63 time(&stamp);
64 localtime_r(&stamp, &ttm);
65
66 va_start(arg, Fmt);
67 vsnprintf(log, 512, Fmt, arg);
68 va_end(arg);
69
70 pthread_mutex_lock(&g_log_mutex);
71 fp = fopen(VTOY_LOG_FILE, "a+");
72 if (fp)
73 {
74 fprintf(fp, "[%04u/%02u/%02u %02u:%02u:%02u] %s\n",
75 ttm.tm_year, ttm.tm_mon, ttm.tm_mday,
76 ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
77 log);
78 fclose(fp);
79 }
80 pthread_mutex_unlock(&g_log_mutex);
81 }
82
83 void ventoy_syslog_printf(const char *Fmt, ...)
84 {
85 char log[512];
86 va_list arg;
87 time_t stamp;
88 struct tm ttm;
89 FILE *fp;
90
91 time(&stamp);
92 localtime_r(&stamp, &ttm);
93
94 va_start(arg, Fmt);
95 vsnprintf(log, 512, Fmt, arg);
96 va_end(arg);
97
98 pthread_mutex_lock(&g_log_mutex);
99 fp = fopen(VTOY_LOG_FILE, "a+");
100 if (fp)
101 {
102 fprintf(fp, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
103 ttm.tm_year, ttm.tm_mon, ttm.tm_mday,
104 ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
105 log);
106 fclose(fp);
107 }
108 pthread_mutex_unlock(&g_log_mutex);
109 }
110
111 void ventoy_syslog(int level, const char *Fmt, ...)
112 {
113 char log[512];
114 va_list arg;
115 time_t stamp;
116 struct tm ttm;
117 FILE *fp;
118
119 if (level > g_ventoy_log_level)
120 {
121 return;
122 }
123
124 time(&stamp);
125 localtime_r(&stamp, &ttm);
126
127 va_start(arg, Fmt);
128 vsnprintf(log, 512, Fmt, arg);
129 va_end(arg);
130
131 pthread_mutex_lock(&g_log_mutex);
132 fp = fopen(VTOY_LOG_FILE, "a+");
133 if (fp)
134 {
135 fprintf(fp, "[%04u/%02u/%02u %02u:%02u:%02u] %s",
136 ttm.tm_year, ttm.tm_mon, ttm.tm_mday,
137 ttm.tm_hour, ttm.tm_min, ttm.tm_sec,
138 log);
139 fclose(fp);
140 }
141 pthread_mutex_unlock(&g_log_mutex);
142 }
143