From 4cd56e82eae50a9aa9613609062baf30dba3cd08 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 30 Sep 2012 12:51:13 +0200 Subject: [PATCH] log to memory --- include/print.h | 9 +++-- src/config.c | 21 +++--------- src/kasse.c | 9 +++-- src/print.c | 88 +++++++++++++++++++++++++++++-------------------- 4 files changed, 70 insertions(+), 57 deletions(-) diff --git a/include/print.h b/include/print.h index 586c708..33acad2 100644 --- a/include/print.h +++ b/include/print.h @@ -1,19 +1,22 @@ #ifndef _PRINT_H #define _PRINT_H +void init_log(); void print_the_buffer(); void print_header(); void log_file(const char *s); +void log_flush(void); #ifdef _IS_PRINT char print_buffer[80 + 2 + 1]; unsigned char log_num = 0; -int log_lines_written = 0; #else extern char print_buffer[80 + 2 + 1]; extern unsigned char log_num; -extern int log_lines_written; - +extern char *log_heap_buf; +extern int log_heap_offset; +extern int log_heap_flushed; +extern const int LOG_SIZE; #endif #endif diff --git a/src/config.c b/src/config.c index 266f441..7304c07 100644 --- a/src/config.c +++ b/src/config.c @@ -40,15 +40,7 @@ struct credits_array_t credits; static void lookup_needed_files() { BYTE lfn = 8, c; struct cbm_dirent dirent; - char *buffer = malloc(sizeof(char) * 64 * 100); - char *walk; char filename[8]; - int n; - - if (buffer == NULL) { - cprintf("Not enough memory available\r\n"); - exit(1); - } if (cbm_opendir(lfn, (BYTE)8) != 0) { cprintf("could not open directory\r\n"); @@ -76,20 +68,15 @@ static void lookup_needed_files() { c128_perror(c, "cbm_open(log) for reading"); exit(1); } - n = cbm_read(lfn, buffer, sizeof(char) * 64 * 100); - if (n < 0) { + log_heap_offset = cbm_read(lfn, log_heap_buf, LOG_SIZE); + if (log_heap_offset < 0) { cprintf("error while cbm_read()ing the logfile\r\n"); exit(1); } - buffer[n] = '\0'; - for (walk = buffer; (walk - buffer) < n; walk++) { - if (*walk == '\r' || *walk == '\n') - log_lines_written++; - } + log_heap_flushed = log_heap_offset; + log_heap_buf[log_heap_offset] = '\0'; cbm_close(lfn); } - - free(buffer); } void load_items() { diff --git a/src/kasse.c b/src/kasse.c index 0710b2a..4883ef8 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -303,6 +303,10 @@ int main() { if (VIDEOMODE == 40) toggle_videomode(); clrscr(); + + /* Allocate logging buffer memory */ + init_log(); + /* Set time initially, c128 doesn't know it */ set_time_interactive(); @@ -322,7 +326,7 @@ int main() { sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17); print_the_buffer(); - sprintf(print_buffer, "%cKasse gestartet um %s. Nutze logfile log-%u, zeile %d.\r", 17, time, log_num, log_lines_written); + sprintf(print_buffer, "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17, time, log_num, log_heap_offset); print_the_buffer(); print_header(); @@ -346,7 +350,8 @@ int main() { } else if (*c == 's') { save_items(); save_credits(); - cprintf("Statefile/Creditfile gesichert, druecke RETURN...\r\n"); + log_flush(); + cprintf("\r\nStatefile/Creditfile/Log gesichert, druecke RETURN...\r\n"); get_input(); } else if (*c == 'g') { credit_manager(); diff --git a/src/print.c b/src/print.c index 803bbb6..768e819 100644 --- a/src/print.c +++ b/src/print.c @@ -19,6 +19,22 @@ */ unsigned char __fastcall__ _sysremove(const char *name); +/* 8192 bytes of log buffer on the heap used for storing one entire logfile in + * memory before writing (unless flushed). */ +char *log_heap_buf; +int log_heap_offset = 0; +int log_heap_flushed = 0; + +const int LOG_SIZE = 8192; + +void init_log() { + log_heap_buf = malloc(sizeof(char) * LOG_SIZE); + if (log_heap_buf == NULL) { + cprintf("malloc(log_heap_buf) failed"); + exit(1); + } +} + void print_the_buffer() { BYTE c; RETRY: @@ -53,48 +69,50 @@ void print_header() { } -void log_file(const char *s) { - /* A log-entry has usually 50 bytes, so we take 64 bytes. - Because files are wrapped (log.0, log.1, ...) every 100 - lines, we don't need more than 100 * 64 bytes. */ - char *buffer = malloc(sizeof(char) * 64 * 100); - char filename[8]; - int read = 0; - unsigned int c; - - if (buffer == NULL) { - cprintf("No memory available\n"); - exit(1); - } - - buffer[0] = '\0'; - if (((++log_lines_written) % 100) == 0) - log_num++; +/* + * Flushes the current log buffer to disk. Called either by log_file() when one + * entire log file is completed or interactively. + * + */ +void log_flush(void) { + int c; + static char filename[8]; sprintf(filename, "log-%d", log_num); - /* Don't read log if there were no lines written before */ - if (log_lines_written != 1) { - if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)0, filename)) != 0) { - c128_perror(c, "cbm_open(log)"); - exit(1); - } - read = cbm_read((BYTE)8, buffer, sizeof(char) * 64 * 100); - cbm_close((BYTE)8); + + /* If we have written to this logfile before, we need to remove it first */ + if (log_heap_flushed > 0) _sysremove(filename); - } + if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) { c128_perror(c, "cbm_open(log)"); exit(1); } - if (read < 0) { - cprintf("Could not read existing logfile (read returned %d)\n", read); - exit(1); - } - strcpy(buffer+read, s); - c = cbm_write((BYTE)8, buffer, read+strlen(s)); - if (c != (read+strlen(s))) { - cprintf("Could not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, (read+strlen(s))); + c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset); + if (c != log_heap_offset) { + textcolor(TC_LIGHT_RED); + cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, log_heap_offset); + c128_perror(c, "cbm_write"); exit(1); } cbm_close((BYTE)8); - free(buffer); + + log_heap_flushed = log_heap_offset; +} + +/* + * Logs to memory and eventually to file (log_flush() is called when one entire + * logfile was completed in memory). + * + */ +void log_file(const char *s) { + strcpy(log_heap_buf+log_heap_offset, s); + log_heap_offset += strlen(s); + + /* Force a flush when there are only five lines left */ + if (log_heap_offset > (LOG_SIZE - (5 * 80))) { + log_flush(); + log_num++; + log_heap_offset = 0; + log_heap_flushed = 0; + } } -- 2.39.5