summaryrefslogtreecommitdiff
path: root/lib/logger/log.c
blob: 9f859bc9f0ea8ceaeb3b883c901f07f57c20a59c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#define FOOLOS_MODULE_NAME "log"

#include <stdarg.h>
#include <stdbool.h>

#include "log.h"
#include "kernel/config.h"
#include "kernel/console.h"
#include "lib/printf/printf.h"
#include "kernel/timer.h"


static char buffer[LOG_BUF_SIZE];
static int first=0;
static int last=0;
static bool init=false;

void log(char *module_name, int log_level, char *format_string, ...)
{

    #ifdef FOOLOS_LOG_OFF
    return;
    #endif

    if(log_level<FOOLOS_LOG_INFO)return;

    char buf_info[256];
    char buf_log[256];
    char buf_time[20];

    uint32_t t=timer_get_ticks();
    uint32_t s=t/25;
    uint32_t ms=t*1000/25-1000*s;

    tfp_sprintf(buf_time,"[%3d.%05d]",s,ms);

    va_list va;
    va_start(va,format_string);
    tfp_vsprintf(buf_info,format_string,va);
    va_end(va);

    tfp_sprintf(buf_log,"%s %s: %s\n",buf_time,module_name,buf_info);

    if(init)console_put_str_gray(buf_log);

    for(int i=0;buf_log[i]!=0;i++)
    {
	buffer[last]=buf_log[i];
	last=(last+1)%LOG_BUF_SIZE;
	if(first<last)if(last-first>=LOG_BUF_SIZE)first=(first+1)%LOG_BUF_SIZE;
	if(last>first)if(LOG_BUF_SIZE-last+first>=LOG_BUF_SIZE)first=(first+1)%LOG_BUF_SIZE;
    }


}

void panic(char *module_name,  char *message)
{
    char buf_log[256];
    tfp_sprintf(buf_log,"KERNEL PANIC !! %s: %s\n",module_name,message);
    console_put_str_red(buf_log);
    while(1); // halt
}


void log_log()
{
    #ifdef FOOLOS_LOG_OFF
    return;
    #endif

    char buf_log[256];
    tfp_sprintf(buf_log,"[  0.00000] log: buffer state: first=%d, last=%d, buf_size=%d\n",first,last,LOG_BUF_SIZE);
    console_put_str_gray(buf_log);
    for(int i=first;i!=last;i++)
    {
	console_put_char_gray(buffer[i]);

    }

    init=true;
}