Skip to content

Commit a527d81

Browse files
committed
logging?
1 parent ce44ba6 commit a527d81

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

src/logging.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* boxes - Command line filter to draw/remove ASCII boxes around text
3+
* Copyright (c) 1999-2023 Thomas Jensen and the boxes contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
6+
* License, version 3, as published by the Free Software Foundation.
7+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
8+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
9+
* details.
10+
* You should have received a copy of the GNU General Public License along with this program.
11+
* If not, see <https://www.gnu.org/licenses/>.
12+
*
13+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
14+
*/
15+
16+
/*
17+
* Simple makeshift log facility, mostly used for handling debug output.
18+
*/
19+
20+
#include <stdarg.h>
21+
#include "tools.h"
22+
#include "logging.h"
23+
24+
log_level_t current_log_level = INFO;
25+
26+
char *log_level_name[] = {
27+
"TRACE", "DEBUG", "INFO", "WARN", "ERROR"
28+
};
29+
30+
31+
void log(log_level_t log_level, const char *module, const char *format, ...)
32+
{
33+
char *s = (char *) malloc(512);
34+
va_list va;
35+
va_start(va, format);
36+
vsprintf(s, format, va);
37+
va_end(va);
38+
39+
switch(log_level) {
40+
case TRACE:
41+
case DBG:
42+
if (log_level >= current_log_level) {
43+
bx_fprintf(stderr, "%5s %s: %s\n", log_level_name[log_level], module, s);
44+
}
45+
break;
46+
47+
case INFO:
48+
if (log_level >= current_log_level) {
49+
bx_fprintf(stdout, s);
50+
}
51+
break;
52+
53+
case WARN:
54+
case ERROR:
55+
if (log_level >= current_log_level) {
56+
bx_fprintf(stderr, "%5s: %s\n", log_level_name[log_level], s);
57+
}
58+
break;
59+
}
60+
}
61+
62+
63+
/* vim: set cindent sw=4: */

src/logging.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* boxes - Command line filter to draw/remove ASCII boxes around text
3+
* Copyright (c) 1999-2023 Thomas Jensen and the boxes contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
6+
* License, version 3, as published by the Free Software Foundation.
7+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
8+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
9+
* details.
10+
* You should have received a copy of the GNU General Public License along with this program.
11+
* If not, see <https://www.gnu.org/licenses/>.
12+
*
13+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
14+
*/
15+
16+
/*
17+
* Simple makeshift log facility.
18+
*/
19+
20+
#ifndef LOGGING_H
21+
#define LOGGING_H
22+
23+
24+
typedef enum _log_level_t {
25+
/** the finest log level */
26+
TRACE,
27+
28+
/** a debug message, should be called `DEBUG` */
29+
DBG,
30+
31+
/** normal informational message */
32+
INFO,
33+
34+
/** a warning message, which should not happen, but can be tolerated */
35+
WARN,
36+
37+
/** an error message, normally this will also lead to abnormal program termination */
38+
ERROR
39+
} log_level_t;
40+
41+
42+
/** currently active log level, determine whether a message gets printed */
43+
extern log_level_t current_log_level;
44+
45+
46+
/** log level names for printing */
47+
extern char *log_level_name[];
48+
49+
50+
/**
51+
* Print a log message. Log messages of level INFO will be printed in `stdout`, others on `stderr`.
52+
* @param log_level the log level
53+
* @param module the module name as per `__FILE__`
54+
* @param format format string for the mesage, followed by the placeholder arguments
55+
*/
56+
void log(log_level_t log_level, const char *module, const char *format, ...);
57+
58+
59+
#endif
60+
61+
/* vim: set cindent sw=4: */

0 commit comments

Comments
 (0)