Line data Source code
1 1 : /**
2 : * @file log/log.c
3 : *
4 : * @brief Logging library
5 : *
6 : * This library can be used to produce logs during simulation runs.
7 : *
8 : * SPDX-FileCopyrightText: 2008-2021 HPDCS Group <rootsim@googlegroups.com>
9 : * SPDX-License-Identifier: GPL-3.0-only
10 : */
11 : #include <log/log.h>
12 :
13 : #include <arch/io.h>
14 :
15 : #include <stdarg.h>
16 : #include <stdio.h>
17 :
18 : /// The minimum log level of the messages to display
19 1 : int log_level = LOG_LEVEL;
20 : /// If set, uses color codes to color the log outputs
21 1 : bool log_colored;
22 :
23 : /// The textual representations and the color codes of the logging levels
24 : static const struct {
25 0 : const char *name;
26 0 : const char *color;
27 : } levels[] = {
28 : [LOG_TRACE] = {.name = "TRACE", .color = "\x1b[94m"},
29 : [LOG_DEBUG] = {.name = "DEBUG", .color = "\x1b[36m"},
30 : [LOG_INFO] = {.name = "INFO", .color = "\x1b[32m"},
31 : [LOG_WARN] = {.name = "WARN", .color = "\x1b[33m"},
32 : [LOG_ERROR] = {.name = "ERROR", .color = "\x1b[31m"},
33 : [LOG_FATAL] = {.name = "FATAL", .color = "\x1b[35m"}
34 1 : };
35 :
36 : /**
37 : * @brief Logs a message. For internal use: log_log() should be used instead
38 : * @param level the importance level of the message to log
39 : * @param file the file name where this function is being called
40 : * @param line the line number where this function is being called
41 : * @param fmt a printf-style format string for the message to log
42 : * @param ... the list of arguments to fill in the format string @a fmt
43 : */
44 1 : void _log_log(int level, const char *file, unsigned line, const char *fmt, ...)
45 : {
46 : va_list args;
47 :
48 : char time_string[IO_TIME_BUFFER_LEN];
49 : io_local_time_get(time_string);
50 :
51 : if(log_colored) {
52 : fprintf(
53 : stderr,
54 : "%s %s%-5s\x1b[0m \x1b[90m%s:%u:\x1b[0m ",
55 : time_string,
56 : levels[level].color,
57 : levels[level].name,
58 : file,
59 : line
60 : );
61 : } else {
62 : fprintf(
63 : stderr,
64 : "%s %-5s %s:%u: ",
65 : time_string,
66 : levels[level].name,
67 : file,
68 : line
69 : );
70 : }
71 :
72 : va_start(args, fmt);
73 : vfprintf(stderr, fmt, args);
74 : va_end(args);
75 : fprintf(stderr, "\n");
76 : fflush(stderr);
77 : }
78 :
79 : /**
80 : * @brief Prints a fancy ROOT-Sim logo on the terminal
81 : */
82 1 : void log_logo_print(void)
83 : {
84 : if(log_colored) {
85 : fprintf(stderr, "\x1b[94m __ \x1b[90m __ _______ \x1b[94m _ \x1b[90m \n");
86 : fprintf(stderr, "\x1b[94m /__)\x1b[90m/ ) / ) / __ \x1b[94m ( `\x1b[90m . ___ \n");
87 : fprintf(stderr, "\x1b[94m / \\ \x1b[90m(__/ (__/ ( \x1b[94m._)\x1b[90m / / / )\n");
88 : fprintf(stderr, "\x1b[0m\n");
89 : } else {
90 : fprintf(stderr, " __ __ _______ _ \n");
91 : fprintf(stderr, " /__) / ) / ) / __ ( ` . ___ \n");
92 : fprintf(stderr, "/ \\ (__/ (__/ ( ._) / / / )\n");
93 : fprintf(stderr, "\n");
94 : }
95 : }
|