MOS Source Code
Loading...
Searching...
No Matches
printk.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4
9#include <mos/misc/setup.hpp>
10#include <mos/syslog/printk.hpp>
11#include <mos_stdio.hpp>
12#include <mos_string.hpp>
13
15static bool printk_quiet = false;
16
17MOS_SETUP("printk_console", printk_setup_console)
18{
19 const auto kcon_name = arg;
20 if (kcon_name.empty())
21 {
22 pr_warn("No console name given for printk");
23 return false;
24 }
25
26 auto maybe_console = console_get(kcon_name);
27 if (maybe_console)
28 {
29 pr_emph("Selected console '%s' for future printk\n", kcon_name);
30 printk_console = *maybe_console;
31 return true;
32 }
33
34 maybe_console = console_get_by_prefix(kcon_name);
35 if (maybe_console)
36 {
37 printk_console = *maybe_console;
38 pr_emph("Selected console '%s' for future printk (prefix-based)\n", printk_console->name().c_str());
39 return true;
40 }
41
42 mos_warn("No console found for printk based on given name or prefix '%s'", kcon_name.data());
44 return false;
45}
46
47MOS_EARLY_SETUP("quiet", printk_setup_quiet)
48{
50 return true;
51}
52
53static inline void deduce_level_color(LogLevel loglevel, StandardColor *fg, StandardColor *bg)
54{
55 *bg = Black;
56 switch (loglevel)
57 {
58 case LogLevel::INFO2: *fg = DarkGray; break;
59 case LogLevel::INFO: *fg = Gray; break;
60 case LogLevel::EMPH: *fg = Cyan; break;
61 case LogLevel::WARN: *fg = Brown; break;
62 case LogLevel::EMERG: *fg = Red; break;
63 case LogLevel::FATAL: *fg = White, *bg = Red; break;
64 default: break; // do not change the color
65 }
66}
67
68void print_to_console(Console *con, LogLevel loglevel, const char *message, size_t len)
69{
70 if (!con)
71 return;
72
73 StandardColor fg = con->fg, bg = con->bg;
74 deduce_level_color(loglevel, &fg, &bg);
75 con->WriteColored(message, len, fg, bg);
76}
77
78void lvprintk(LogLevel loglevel, const char *fmt, va_list args)
79{
80 // only print warnings and errors if quiet mode is enabled
81 if (printk_quiet && loglevel < LogLevel::WARN)
82 return;
83
84 char message[MOS_PRINTK_BUFFER_SIZE];
85 const int len = vsnprintf(message, MOS_PRINTK_BUFFER_SIZE, fmt, args);
86
88 printk_console = consoles.front();
89 print_to_console(printk_console, loglevel, message, len);
90}
91
93{
94 bool was_quiet = printk_quiet;
95 printk_quiet = false;
96 return was_quiet;
97}
98
99void printk_set_quiet(bool quiet)
100{
101 printk_quiet = quiet;
102}
103
104void lprintk(LogLevel loglevel, const char *format, ...)
105{
106 va_list args;
107 va_start(args, format);
108 lvprintk(loglevel, format, args);
109 va_end(args);
110}
111
112void printk(const char *format, ...)
113{
114 va_list args;
115 va_start(args, format);
117 va_end(args);
118}
#define mos_warn(fmt,...)
Definition assert.hpp:22
#define MOS_PRINTK_BUFFER_SIZE
Definition autoconf.h:19
char args[3][16]
Definition avr_io.c:16
std::optional< Console * > console_get(mos::string_view name)
Definition console.cpp:23
std::array< Console *, 128 > consoles
Definition console.cpp:20
std::optional< Console * > console_get_by_prefix(mos::string_view prefix)
Definition console.cpp:32
StandardColor
Definition ansi_colors.h:18
@ DarkGray
Definition ansi_colors.h:27
@ Gray
Definition ansi_colors.h:26
@ Brown
Definition ansi_colors.h:25
@ Cyan
Definition ansi_colors.h:22
@ White
Definition ansi_colors.h:34
@ Black
Definition ansi_colors.h:19
@ Red
Definition ansi_colors.h:23
MOSAPI int vsnprintf(char *__restrict buf, size_t size, const char *__restrict format, va_list args)
MOSAPI const char *__restrict format
Definition mos_stdio.hpp:17
bool cmdline_string_truthiness(mos::string_view arg, bool default_value)
Definition cmdline.cpp:83
#define unlikely(x)
Definition mos_global.h:40
#define NULL
Definition pb_syshdr.h:46
bool printk_unquiet(void)
Definition printk.cpp:92
void print_to_console(Console *con, LogLevel loglevel, const char *message, size_t len)
Definition printk.cpp:68
static void deduce_level_color(LogLevel loglevel, StandardColor *fg, StandardColor *bg)
Definition printk.cpp:53
void printk_set_quiet(bool quiet)
Definition printk.cpp:99
void printk(const char *format,...)
Definition printk.cpp:112
static bool printk_quiet
Definition printk.cpp:15
void lvprintk(LogLevel loglevel, const char *fmt, va_list args)
Definition printk.cpp:78
void lprintk(LogLevel loglevel, const char *format,...)
Definition printk.cpp:104
#define pr_warn(fmt,...)
Definition printk.hpp:38
#define pr_emph(fmt,...)
Definition printk.hpp:37
#define MOS_EARLY_SETUP(_param, _fn)
Definition setup.hpp:29
#define MOS_SETUP(_param, _fn)
Definition setup.hpp:34
size_t WriteColored(const char *data, size_t size, StandardColor fg, StandardColor bg)
Definition console.cpp:58
StandardColor bg
Definition console.hpp:36
StandardColor fg
Definition console.hpp:36
struct Console * printk_console
Definition printk.cpp:14
#define fmt(_fmt,...)
Definition syslog.hpp:161
LogLevel
Definition syslog.hpp:15