From 0c1fd482bd27c5a48d91c086558b4cf07ca6e000 Mon Sep 17 00:00:00 2001 From: milang Date: Tue, 1 Oct 2019 19:30:42 +0200 Subject: [PATCH] add a custom ram weight --- liblog.c | 79 +++++++++++++++++++++++++++++++++++++++++++++----------- liblog.h | 2 ++ 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/liblog.c b/liblog.c index ede7a50..c1f5deb 100644 --- a/liblog.c +++ b/liblog.c @@ -38,15 +38,20 @@ typedef struct log_line static log_line first_line= { 0, - "", + "You are at the top !", 0, 0 }; static log_line* current_line=&first_line; -static int state; +/// Change the following line to custom ram weight +static int number_max_messages=2048; // 1Ko max +static int number_messages=0; + +static int state=1; + void ll_set_state(int s) { state = s; @@ -57,21 +62,59 @@ int ll_get_state() return state; } -static log_line* add_line() +static log_line* cut_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; + log_line* maillon = first_line.next; + log_line* maillon_suivant = maillon->next; + + first_line.next=maillon_suivant; + maillon_suivant->previous=&first_line; + + return maillon; +} + +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; itext[i]='\0'; +} + +static log_line* add_line() +{ + int malloc_fail=0; + + log_line* line; + + int test=(number_messages*sizeof(log_line)>=number_max_messages); + if (!test) + { + number_messages++; + line = malloc(sizeof(log_line)); + if (0==line) + malloc_fail=1; + } + + if (test || malloc_fail) // fail du malloc ou dépassement de la limite de ram imposée lors du build + line=cut_line(); + + link_line(line); + clear_line(line); return current_line; + } void ll_clear() { + number_messages=0; while (current_line->previous) { log_line* next=current_line->previous; @@ -84,14 +127,12 @@ void ll_clear() 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 (current_line==&first_line) + add_line(); if (c=='\n') { current_line->text[current_line->cursor] = '\0'; @@ -132,6 +173,11 @@ void ll_send(const char * txt) } } +static void show_line(const log_line* l, int y) +{ + dtext(1, y, &l->text[0], C_BLACK, C_NONE); +} + void ll_display_custom(log_line* line) { dfont(NULL); @@ -148,10 +194,12 @@ void ll_display_custom(log_line* line) if (!line) break; } - dupdate(); + dupdate_noint(); } + + void ll_display() { ll_display_custom(current_line); @@ -159,6 +207,7 @@ void ll_display() void ll_pause() { + dclear(C_WHITE); log_line* line=current_line; while (1) { diff --git a/liblog.h b/liblog.h index 1bf0ae0..da2583f 100644 --- a/liblog.h +++ b/liblog.h @@ -26,4 +26,6 @@ void ll_pause(); // Clear log void ll_clear(); +void ll_set_lenght(); + #endif