LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
log.c
Go to the documentation of this file.
1
9#include <kernel/kprintf.h>
10#include <lib/kstring.h>
11#include <kernel/log.h>
12
14static bool LOG_ENABLE_DEBUG = false;
15int LOGGING_LEVEL = LOG_LEVEL_INFO;
16
17void klog(int level, ...) {
18
19 // If it is a debug level, then dont print it
20 if (!get_debug_on() && level == LOG_LEVEL_DEBUG) return;
21
22 if (level > LOGGING_LEVEL && level != LOG_LEVEL_DEBUG) return;
23
24 // Hold the level turned into a string
25 const char* level_str;
26
27 // Assign the string to a level
28 switch (level) {
29 case LOG_LEVEL_INFO:
30 level_str = "[INFO]";
31 break;
32 case LOG_LEVEL_WARN:
33 level_str = "\033[93m[WARN]\033[0m";
34 break;
35 case LOG_LEVEL_ERROR:
36 level_str = "\033[91m[ERROR]\033[0m";
37 break;
38 case LOG_LEVEL_DEBUG:
39 level_str = "\033[90m[DEBUG]\033[0m";
40 break;
41 default:
42 level_str = "[INFO]";
43 }
44
45 va_list ap;
46 va_start(ap, level);
47
48 // Get the original format string
49 const char *fmt = va_arg(ap, const char*);
50
51 // New string to hold format string + level identifier
52 char txt[strlen(level_str) + strlen(fmt) + 3];
53 txt[0] = '\0';
54
55 // Join the strings
56 strcat(txt, level_str);
57 strcat(txt, " ");
58 strcat(txt, fmt);
59
60 // Print the result
61 kvprintf(txt, ap);
62
63 va_end(ap);
64}
65
66void set_debug_on(bool is_on) {
67 LOG_ENABLE_DEBUG = is_on;
68}
69
70bool get_debug_on(void) {
71 return LOG_ENABLE_DEBUG || LOGGING_LEVEL >= LOG_LEVEL_DEBUG;
72}
Tools for printing to the console and serial output.
void void kvprintf(const char *fmt, va_list ap)
Handles printing a formatted string.
Definition kprintf.c:28
Utilities for memory and string operations.
size_t strlen(const char *str)
Calculates the length of a string.
Definition kstring.c:92
char * strcat(char *str1, const char *str2)
Concatenates (joins) 2 strings.
Definition kstring.c:107
bool get_debug_on(void)
Gets the current value of LOG_ENABLE_DEBUG.
Definition log.c:70
static bool LOG_ENABLE_DEBUG
Should the logging util print Debug statements.
Definition log.c:14
void klog(int level,...)
Formats a string to hold a logging level and print it.
Definition log.c:17
void set_debug_on(bool is_on)
Sets the debug mode.
Definition log.c:66
Logging utilities for the kernel.