Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-only */
2 : /*
3 : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #include "core/logger.h"
7 :
8 : #include <stdbool.h>
9 : #include <stdio.h>
10 : #include <unistd.h>
11 :
12 : /// If true, log messages will be printed in colors.
13 : static bool _bf_can_print_color = false;
14 :
15 : /** Default log level for log messages. All the logs below this level will
16 : * be ignored (not printed). */
17 : static enum bf_log_level _bf_log_level = BF_LOG_INFO;
18 :
19 0 : void bf_logger_setup(void)
20 : {
21 0 : _bf_can_print_color = isatty(fileno(stdout)) && isatty(fileno(stderr));
22 0 : }
23 :
24 70 : const char *bf_logger_get_color(enum bf_color color, enum bf_style style)
25 : {
26 70 : if (!_bf_can_print_color) {
27 : return "";
28 : }
29 :
30 0 : switch (color) {
31 0 : case BF_COLOR_DEFAULT:
32 0 : return (style == BF_STYLE_BOLD) ? "\033[1;39m" : "\033[0;39m";
33 0 : case BF_COLOR_BLACK:
34 0 : return (style == BF_STYLE_BOLD) ? "\033[1;30m" : "\033[0;30m";
35 0 : case BF_COLOR_RED:
36 0 : return (style == BF_STYLE_BOLD) ? "\033[1;31m" : "\033[0;31m";
37 0 : case BF_COLOR_GREEN:
38 0 : return (style == BF_STYLE_BOLD) ? "\033[1;32m" : "\033[0;32m";
39 0 : case BF_COLOR_YELLOW:
40 0 : return (style == BF_STYLE_BOLD) ? "\033[1;33m" : "\033[0;33m";
41 0 : case BF_COLOR_BLUE:
42 0 : return (style == BF_STYLE_BOLD) ? "\033[1;34m" : "\033[0;34m";
43 0 : case BF_COLOR_MAGENTA:
44 0 : return (style == BF_STYLE_BOLD) ? "\033[1;35m" : "\033[0;35m";
45 0 : case BF_COLOR_CYAN:
46 0 : return (style == BF_STYLE_BOLD) ? "\033[1;36m" : "\033[0;36m";
47 0 : case BF_COLOR_LIGHT_GRAY:
48 0 : return (style == BF_STYLE_BOLD) ? "\033[1;37m" : "\033[0;37m";
49 0 : case BF_COLOR_DARK_GRAY:
50 0 : return (style == BF_STYLE_BOLD) ? "\033[1;90m" : "\033[0;90m";
51 0 : case BF_COLOR_LIGHT_RED:
52 0 : return (style == BF_STYLE_BOLD) ? "\033[1;91m" : "\033[0;91m";
53 0 : case BF_COLOR_LIGHT_GREEN:
54 0 : return (style == BF_STYLE_BOLD) ? "\033[1;92m" : "\033[0;92m";
55 0 : case BF_COLOR_LIGHT_YELLOW:
56 0 : return (style == BF_STYLE_BOLD) ? "\033[1;93m" : "\033[0;93m";
57 0 : case BF_COLOR_LIGHT_BLUE:
58 0 : return (style == BF_STYLE_BOLD) ? "\033[1;94m" : "\033[0;94m";
59 0 : case BF_COLOR_LIGHT_MAGENTA:
60 0 : return (style == BF_STYLE_BOLD) ? "\033[1;95m" : "\033[0;95m";
61 0 : case BF_COLOR_LIGHT_CYAN:
62 0 : return (style == BF_STYLE_BOLD) ? "\033[1;96m" : "\033[0;96m";
63 0 : case BF_COLOR_WHITE:
64 0 : return (style == BF_STYLE_BOLD) ? "\033[1;97m" : "\033[0;97m";
65 : default:
66 : return "\033[0m";
67 : }
68 : }
69 :
70 43 : enum bf_log_level bf_log_get_level(void)
71 : {
72 43 : return _bf_log_level;
73 : }
74 :
75 0 : void bf_log_set_level(enum bf_log_level level)
76 : {
77 0 : _bf_log_level = level;
78 0 : }
79 :
80 : static const char *_bf_log_level_strs[] = {
81 : [BF_LOG_DBG] = "debug", [BF_LOG_INFO] = "info", [BF_LOG_WARN] = "warning",
82 : [BF_LOG_ERR] = "error", [BF_LOG_ABORT] = "abort",
83 : };
84 : static_assert(ARRAY_SIZE(_bf_log_level_strs) == _BF_LOG_MAX,
85 : "missing entries in _bf_log_level_strs strings array");
86 :
87 35 : const char *bf_log_level_to_str(enum bf_log_level level)
88 : {
89 35 : return _bf_log_level_strs[level];
90 : }
91 :
92 0 : enum bf_log_level bf_log_level_from_str(const char *str)
93 : {
94 0 : bf_assert(str);
95 :
96 0 : for (enum bf_log_level level = 0; level < _BF_LOG_MAX; ++level) {
97 0 : if (bf_streq(_bf_log_level_strs[level], str))
98 : return level;
99 : }
100 :
101 : return -EINVAL;
102 : }
|