-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathkpatch_log.c
More file actions
106 lines (85 loc) · 1.88 KB
/
kpatch_log.c
File metadata and controls
106 lines (85 loc) · 1.88 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "kpatch_log.h"
int log_level = LOG_INFO;
int log_indent;
static void __valog(int level, const char *prefix, const char *fmt, va_list va)
{
FILE *f = level <= LOG_WARN ? stderr : stdout;
if (prefix)
fprintf(f, "%s", prefix);
vfprintf(f, fmt, va);
}
void kplog(int level, const char *fmt, ...)
{
int errno_sv;
va_list va;
char indent[8];
if (level > log_level)
return;
errno_sv = errno;
va_start(va, fmt);
memset(indent, ' ', sizeof(indent));
indent[log_indent] = 0;
__valog(level, indent, fmt, va);
#if 0
if (fmt[0] == '+')
log_indent++;
else if (fmt[0] == '-')
log_indent--;
#endif
va_end(va);
errno = errno_sv;
}
void kpfatal(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
__valog(LOG_ERR, "FATAL! ", fmt, va);
va_end(va);
#ifdef STRESS_TEST
if (parent_pid >= 0)
stress_test_notify_parent();
#endif
exit(ERROR_FATAL);
}
extern int elf_errno(void) __attribute__((weak));
extern const char *elf_errmsg(int err) __attribute((weak));
void __valogerror(const char *file, int line, const char *fmt, va_list va)
{
int errno_sv = errno;
fprintf(stderr, "%s(%d): ", file, line);
__valog(LOG_ERR, NULL, fmt, va);
if (errno_sv) {
fprintf(stderr, "errno = %d, msg = '%s'\n",
errno, strerror(errno));
}
#ifndef __MACH__
if (elf_errno && elf_errmsg) {
int errno_elf = elf_errno();
if (errno_elf) {
fprintf(stderr, "errno_elf = %d, msg = '%s'\n",
errno_elf, elf_errmsg(errno_elf));
}
}
#endif
errno = errno_sv;
}
void _kplogerror(const char *file, int line, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
__valogerror(file, line, fmt, va);
va_end(va);
}
void _kpfatalerror(const char *file, int line, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
__valogerror(file, line, fmt, va);
va_end(va);
exit(ERROR_FATAL);
}