forked from rayylee/kvm-dmesg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
118 lines (94 loc) · 2.33 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* log.c
*
* Copyright (C) 2024 Ray Lee
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "log.h"
int loglevel = LOGLEVEL_WARNING;
void log_init(int level)
{
if (level > 0 && level < LOGLEVEL_MAX)
loglevel = level;
}
static void report(const char *prefix, const char *err, va_list params)
{
char msg[1024];
vsnprintf(msg, sizeof(msg), err, params);
fprintf(stderr, "%s%s\n", prefix, msg);
}
static void debug_builtin(const char *debug, va_list params)
{
report("[Debug] ", debug, params);
}
static void info_builtin(const char *info, va_list params)
{
report("[Info] ", info, params);
}
static void warn_builtin(const char *warn, va_list params)
{
report("[Warning] ", warn, params);
}
static void error_builtin(const char *err, va_list params)
{
report("[Error] ", err, params);
}
static void die_builtin(const char *err, va_list params)
{
report("[Fatal] ", err, params);
exit(128);
}
void __pr_debug(const char *debug, ...)
{
va_list params;
if (loglevel < LOGLEVEL_DEBUG)
return;
va_start(params, debug);
debug_builtin(debug, params);
va_end(params);
}
void pr_info(const char *info, ...)
{
va_list params;
if (loglevel < LOGLEVEL_INFO)
return;
va_start(params, info);
info_builtin(info, params);
va_end(params);
}
void pr_warning(const char *warn, ...)
{
va_list params;
if (loglevel < LOGLEVEL_WARNING)
return;
va_start(params, warn);
warn_builtin(warn, params);
va_end(params);
}
void pr_err(const char *err, ...)
{
va_list params;
if (loglevel < LOGLEVEL_ERROR)
return;
va_start(params, err);
error_builtin(err, params);
va_end(params);
}
void die(const char *err, ...)
{
va_list params;
va_start(params, err);
die_builtin(err, params);
va_end(params);
}