The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
klog.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * kernel logging: level-tagged diagnostics written to the serial port.
5 *
6 * klog output goes to COM1 (see drivers/serial), so it is visible headlessly
7 * under QEMU with `-serial stdio` and works before/without the VGA console.
8 *
9 * use the level macros (KLOG_DEBUG/INFO/WARN/ERROR) rather than calling
10 * klog() directly. each expands to nothing when its level is below the
11 * compile-time KLOG_LEVEL threshold, so verbose logging costs zero in
12 * release builds.
13 */
14
15#include "common/stdint.h"
16
17/**
18 * log severity levels, lowest to highest.
19 */
27
28/**
29 * compile-time minimum level. messages below this are dropped at compile
30 * time. defaults to INFO in release (NDEBUG) and DEBUG otherwise. override
31 * by defining KLOG_LEVEL on the compiler command line.
32 */
33#ifndef KLOG_LEVEL
34#ifdef NDEBUG
35#define KLOG_LEVEL KLOG_LEVEL_INFO
36#else
37#define KLOG_LEVEL KLOG_LEVEL_DEBUG
38#endif
39#endif
40
41/**
42 * initialize the logging backend (brings up the serial port).
43 * call once, early in boot, before any KLOG_* macro is used.
44 */
45void klog_init(void);
46
47/**
48 * register /dev/kmsg: each line a program writes becomes an info line in
49 * the log (on the serial port). call once the device registry is up.
50 */
51void klog_device_init(void);
52
53/**
54 * also write every log line with this function (the console during boot),
55 * or stop with NULL.
56 */
57void klog_set_echo(void (*putchar)(char c));
58
59/**
60 * write a level-tagged, formatted log line to the serial port.
61 * appends a trailing newline. prefer the KLOG_* macros over this.
62 * @param level severity of the message.
63 * @param fmt printf-style format string.
64 */
65void klog(klog_level_t level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
66
67// level macros: expand to a klog() call only when the level is enabled,
68// otherwise to a no-op that discards the arguments at compile time.
69#if KLOG_LEVEL <= KLOG_LEVEL_DEBUG
70#define KLOG_DEBUG(...) klog(KLOG_LEVEL_DEBUG, __VA_ARGS__)
71#else
72#define KLOG_DEBUG(...) ((void)0)
73#endif
74
75#if KLOG_LEVEL <= KLOG_LEVEL_INFO
76#define KLOG_INFO(...) klog(KLOG_LEVEL_INFO, __VA_ARGS__)
77#else
78#define KLOG_INFO(...) ((void)0)
79#endif
80
81#if KLOG_LEVEL <= KLOG_LEVEL_WARN
82#define KLOG_WARN(...) klog(KLOG_LEVEL_WARN, __VA_ARGS__)
83#else
84#define KLOG_WARN(...) ((void)0)
85#endif
86
87#if KLOG_LEVEL <= KLOG_LEVEL_ERROR
88#define KLOG_ERROR(...) klog(KLOG_LEVEL_ERROR, __VA_ARGS__)
89#else
90#define KLOG_ERROR(...) ((void)0)
91#endif
void klog_init(void)
initialize the logging backend (brings up the serial port).
Definition klog.c:29
void klog_device_init(void)
register /dev/kmsg: each line a program writes becomes an info line in the log (on the serial port).
Definition klog.c:93
void klog(klog_level_t level, const char *fmt,...)
Definition klog.c:34
void klog_set_echo(void(*putchar)(char c))
also write every log line with this function (the console during boot), or stop with NULL.
Definition klog.c:18
klog_level_t
kernel logging: level-tagged diagnostics written to the serial port.
Definition klog.h:21
@ KLOG_LEVEL_INFO
Definition klog.h:23
@ KLOG_LEVEL_DEBUG
Definition klog.h:22
@ KLOG_LEVEL_WARN
Definition klog.h:24
@ KLOG_LEVEL_ERROR
Definition klog.h:25
int printf(const char *fmt,...) __attribute__((format(printf
formatted output.
int putchar(int c)
ELF32 file header.
Definition elf.h:51