LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
log.c
Go to the documentation of this file.
1#include <config.h>
2#include <stdint.h>
3#include <string.h>
4#include <panic.h>
5#include <fbconsole.h>
6#include <log.h>
7#include <vm.h>
8#include <scheduler.h>
9#include <efi.h>
10#include <io.h>
11
12const int logging_page_size = 4080;
13
21
22// this is our logging page to use before memory management is up
24 .prev = 0,
25 .next = 0,
26 .current_end = 0,
27};
28
33
34void log_append_page(void) {
36 memset((void*)new, 0, logging_page_size);
37 new->prev = log_last;
38 log_last->next = new;
39 log_last = new;
40
42
43 while(log_page_count * logging_page_size > LOG_MAX_BUFFER) {
44 struct logging_page* page = log_first;
45
46 page->next->prev = 0;
47 log_first = page->next;
49
50 for(int i = 0; i < page->current_end; ++i) {
51 if(!page->messages[i]) {
52 --log_count;
53 }
54 }
55
56 if(page != &log_initial_page) {
57 vm_free(page);
58 }
59 }
60}
61
62void log_append(char level, char* component, char* message) {
63 size_t len = strlen(component) + strlen(message) + 4; // 4: level, two tabs and terminator
64
65 if(len > sizeof(log_first->messages) - log_last->current_end) {
67 }
68
69 uint64_t msg_start = log_last->current_end;
70
71 ksnprintf(log_last->messages + log_last->current_end, sizeof(log_last->messages) - log_last->current_end, "%c\t%s\t%s", (int)level, component, message);
72 log_last->current_end += len;
73 uint64_t msg_end = log_last->current_end;
74
76 int color_code;
77
78 switch(level) {
79 case 'D':
80 color_code = 8;
81 break;
82 case 'I':
83 color_code = 15;
84 break;
85 case 'W':
86 color_code = 3;
87 break;
88 case 'E':
89 color_code = 1;
90 break;
91 case 'F':
92 color_code = 5;
93 break;
94 default:
95 color_code = 4;
96 break;
97 }
98
99 fbconsole_write("\x1b[38;5;%dm[%c] %s: %s\n", color_code, level, component, message);
100 }
101
102 if(LOG_COM0) {
103 for(size_t i = msg_start; i < msg_end-1; ++i) {
104 while((inb(0x3F8 + 5) & 0x20) == 0) {}
105 outb(0x3F8, log_last->messages[i]);
106 }
107 while((inb(0x3F8 + 5) & 0x20) == 0) {}
108 outb(0x3F8, '\r');
109 while((inb(0x3F8 + 5) & 0x20) == 0) {}
110 outb(0x3F8, '\n');
111 }
112
113 if(LOG_EFI) {
114 efi_append_log(log_last->messages + msg_start);
115 }
116
117 ++log_count;
118}
119
120void log(char level, char* component, char* fmt, ...) {
121 va_list args;
122 char buffer[logging_page_size];
123 memset((uint8_t*)buffer, 0, sizeof(buffer));
124
125 va_start(args, fmt);
126 kvsnprintf(buffer, sizeof(buffer), fmt, args);
127 va_end(args);
128
129 log_append(level, component, buffer);
130}
131
133 if(message[strlen(message)-1] == '\n') {
134 message[strlen(message)-1] = 0;
135 }
136
137 char buffer[20];
138 ksnprintf(buffer, sizeof(buffer), "process %d", scheduler_current_process);
139
140 logd(buffer, "%s", message);
141}
unsigned short uint16_t
Definition arch.h:8
unsigned long uint64_t
Definition arch.h:14
unsigned char uint8_t
Definition arch.h:5
void efi_append_log(char *msg)
Definition efi.c:80
bool fbconsole_active
Definition fbconsole.c:13
int fbconsole_write(char *string,...)
Definition fbconsole.c:223
static uint8_t inb(uint16_t port)
Definition io.h:18
static void outb(uint16_t port, uint8_t data)
Definition io.h:6
size_t strlen(const char *str)
Definition string.c:30
void * memset(void *dest, int c, size_t size)
Definition string.c:72
size_t ksnprintf(char *buffer, size_t buffer_size, const char *format,...)
Definition string.c:339
size_t kvsnprintf(char *buffer, size_t buffer_size, const char *format, va_list args)
Definition string.c:224
struct logging_page * prev
Definition log.c:15
uint16_t current_end
Definition log.c:17
struct logging_page * next
Definition log.c:16
void log(char level, char *component, char *fmt,...)
Definition log.c:120
char messages[logging_page_size - 18]
Definition log.c:19
void sc_handle_debug_print(char *message)
Definition log.c:132
uint64_t log_count
Definition log.c:32
void log_append_page(void)
Definition log.c:34
const int logging_page_size
Definition log.c:12
struct logging_page * log_first
Definition log.c:29
void log_append(char level, char *component, char *message)
Definition log.c:62
uint64_t log_page_count
Definition log.c:31
struct logging_page log_initial_page
Definition log.c:23
struct logging_page * log_last
Definition log.c:30
#define logd(component, fmt,...)
Definition log.h:28
volatile pid_t scheduler_current_process
Definition scheduler.c:50
#define va_start(ap, X)
Definition stdarg.h:6
#define va_end(ap)
Definition stdarg.h:8
__builtin_va_list va_list
Definition stdarg.h:4
static bool struct Message * message
Definition syscalls.h:338
void * vm_alloc(size_t size)
Like malloc but allocates full pages only. 16 byte data overhead.
Definition vm.c:697
void vm_free(void *ptr)
the matching free() like function for vm_alloc
Definition vm.c:707