Skip to content

Commit

Permalink
console: unify panic() and warning()
Browse files Browse the repository at this point in the history
Move the duplicated code of panic() and warning() to a common helper
that'll take care about the printk()s. The message type ("PANIC" /
"WARNING") will still be center aligned.

Signed-off-by: Mathias Krause <[email protected]>
  • Loading branch information
minipli-oss authored and wipawel committed Nov 13, 2023
1 parent 5fb4cd1 commit 8350123
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions common/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,39 @@ void register_console_callback(console_callback_t cb, void *arg) {
console_callbacks[num_console_callbacks++].arg = arg;
}

void panic(const char *fmt, ...) {
va_list args;
static void oops_print(const char *fmt, va_list args, const char *type) {
static const char stars[] = "***********************************";
size_t slen = sizeof(stars) - 1;
size_t tlen = strlen(type);
int s1_len, s2_len;

if (tlen > slen - 4)
tlen = slen - 4;

s1_len = (slen - tlen - 2) / 2;
s2_len = slen - tlen - 2 - s1_len;

printk("************** PANIC **************\n");
printk("%.*s %s %.*s\n", s1_len, stars, type, s2_len, stars);
printk("CPU[%u]: ", smp_processor_id());
vprintk(fmt, args);
printk("\n%s\n", stars);
}

void panic(const char *fmt, ...) {
va_list args;

va_start(args, fmt);
vprintk(fmt, args);
oops_print(fmt, args, "PANIC");
va_end(args);

printk("\n***********************************\n");

while (1)
halt();
}

void warning(const char *fmt, ...) {
va_list args;

printk("************* WARNING *************\n");
printk("CPU[%u]: ", smp_processor_id());

va_start(args, fmt);
vprintk(fmt, args);
oops_print(fmt, args, "WARNING");
va_end(args);

printk("\n***********************************\n");
}

0 comments on commit 8350123

Please sign in to comment.