Skip to content

Commit e7a3e95

Browse files
committed
logging?
1 parent a527d81 commit e7a3e95

File tree

3 files changed

+118
-55
lines changed

3 files changed

+118
-55
lines changed

src/Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ GEN_HDR = parser.h boxes.h lex.yy.h
2323
GEN_SRC = parser.c lex.yy.c
2424
GEN_FILES = $(GEN_SRC) $(GEN_HDR)
2525
ORIG_HDRCL = boxes.in.h config.h
26-
ORIG_HDR = $(ORIG_HDRCL) bxstring.h cmdline.h detect.h discovery.h generate.h input.h list.h parsecode.h parsing.h \
27-
query.h regulex.h remove.h shape.h tools.h unicode.h
26+
ORIG_HDR = $(ORIG_HDRCL) bxstring.h cmdline.h detect.h discovery.h generate.h input.h list.h logging.h parsecode.h \
27+
parsing.h query.h regulex.h remove.h shape.h tools.h unicode.h
2828
ORIG_GEN = lexer.l parser.y
29-
ORIG_NORM = boxes.c bxstring.c cmdline.c detect.c discovery.c generate.c input.c list.c parsecode.c parsing.c query.c \
30-
regulex.c remove.c shape.c tools.c unicode.c
29+
ORIG_NORM = boxes.c bxstring.c cmdline.c detect.c discovery.c generate.c input.c list.c logging.c parsecode.c \
30+
parsing.c query.c regulex.c remove.c shape.c tools.c unicode.c
3131
ORIG_SRC = $(ORIG_GEN) $(ORIG_NORM)
3232
ORIG_FILES = $(ORIG_SRC) $(ORIG_HDR)
3333

@@ -116,6 +116,7 @@ generate.o: generate.c generate.h boxes.h shape.h tools.h unicode.h config.h |
116116
input.o: input.c boxes.h input.h regulex.h tools.h unicode.h config.h | check_dir
117117
lex.yy.o: lex.yy.c parser.h boxes.h parsing.h tools.h shape.h unicode.h config.h | check_dir
118118
list.o: list.c list.h boxes.h bxstring.h parsing.h query.h shape.h tools.h unicode.h config.h | check_dir
119+
logging.o: logging.c logging.h tools.h config.h | check_dir
119120
parsecode.o: parsecode.c parsecode.h discovery.h lex.yy.h parsing.h parser.h query.h regulex.h shape.h tools.h unicode.h config.h | check_dir
120121
parser.o: parser.c boxes.h bxstring.h lex.yy.h parsecode.h parser.h parsing.h shape.h tools.h unicode.h config.h | check_dir
121122
parsing.o: parsing.c parsing.h bxstring.h parser.h lex.yy.h boxes.h tools.h config.h | check_dir

src/logging.c

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,89 @@
1414
*/
1515

1616
/*
17-
* Simple makeshift log facility, mostly used for handling debug output.
17+
* Simple makeshift log facility for handling debug output.
1818
*/
1919

20+
#include "config.h"
21+
2022
#include <stdarg.h>
23+
#include <string.h>
24+
2125
#include "tools.h"
2226
#include "logging.h"
2327

24-
log_level_t current_log_level = INFO;
2528

26-
char *log_level_name[] = {
27-
"TRACE", "DEBUG", "INFO", "WARN", "ERROR"
28-
};
29+
static int debug_logging_active = 0;
30+
31+
static int *areas_active = NULL;
2932

3033

31-
void log(log_level_t log_level, const char *module, const char *format, ...)
34+
35+
void activate_debug_logging(log_area_t *log_areas)
3236
{
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);
37+
debug_logging_active = 0;
38+
39+
if (areas_active != NULL) {
40+
BFREE(areas_active);
41+
}
42+
if (log_areas != NULL) {
43+
size_t count = 0;
44+
while (log_areas[count] != RESERVED) {
45+
debug_logging_active = 1;
46+
if (areas_active == NULL) {
47+
areas_active = (int *) calloc(NUM_LOG_AREAS, sizeof(int));
4448
}
45-
break;
46-
47-
case INFO:
48-
if (log_level >= current_log_level) {
49-
bx_fprintf(stdout, s);
49+
if (log_areas[count] == ALL) {
50+
for (size_t i = 0; i < NUM_LOG_AREAS; i++) {
51+
areas_active[i] = 1;
52+
}
5053
}
51-
break;
54+
else {
55+
areas_active[log_areas[count] - 2] = 1;
56+
}
57+
count++;
58+
}
59+
}
60+
}
5261

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);
62+
63+
64+
static int is_area_active(log_area_t log_area)
65+
{
66+
if (debug_logging_active && areas_active != NULL && log_area != RESERVED && log_area < NUM_LOG_AREAS + 2) {
67+
if (log_area == ALL) {
68+
for (size_t i = 0; i < NUM_LOG_AREAS; i++) {
69+
if (!areas_active[i]) {
70+
return 0;
71+
}
5772
}
58-
break;
73+
return 1;
74+
}
75+
return areas_active[log_area - 2];
76+
}
77+
return 0;
78+
}
79+
80+
81+
82+
int is_debug_logging(log_area_t log_area)
83+
{
84+
return (debug_logging_active && is_area_active(log_area)) ? 1 : 0;
85+
}
86+
87+
88+
89+
void log_debug(const char *module, log_area_t log_area, const char *format, ...)
90+
{
91+
if (is_debug_logging(log_area)) {
92+
char *s = (char *) malloc(512);
93+
va_list va;
94+
va_start(va, format);
95+
vsprintf(s, format, va);
96+
va_end(va);
97+
98+
bx_fprintf(stderr, "[DEBUG] %s: %s\n", module, s);
99+
BFREE(s);
59100
}
60101
}
61102

src/logging.h

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,67 @@
1414
*/
1515

1616
/*
17-
* Simple makeshift log facility.
17+
* Simple makeshift log facility for handling debug output.
18+
*
19+
* Debug output is printed on stderr. Each message is annotated with its module and "area".
20+
* The "area" defines certain sections of code as a possible filter criterion.
1821
*/
1922

2023
#ifndef LOGGING_H
2124
#define LOGGING_H
2225

2326

24-
typedef enum _log_level_t {
25-
/** the finest log level */
26-
TRACE,
27-
28-
/** a debug message, should be called `DEBUG` */
29-
DBG,
27+
/** Enum of the log areas supported. `ALL` means all of them, `RESERVED` cannot be used. */
28+
typedef enum _log_area_t {
29+
/** the 0 value is reserved and must not be used */
30+
RESERVED,
31+
32+
/** all areas */
33+
ALL,
34+
35+
/** the main areas of the code (activate this if unsure) */
36+
MAIN,
3037

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;
38+
/** regular expression handling */
39+
REGEXP,
4040

41+
/** parser code */
42+
PARSER,
4143

42-
/** currently active log level, determine whether a message gets printed */
43-
extern log_level_t current_log_level;
44+
/** lexer code */
45+
LEXER,
4446

47+
/** config file discovery */
48+
DISCOVERY
49+
} log_area_t;
4550

46-
/** log level names for printing */
47-
extern char *log_level_name[];
51+
/** number of log areas in `log_area_t`, excluding `ALL` and `RESERVED` */
52+
const size_t NUM_LOG_AREAS = 5;
53+
54+
55+
/**
56+
* Activate debug logging.
57+
* @param log_areas an array of log areas to activate, zero-terminated, should not be empty. The value will by copied
58+
* and can be freed after returning from the function.
59+
*/
60+
void activate_debug_logging(log_area_t *log_areas);
61+
62+
63+
/**
64+
* Determine if debug logging is active and the given area is active, too.
65+
* @param log_area the log area to check
66+
* @return 1 if active, 0 if not
67+
*/
68+
int is_debug_logging(log_area_t log_area);
4869

4970

5071
/**
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
72+
* Print a debug log message. Debug log messages will be printed on `stderr`.
5373
* @param module the module name as per `__FILE__`
74+
* @param log_area_t log_area
5475
* @param format format string for the mesage, followed by the placeholder arguments
5576
*/
56-
void log(log_level_t log_level, const char *module, const char *format, ...);
77+
void log_debug(const char *module, log_area_t log_area, const char *format, ...);
5778

5879

5980
#endif

0 commit comments

Comments
 (0)