liblog/liblog.c

196 lines
3.3 KiB
C

#include <gint/exc.h>
#include <gint/keyboard.h>
#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>
#ifdef FX9860G
#define MAX_LENGHT 22
#define WIDTH 8
#endif
#ifdef FXCG50
#define MAX_LENGHT 132
#define WIDTH 17
#endif
typedef struct log_line log_line;
typedef struct log_line
{
void* previous;
#ifdef FXCG50
char text[MAX_LENGHT];
#endif
#ifdef FX9860G
char text[MAX_LENGHT];
#endif
void* next;
int cursor;
} log_line;
static log_line first_line=
{
0,
"",
0,
0
};
static log_line* current_line=&first_line;
static int state;
void ll_set_state(int s)
{
state = s;
}
int ll_get_state()
{
return state;
}
static log_line* add_line()
{
current_line->next = malloc(sizeof(log_line));
log_line* previous = current_line;
current_line = current_line->next;
current_line->previous=previous;
current_line->next=0;
current_line->cursor=0;
for (int i=0; i<MAX_LENGHT; i++)
current_line->text[i]='\0';
return current_line;
}
void ll_clear()
{
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';
}
static void show_line(const log_line* l, int y)
{
dtext(1, y, &l->text[0], C_BLACK, C_NONE);
}
static void append_character(char c)
{
if (c=='\n')
{
current_line->text[current_line->cursor]= '\0';
add_line();
}
else
{
current_line->text[current_line->cursor] = c;
current_line->cursor ++;
}
if (current_line->cursor == MAX_LENGHT-1)
add_line();
current_line->text[current_line->cursor] = '\0';
#ifdef FXCG50
if (dsize(&current_line->text[0])>396-7)
{
add_line();
}
#endif
}
// log something
void ll_send(const char * txt)
{
if (state)
{
char c=1;
int i=0;
while (c)
{
c=txt[i];
append_character(c);
i++;
}
}
}
void ll_display_custom(log_line* line)
{
dfont(NULL);
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;
}
dupdate();
}
void ll_display()
{
ll_display_custom(current_line);
}
void ll_pause()
{
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;
}
}
GNORETURN void ll_panic(uint32_t code)
{
char str[10];
sprintf(str, "\nException !\n>> LogState=%d\n>> ErrCode=%d", state, code);
ll_set_state(1);
ll_send(str);
while (1)
ll_pause();
}
void ll_set_panic()
{
gint_panic_set(ll_panic);
}