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");
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() {
if (VIDEOMODE == 40)
toggle_videomode();
clrscr();
+
+ /* Allocate logging buffer memory */
+ init_log();
+
/* Set time initially, c128 doesn't know it */
set_time_interactive();
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();
} 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();
*/
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:
}
-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;
+ }
}