GE-115 Emulator
An Emulator of the General Electrics GE-115 computer
log.c
Go to the documentation of this file.
1#include "log.h"
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <string.h>
6#include <ctype.h>
7
8static ge_log_type active_log_types = -1; // ~(LOG_CONDS | LOG_STATES);
9
10static const char *log_type_name(ge_log_type type)
11{
12 switch (type) {
13 case LOG_ERR:
14 return "error ";
15 case LOG_DEBUG:
16 return "debug ";
17 case LOG_REGS:
18 return "regs ";
19 case LOG_STATES:
20 return "states ";
21 case LOG_CONDS:
22 return "conds ";
23 case LOG_REGS_V:
24 return "regs v ";
25 case LOG_FUTURE:
26 return "future ";
27 case LOG_CYCLE:
28 return "cycle ";
29 case LOG_CONSOLE:
30 return "console ";
31 case LOG_PERI:
32 return "periph ";
33 case LOG_READER:
34 return "reader ";
35 case LOG_CMDS:
36 return "cmds ";
37 default:
38 return " ";
39 }
40}
41
46
48{
49 /* Fixed-size token buffer; names are short, 64 bytes is ample. */
50 char tok[64];
51 ge_log_type mask = 0;
52 const char *p, *end;
53
54 if (spec == NULL || spec[0] == '\0')
55 return;
56
57 p = spec;
58 while (*p != '\0') {
59 /* Skip leading spaces/commas */
60 while (*p == ',' || isspace((unsigned char)*p))
61 p++;
62 if (*p == '\0')
63 break;
64
65 /* Find end of token */
66 end = p;
67 while (*end != '\0' && *end != ',')
68 end++;
69
70 /* Trim trailing spaces within the token */
71 const char *token_end = end;
72 while (token_end > p && isspace((unsigned char)*(token_end - 1)))
73 token_end--;
74
75 /* Copy token safely */
76 size_t len = (size_t)(token_end - p);
77 if (len >= sizeof(tok))
78 len = sizeof(tok) - 1;
79 memcpy(tok, p, len);
80 tok[len] = '\0';
81
82 /* Map token to bitmask */
83 if (strcmp(tok, "all") == 0)
84 mask |= (ge_log_type)-1;
85 else if (strcmp(tok, "none") == 0)
86 mask = 0;
87 else if (strcmp(tok, "err") == 0)
88 mask |= LOG_ERR;
89 else if (strcmp(tok, "debug") == 0)
90 mask |= LOG_DEBUG;
91 else if (strcmp(tok, "regs") == 0)
92 mask |= LOG_REGS;
93 else if (strcmp(tok, "states") == 0)
94 mask |= LOG_STATES;
95 else if (strcmp(tok, "conds") == 0)
96 mask |= LOG_CONDS;
97 else if (strcmp(tok, "regs_v") == 0)
98 mask |= LOG_REGS_V;
99 else if (strcmp(tok, "future") == 0)
100 mask |= LOG_FUTURE;
101 else if (strcmp(tok, "cycle") == 0)
102 mask |= LOG_CYCLE;
103 else if (strcmp(tok, "console") == 0)
104 mask |= LOG_CONSOLE;
105 else if (strcmp(tok, "peri") == 0)
106 mask |= LOG_PERI;
107 else if (strcmp(tok, "reader") == 0)
108 mask |= LOG_READER;
109 else if (strcmp(tok, "cmds") == 0)
110 mask |= LOG_CMDS;
111 else {
112 /* Unknown category: emit an error but do not abort. */
113 ge_log(LOG_ERR, "ge_log_set_active_types_from_spec: unknown category '%s'\n", tok);
114 }
115
116 p = end;
117 }
118
120}
121
122void ge_log(ge_log_type type, const char *format, ...)
123{
124 static char line[0x1000];
125 va_list args;
126
127 if (!(active_log_types & type))
128 return;
129
130 va_start (args, format);
131 vsnprintf(line, sizeof(line), format, args);
132 va_end (args);
133
134 printf(" %-7s ] %s", log_type_name(type), line);
135}
136
138 return !!(active_log_types & type);
139}
void ge_log_set_active_types_from_spec(const char *spec)
Set active log types from a comma-separated name specification.
Definition log.c:47
static ge_log_type active_log_types
Definition log.c:8
void ge_log_set_active_types(ge_log_type types)
Set active log types.
Definition log.c:42
void ge_log(ge_log_type type, const char *format,...)
Log message.
Definition log.c:122
uint8_t ge_log_enabled(ge_log_type type)
Check if a log type is enabled.
Definition log.c:137
static const char * log_type_name(ge_log_type type)
Definition log.c:10
int ge_log_type
Definition log.h:6
@ LOG_REGS_V
Register trace per pulse.
Definition log.h:23
@ LOG_ERR
Emulator unrecoverable condition.
Definition log.h:18
@ LOG_FUTURE
Future state network debug.
Definition log.h:24
@ LOG_REGS
Register trace per cycle.
Definition log.h:20
@ LOG_STATES
State trace.
Definition log.h:21
@ LOG_CONSOLE
Console interactions.
Definition log.h:26
@ LOG_CONDS
MSL conditions trace.
Definition log.h:22
@ LOG_CYCLE
Cycle attribution debug.
Definition log.h:25
@ LOG_CMDS
MSL commands trace.
Definition log.h:29
@ LOG_PERI
Peripherals IO.
Definition log.h:27
@ LOG_DEBUG
General detailed debug information.
Definition log.h:19
@ LOG_READER
Integrated Reader.
Definition log.h:28