MOS Source Code
Loading...
Searching...
No Matches
printk.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
6#include <mos/misc/cmdline.h>
7#include <mos/misc/setup.h>
8#include <mos/syslog/printk.h>
9#include <mos_stdio.h>
10#include <mos_string.h>
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_t *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_t *con, loglevel_t loglevel, const char *message, size_t len)
67{
68 if (!con)
69 return;
70
71 static standard_color_t fg, bg; // declared static to use previous values
72 if (once())
73 con->ops->get_color(con, &fg, &bg); // only fill the colors once
74 deduce_level_color(loglevel, &fg, &bg);
75 console_write_color(con, message, len, fg, bg);
76}
77
78void lvprintk(loglevel_t loglevel, const char *fmt, va_list args)
79{
80 // only print warnings and errors if quiet mode is enabled
81 if (printk_quiet && loglevel < MOS_LOG_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 {
90 {
91 printk_console = con; // set the first console as the default
92 break;
93 }
94 }
95
96 print_to_console(printk_console, loglevel, message, len);
97}
98
100{
101 bool was_quiet = printk_quiet;
102 printk_quiet = false;
103 return was_quiet;
104}
105
106void printk_set_quiet(bool quiet)
107{
108 printk_quiet = quiet;
109}
110
111void lprintk(loglevel_t loglevel, const char *format, ...)
112{
113 va_list args;
114 va_start(args, format);
115 lvprintk(loglevel, format, args);
116 va_end(args);
117}
118
119void printk(const char *format, ...)
120{
121 va_list args;
122 va_start(args, format);
124 va_end(args);
125}
u32 const char * fmt
Definition assert.h:36
#define mos_warn(fmt,...)
Definition assert.h:23
#define MOS_PRINTK_BUFFER_SIZE
Definition autoconf.h:19
char args[3][16]
Definition avr_io.c:16
size_t console_write_color(console_t *con, const char *data, size_t size, standard_color_t fg, standard_color_t bg)
Definition console.c:132
console_t * console_get_by_prefix(const char *prefix)
Definition console.c:114
console_t * console_get(const char *name)
Definition console.c:101
list_head consoles
Definition console.c:16
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.h:17
#define list_foreach(t, v, h)
Iterate over a list.
Definition list.h:83
bool cmdline_string_truthiness(const char *arg, bool default_value)
Definition cmdline.c:83
#define unlikely(x)
Definition mos_global.h:40
#define once()
Returns true for the first call, false for all subsequent calls.
Definition mos_global.h:114
#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.c:99
static console_t * printk_console
Definition printk.c:12
void printk_set_quiet(bool quiet)
Definition printk.c:106
void lvprintk(loglevel_t loglevel, const char *fmt, va_list args)
Definition printk.c:78
void printk(const char *format,...)
Definition printk.c:119
static bool printk_quiet
Definition printk.c:13
static void deduce_level_color(int loglevel, standard_color_t *fg, standard_color_t *bg)
Definition printk.c:51
static void print_to_console(console_t *con, loglevel_t loglevel, const char *message, size_t len)
Definition printk.c:66
void lprintk(loglevel_t loglevel, const char *format,...)
Definition printk.c:111
#define pr_warn(fmt,...)
Definition printk.h:38
#define pr_emph(fmt,...)
Definition printk.h:37
#define MOS_EARLY_SETUP(_param, _fn)
Definition setup.h:30
#define MOS_SETUP(_param, _fn)
Definition setup.h:35
struct console_ops * ops
Definition console.h:30
const char * name
Definition console.h:31
loglevel_t
Definition syslog.h:9
@ MOS_LOG_INFO
Definition syslog.h:14
@ MOS_LOG_FATAL
Definition syslog.h:10
@ MOS_LOG_INFO2
Definition syslog.h:15
@ MOS_LOG_WARN
Definition syslog.h:12
@ MOS_LOG_EMERG
Definition syslog.h:11
@ MOS_LOG_EMPH
Definition syslog.h:13