-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog.c
More file actions
179 lines (166 loc) · 4.58 KB
/
log.c
File metadata and controls
179 lines (166 loc) · 4.58 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef _AMALGAMATE_
#include "basedefs.h"
#include "log.h"
#include "xstr.h"
#include "alloc.h"
#include "utils.h"
#include "config.h"
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#endif
#define LEVEL_VERBOSE 0
#define LEVEL_INFO 1
#define LEVEL_WARN 2
#define LEVEL_ERROR 3
#define LEVEL_FATAL 4
static const char* _error_get(int code) {
switch (code) {
case AK_ERROR_OVERFLOW:
return "Value/argument overflow. (AK_ERROR_OVERFLOW)";
case AK_ERROR_ASSERTION:
return "Assertion failed (AK_ERROR_ASSERTION)";
case AK_ERROR_INVALID_ARGS:
return "Invalid function arguments (AKL_ERROR_INVALID_ARGS)";
case AK_ERROR_FAIL:
return "Fail (AK_ERROR_FAIL)";
case AK_ERROR_IO:
return "IO Error (AK_ERROR_IO)";
case AK_ERROR_UNIMPLEMETED:
return "Not implemented (AK_ERROR_UNIMPLEMETED)";
case AK_ERROR_SCRIPT_SYNTAX:
return "Invalid autark config syntax (AK_ERROR_SCRIPT_SYNTAX)";
case AK_ERROR_SCRIPT:
return "Autark script error (AK_ERROR_SCRIPT)";
case AK_ERROR_CYCLIC_BUILD_DEPS:
return "Detected cyclic build dependency (AK_ERROR_CYCLIC_BUILD_DEPS)";
case AK_ERROR_SCRIPT_ERROR:
return "Build script error (AK_ERROR_SCRIPT_ERROR)";
case AK_ERROR_NOT_IMPLEMENTED:
return "Not implemented (AK_ERROR_NOT_IMPLEMENTED)";
case AK_ERROR_EXTERNAL_COMMAND:
return "External command failed (AK_ERROR_EXTERNAL_COMMAND)";
case AK_ERROR_DEPENDENCY_UNRESOLVED:
return "I don't know how to build dependency (AK_ERROR_DEPENDENCY_UNRESOLVED)";
case AK_ERROR_MACRO_MAX_RECURSIVE_CALLS:
return "Max number of recursive macro calls reached: "
Q(MACRO_MAX_RECURSIVE_CALLS) " (AK_ERROR_MACRO_MAX_RECURSIVE_CALLS)";
case AK_ERROR_OK:
return "OK";
default:
return strerror(code);
}
}
static char* _log_basename(char *path) {
size_t i;
if (!path || *path == '\0') {
return ".";
}
i = strlen(path) - 1;
for ( ; i && path[i] == '/'; i--) path[i] = 0;
for ( ; i && path[i - 1] != '/'; i--) ;
return path + i;
}
static void _event_va(
int level,
const char *file,
int line,
int code,
const char *format,
va_list va) {
struct xstr *xstr = 0;
xstr = xstr_create_empty();
switch (level) {
case LEVEL_FATAL:
xstr_cat2(xstr, "FATAL ", LLEN("FATAL "));
break;
case LEVEL_INFO:
xstr_cat2(xstr, "INFO ", LLEN("INFO "));
break;
case LEVEL_ERROR:
xstr_cat2(xstr, "ERROR ", LLEN("ERROR "));
break;
case LEVEL_WARN:
xstr_cat2(xstr, "WARN ", LLEN("WARN "));
break;
case LEVEL_VERBOSE:
xstr_cat2(xstr, "VERBOSE ", LLEN("VERBOSE "));
break;
default:
xstr_cat2(xstr, "XXX ", LLEN("XXX "));
break;
}
if (file) {
char *cfile = xstrdup(file);
if (cfile) {
char *p = _log_basename(cfile);
xstr_printf(xstr, "%s:%d ", p, line);
free(cfile);
}
}
if (code) {
const char *err = _error_get(code);
xstr_printf(xstr, "%d|", code);
if (err) {
xstr_cat(xstr, err);
}
}
if (format) {
if (code) {
xstr_cat2(xstr, "|", 1);
}
xstr_printf_va(xstr, format, va);
}
xstr_cat2(xstr, "\n", 1);
utils_chars_replace(xstr_ptr(xstr), '\1', '^');
fputs(xstr_ptr(xstr), stderr);
xstr_destroy(xstr);
}
void akfatal2(const char *msg) {
if (msg) {
fputs(msg, stderr);
fputc('\n', stderr);
}
_exit(254);
}
void _akfatal(const char *file, int line, int code, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
_event_va(LEVEL_FATAL, file, line, code, fmt, ap);
va_end(ap);
akfatal2(0);
}
int _akerror(const char *file, int line, int code, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
_event_va(LEVEL_ERROR, file, line, code, fmt, ap);
va_end(ap);
return code;
}
void _akverbose(const char *file, int line, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
_event_va(LEVEL_VERBOSE, file, line, 0, fmt, ap);
va_end(ap);
}
void akinfo(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
_event_va(LEVEL_INFO, 0, 0, 0, fmt, ap);
va_end(ap);
}
void akwarn(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
_event_va(LEVEL_WARN, 0, 0, 0, fmt, ap);
va_end(ap);
}
void _akerror_va(const char *file, int line, int code, const char *fmt, va_list ap) {
_event_va(LEVEL_ERROR, file, line, code, fmt, ap);
}
void _akfatal_va(const char *file, int line, int code, const char *fmt, va_list ap) {
_event_va(LEVEL_FATAL, file, line, code, fmt, ap);
akfatal2(0);
}