-
Notifications
You must be signed in to change notification settings - Fork 4
/
log.c
78 lines (68 loc) · 2.44 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
/*
Logging infrastructure
*/
/*
SPDX-License-Identifier: MIT
Copyright (c) 2024 Ewen McNeill <[email protected]>
*/
#include "log.h"
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
// Default log levels for log sources
#define LOG_UART_DEFAULT_LEVEL LOG_WARNING
#define LOG_CSR_DEFAULT_LEVEL LOG_WARNING
#define LOG_MBUS_DEFAULT_LEVEL LOG_WARNING
#define LOG_MAPPER_DEFAULT_LEVEL LOG_WARNING
#define LOG_SCSI_DEFAULT_LEVEL LOG_WARNING
#define LOG_EMU_DEFAULT_LEVEL LOG_WARNING
#define LOG_RAMROM_DEFAULT_LEVEL LOG_WARNING
#define LOG_RTC_DEFAULT_LEVEL LOG_WARNING
#define LOG_EMU_DEFAULT_LEVEL LOG_WARNING
#define LOG_STRACE_DEFAULT_LEVEL LOG_INFO //keep here, we have a separate switch for this
int log_channel_verbose_level[] = {
[LOG_SRC_UART]=LOG_UART_DEFAULT_LEVEL,
[LOG_SRC_CSR]=LOG_CSR_DEFAULT_LEVEL,
[LOG_SRC_MBUS]=LOG_MBUS_DEFAULT_LEVEL,
[LOG_SRC_MAPPER]=LOG_MAPPER_DEFAULT_LEVEL,
[LOG_SRC_SCSI]=LOG_SCSI_DEFAULT_LEVEL,
[LOG_SRC_RAMROM]=LOG_RAMROM_DEFAULT_LEVEL,
[LOG_SRC_RTC]=LOG_RTC_DEFAULT_LEVEL,
[LOG_SRC_EMU]=LOG_EMU_DEFAULT_LEVEL,
[LOG_SRC_STRACE]=LOG_STRACE_DEFAULT_LEVEL
};
// "ANSI" colour escape sequences
#define ANSI_COLOUR_NORMAL "\033[0m"
#define ANSI_COLOUR_RED "\033[31m"
#define ANSI_COLOUR_GREEN "\033[32m"
#define ANSI_COLOUR_YELLOW "\033[33m"
#define ANSI_COLOUR_MAGENTA "\033[35m"
#define ANSI_COLOUR_GREY "\033[37m"
// These must be in teh same order as enum log_level (in log.h)
const char *log_level_colour[] = {
ANSI_COLOUR_RED, // LOG_ERR (0)
ANSI_COLOUR_MAGENTA, // LOG_WARNING (1)
ANSI_COLOUR_YELLOW, // LOG_NOTICE (2)
ANSI_COLOUR_GREEN, // LOG_INFO (3)
ANSI_COLOUR_GREY // LOG_DEBUG (4)
};
void log_set_level(enum log_source source, enum log_level msg_level) {
log_channel_verbose_level[source]=msg_level;
}
int log_level_active(enum log_source source, enum log_level msg_level) {
return log_channel_verbose_level[source] >= msg_level;
}
int log_printf(enum log_source source, enum log_level msg_level, const char *format, ...) {
static_assert(sizeof(log_channel_verbose_level)/sizeof(log_channel_verbose_level[0])==LOG_SRC_MAX,
"log_channel_verbose_level missing an entry");
va_list ap;
int printed = 0;
if (log_channel_verbose_level[source] >= msg_level) {
printf("%s", log_level_colour[msg_level]);
va_start(ap, format);
printed = vprintf(format, ap);
va_end(ap);
printf("%s", ANSI_COLOUR_NORMAL);
}
return printed;
}