liblog/liblog.c

292 lines
4.5 KiB
C
Raw Permalink Normal View History

#include <gint/exc.h>
#include <gint/keyboard.h>
2019-09-18 17:44:47 +02:00
#include <gint/display.h>
#include <gint/std/stdlib.h>
#include <gint/std/stdio.h>
#include <gint/defs/attributes.h>
#include <liblog.h>
#include <stdint.h>
#include <gint/timer.h>
#ifdef FX9860G
static unsigned int number_max_messages=4096; // Ram 4Ko
#define MAX_LENGHT 22
#define WIDTH 8
#endif
2020-01-02 22:07:04 +01:00
#ifdef FXCG50
static unsigned int number_max_messages=10000; // Ram 10 Ko
#define MAX_LENGHT 132
#define WIDTH 17
#endif
typedef struct log_line log_line;
typedef struct log_line
{
void* previous;
char text[MAX_LENGHT];
void* next;
int cursor;
} log_line;
static log_line first_line=
2019-09-18 17:44:47 +02:00
{
0,
"Log beginning:",
0,
0
2019-09-18 17:44:47 +02:00
};
2019-09-28 14:28:05 +02:00
2020-01-02 22:07:04 +01:00
static log_line* current_line=&first_line;
2019-09-28 14:28:05 +02:00
2019-10-01 19:30:42 +02:00
static int number_messages=0;
2020-01-06 19:48:41 +01:00
static log_level_t priority=LEVEL_INFO;
2019-10-01 19:30:42 +02:00
2019-10-15 19:35:26 +02:00
static log_line* cut_line()
{
log_line* maillon = first_line.next;
log_line* maillon_suivant = maillon->next;
first_line.next=maillon_suivant;
if (maillon_suivant)
maillon_suivant->previous=&first_line;
number_messages--;
2019-10-15 19:35:26 +02:00
return maillon;
}
static void optimize()
{
while (number_messages*sizeof(log_line)>=number_max_messages)
{
log_line* line = cut_line();
free(line);
//number_messages--;
2019-10-15 19:35:26 +02:00
}
}
void ll_set_size(int s)
{
2019-10-31 11:18:51 +01:00
number_max_messages=s;
2019-10-15 19:35:26 +02:00
optimize();
}
int ll_get_size();
2019-10-01 19:30:42 +02:00
static void link_line(log_line* line)
{
current_line->next=line;
line->previous=current_line;
current_line=line;
}
static void clear_line(log_line* line)
{
line->next=0;
line->cursor=0;
for (int i=0; i<MAX_LENGHT; i++)
current_line->text[i]='\0';
2019-10-01 19:30:42 +02:00
}
static log_line* add_line()
{
int malloc_fail=0;
2019-10-01 19:30:42 +02:00
log_line* line;
2019-10-15 19:35:26 +02:00
int test=(number_messages*sizeof(log_line)>=number_max_messages);
if (!test)
{
line = malloc(sizeof(log_line));
if (0==line)
malloc_fail=1;
}
2019-10-15 19:35:26 +02:00
if (test || malloc_fail) // fail du malloc ou dépassement de la limite de ram imposée lors du build
line=cut_line();
2019-10-01 19:30:42 +02:00
link_line(line);
clear_line(line);
number_messages++;
optimize();
return current_line;
2019-10-01 19:30:42 +02:00
}
void ll_clear()
2019-09-18 17:44:47 +02:00
{
number_messages=0;
while (current_line->previous)
{
log_line* next=current_line->previous;
free(current_line);
current_line=next;
}
current_line->next=0;
current_line->cursor=0;
for (int i=0; i<MAX_LENGHT; i++)
current_line->text[i]='\0';
2019-09-18 17:44:47 +02:00
}
2019-10-01 19:30:42 +02:00
2019-09-18 17:44:47 +02:00
static void append_character(char c)
2019-09-18 17:44:47 +02:00
{
if (current_line==&first_line)
add_line();
if (c=='\n')
{
current_line->text[current_line->cursor] = '\0';
add_line();
return;
}
current_line->text[current_line->cursor] = c;
if (c!='\0')
{
current_line->cursor ++;
if (current_line->cursor == MAX_LENGHT-1)
add_line();
//current_line->text[current_line->cursor] = '\0';
#ifdef FXCG50
else if (dsize(&current_line->text[0],0,0,0)>396-7)
{
add_line();
}
2019-09-28 14:28:05 +02:00
#endif
}
2019-09-18 17:44:47 +02:00
}
// log something
static void ll_send_internal(const char * txt)
2019-09-18 17:44:47 +02:00
{
if (priority!=LEVEL_QUIET)
{
char c=1;
int i=0;
while (c!='\0')
{
c=txt[i];
append_character(c);
i++;
}
}
2019-09-18 17:44:47 +02:00
}
void ll_send(log_level_t p, char const *format, ...)
2020-01-02 22:07:04 +01:00
{
if (p>=priority)
{
char str[512];
va_list args;
va_start(args, format);
vsnprintf(str, 512, format, args);
va_end(args);
ll_send_internal(str);
}
2020-01-02 22:07:04 +01:00
}
2019-10-01 19:30:42 +02:00
static void show_line(const log_line* l, int y)
{
font_t const * f=dfont(0);
2020-01-03 20:36:32 +01:00
2020-11-11 14:59:17 +01:00
dtext(1, y, C_BLACK, &l->text[0]);
2020-01-03 20:36:32 +01:00
dfont(f);
2019-10-01 19:30:42 +02:00
}
2019-09-20 13:43:21 +02:00
void ll_display_custom(log_line* line)
2019-09-18 17:44:47 +02:00
{
dclear(C_WHITE);
for (int i=0; i<8; i++)
{
#ifdef FX9860G
show_line(line, 63 - 8*(i+1));
#endif
#ifdef FXCG50
show_line(line, 224 - 13*(i+1));
#endif
line=line->previous;
if (!line)
break;
}
2020-11-11 14:59:17 +01:00
//dupdate_noint(); broke with gint update :thinking:
dupdate();
2019-09-18 17:44:47 +02:00
}
2019-09-20 13:43:21 +02:00
2019-10-01 19:30:42 +02:00
2019-09-20 13:43:21 +02:00
void ll_display()
{
ll_display_custom(current_line);
2019-09-20 13:43:21 +02:00
}
void ll_pause()
{
//for (int i=0; i<timer_count(); i++)
// if (i!=3) // keyboard timer
// timer_pause(i);
dclear(C_WHITE);
log_line* line=current_line;
while (1)
{
ll_display_custom(line);
int key = getkey().key;
if (key==KEY_UP)
{
log_line* linet=line->previous;
if (linet)
line=linet;
}
if (key==KEY_DOWN)
{
log_line* linet=line->next;
if (linet)
line=linet;
}
if (key==KEY_EXIT)
break;
}
//for (int i=0; i<timer_count(); i++)
// if (i!=3)
// timer_start(i);
}
2019-09-20 13:43:21 +02:00
GNORETURN void ll_panic(uint32_t code)
{
char str[10];
sprintf(str, "\nException !\n>> ErrCode=%d\nUse restart :(",code);
ll_set_level(LEVEL_INFO);
ll_send(LEVEL_FATAL, str);
while (1)
ll_pause();
}
void ll_set_panic()
{
gint_panic_set(ll_panic);
}
2020-01-06 19:48:41 +01:00
void ll_set_level(log_level_t l)
{
priority=l;
2020-01-06 19:48:41 +01:00
}
log_level_t ll_get_level(void)
{
return priority;
}