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
7#include <mos/misc/setup.hpp>
9#include <mos_stdio.hpp>
10#include <mos_string.hpp>
11
13static bool printk_quiet = false;
14
15MOS_SETUP("printk_console", printk_setup_console)
16{
17 const char *kcon_name = arg;
18 if (!kcon_name || !strlen(kcon_name))
19 {
20 pr_warn("No console name given for printk");
21 return false;
22 }
23
24 Console *console = console_get(kcon_name);
25 if (console)
26 {
27 pr_emph("Selected console '%s' for future printk\n", kcon_name);
28 printk_console = console;
29 return true;
30 }
31
32 console = console_get_by_prefix(kcon_name);
33 if (console)
34 {
35 pr_emph("Selected console '%s' for future printk (prefix-based)\n", console->name);
36 printk_console = console;
37 return true;
38 }
39
40 mos_warn("No console found for printk based on given name or prefix '%s'", kcon_name);
42 return false;
43}
44
45MOS_EARLY_SETUP("quiet", printk_setup_quiet)
46{
48 return true;
49}
50
51static inline void deduce_level_color(int loglevel, standard_color_t *fg, standard_color_t *bg)
52{
53 *bg = Black;
54 switch (loglevel)
55 {
56 case MOS_LOG_INFO2: *fg = DarkGray; break;
57 case MOS_LOG_INFO: *fg = Gray; break;
58 case MOS_LOG_EMPH: *fg = Cyan; break;
59 case MOS_LOG_WARN: *fg = Brown; break;
60 case MOS_LOG_EMERG: *fg = Red; break;
61 case MOS_LOG_FATAL: *fg = White, *bg = Red; break;
62 default: break; // do not change the color
63 }
64}
65
66static void print_to_console(Console *con, loglevel_t loglevel, const char *message, size_t len)
67{
68 if (!con)
69 return;
70
71 standard_color_t fg = con->fg, bg = con->bg;
72 deduce_level_color(loglevel, &fg, &bg);
73 con->write_color(message, len, fg, bg);
74}
75
76void lvprintk(loglevel_t loglevel, const char *fmt, va_list args)
77{
78 // only print warnings and errors if quiet mode is enabled
79 if (printk_quiet && loglevel < MOS_LOG_WARN)
80 return;
81
82 char message[MOS_PRINTK_BUFFER_SIZE];
83 const int len = vsnprintf(message, MOS_PRINTK_BUFFER_SIZE, fmt, args);
84
86 {
88 {
89 printk_console = con; // set the first console as the default
90 break;
91 }
92 }
93
94 print_to_console(printk_console, loglevel, message, len);
95}
96
98{
99 bool was_quiet = printk_quiet;
100 printk_quiet = false;
101 return was_quiet;
102}
103
104void printk_set_quiet(bool quiet)
105{
106 printk_quiet = quiet;
107}
108
109void lprintk(loglevel_t loglevel, const char *format, ...)
110{
111 va_list args;
112 va_start(args, format);
113 lvprintk(loglevel, format, args);
114 va_end(args);
115}
116
117void printk(const char *format, ...)
118{
119 va_list args;
120 va_start(args, format);
122 va_end(args);
123}
#define mos_warn(fmt,...)
Definition assert.hpp:23
#define MOS_PRINTK_BUFFER_SIZE
Definition autoconf.h:19
char args[3][16]
Definition avr_io.c:16
Console * console_get_by_prefix(const char *prefix)
Definition console.cpp:130
Console * console_get(const char *name)
Definition console.cpp:117
list_head consoles
Definition console.cpp:17
standard_color_t
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
#define list_foreach(t, v, h)
Iterate over a list.
Definition list.hpp:89
bool cmdline_string_truthiness(const char *arg, bool default_value)
Definition cmdline.cpp:83
#define unlikely(x)
Definition mos_global.h:40
#define NULL
Definition pb_syshdr.h:46
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
bool printk_unquiet(void)
Definition printk.cpp:97
void printk_set_quiet(bool quiet)
Definition printk.cpp:104
void lvprintk(loglevel_t loglevel, const char *fmt, va_list args)
Definition printk.cpp:76
void printk(const char *format,...)
Definition printk.cpp:117
static void print_to_console(Console *con, loglevel_t loglevel, const char *message, size_t len)
Definition printk.cpp:66
static bool printk_quiet
Definition printk.cpp:13
static void deduce_level_color(int loglevel, standard_color_t *fg, standard_color_t *bg)
Definition printk.cpp:51
static Console * printk_console
Definition printk.cpp:12
void lprintk(loglevel_t loglevel, const char *format,...)
Definition printk.cpp:109
#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:28
#define MOS_SETUP(_param, _fn)
Definition setup.hpp:33
standard_color_t fg
Definition console.hpp:65
standard_color_t bg
Definition console.hpp:65
size_t write_color(const char *data, size_t size, standard_color_t fg, standard_color_t bg)
Definition console.hpp:77
const char * name
Definition console.hpp:37
loglevel_t
Definition syslog.hpp:8
@ MOS_LOG_INFO
Definition syslog.hpp:13
@ MOS_LOG_FATAL
Definition syslog.hpp:9
@ MOS_LOG_INFO2
Definition syslog.hpp:14
@ MOS_LOG_WARN
Definition syslog.hpp:11
@ MOS_LOG_EMERG
Definition syslog.hpp:10
@ MOS_LOG_EMPH
Definition syslog.hpp:12