stop appending repeat keys to the input buffer
[retroray] / src / logger.c
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21 #include "logger.h"
22
23 #if defined(__MSDOS__) || defined(MSDOS)
24 static int setup_serial(int sdev);
25
26 void ser_putchar(int c);
27 void ser_puts(const char *s);
28 void ser_printf(const char *fmt, ...);
29 #else
30 #define USE_STD
31 #endif
32
33 enum { LOG_FILE, LOG_STREAM, LOG_CON, LOG_CB };
34 enum { LOG_DBG, LOG_INFO, LOG_WARN, LOG_ERR };
35
36 struct log_callback {
37         void (*func)(const char*, void*);
38         void *cls;
39 };
40
41 struct log_output {
42         int type, level;
43         union {
44                 FILE *fp;
45                 int con;
46                 struct log_callback cb;
47         } out;
48 };
49
50 #define MAX_OUTPUTS     8
51 static struct log_output outputs[MAX_OUTPUTS];
52 static int num_outputs;
53
54 void init_logger(void)
55 {
56         num_outputs = 0;
57 }
58
59 void cleanup_logger(void)
60 {
61         int i;
62
63         for(i=0; i<num_outputs; i++) {
64                 if(outputs[i].type == LOG_FILE) {
65                         fclose(outputs[i].out.fp);
66                 }
67         }
68         num_outputs = 0;
69 }
70
71 int add_log_file(const char *fname)
72 {
73         FILE *fp;
74         int idx;
75
76         if(num_outputs >= MAX_OUTPUTS) {
77                 return -1;
78         }
79         if(!(fp = fopen(fname, "w"))) {
80                 return -1;
81         }
82         idx = num_outputs++;
83
84         outputs[idx].type = LOG_FILE;
85         outputs[idx].out.fp = fp;
86         return 0;
87 }
88
89 int add_log_stream(FILE *fp)
90 {
91         int idx;
92
93         if(num_outputs >= MAX_OUTPUTS) {
94                 return -1;
95         }
96         idx = num_outputs++;
97
98         outputs[idx].type = LOG_STREAM;
99         outputs[idx].out.fp = fp;
100         return 0;
101 }
102
103 int add_log_console(const char *devname)
104 {
105 #if defined(MSDOS) || defined(__MSDOS__)
106         int i, comport;
107         if(sscanf(devname, "COM%d", &comport) != 1 || comport < 1 || comport > 2) {
108                 return -1;
109         }
110         comport--;
111
112         if(num_outputs >= MAX_OUTPUTS) {
113                 return -1;
114         }
115         for(i=0; i<num_outputs; i++) {
116                 if(outputs[i].type == LOG_CON && outputs[i].out.con == comport) {
117                         return -1;
118                 }
119         }
120         if(setup_serial(comport) == -1) {
121                 return -1;
122         }
123
124         i = num_outputs++;
125         outputs[i].type = LOG_CON;
126         outputs[i].out.con = comport;
127         return 0;
128
129 #elif defined(unix) || defined(__unix__)
130         /* TODO? */
131         return -1;
132 #endif
133 }
134
135 int add_log_callback(void (*cbfunc)(const char*, void*), void *cls)
136 {
137         int idx;
138
139         if(num_outputs >= MAX_OUTPUTS) {
140                 return -1;
141         }
142         idx = num_outputs++;
143
144         outputs[idx].type = LOG_CB;
145         outputs[idx].out.cb.func = cbfunc;
146         outputs[idx].out.cb.cls = cls;
147         return 0;
148 }
149
150 #if defined(__WATCOMC__)
151 #ifndef vsnprintf
152 #define vsnprintf _vsnprintf
153 #endif
154 #endif
155
156 static int logmsg(int type, const char *fmt, va_list ap)
157 {
158         static char buf[2048];
159         int i, len;
160
161         len = vsnprintf(buf, sizeof buf, fmt, ap);
162
163         for(i=0; i<num_outputs; i++) {
164                 switch(outputs[i].type) {
165                 case LOG_FILE:
166                 case LOG_STREAM:
167                         fputs(buf, outputs[i].out.fp);
168                         fflush(outputs[i].out.fp);
169                         break;
170
171 #if defined(MSDOS) || defined(__MSDOS__)
172                 case LOG_CON:
173                         ser_puts(buf);
174                         break;
175 #endif
176                 case LOG_CB:
177                         outputs[i].out.cb.func(buf, outputs[i].out.cb.cls);
178                         break;
179
180                 default:
181                         break;
182                 }
183         }
184
185         return len;
186 }
187
188 int errormsg(const char *fmt, ...)
189 {
190         int len;
191         va_list ap;
192         va_start(ap, fmt);
193         len = logmsg(LOG_ERR, fmt, ap);
194         va_end(ap);
195         return len;
196 }
197
198 int warnmsg(const char *fmt, ...)
199 {
200         int len;
201         va_list ap;
202         va_start(ap, fmt);
203         len = logmsg(LOG_WARN, fmt, ap);
204         va_end(ap);
205         return len;
206 }
207
208 int infomsg(const char *fmt, ...)
209 {
210         int len;
211         va_list ap;
212         va_start(ap, fmt);
213         len = logmsg(LOG_INFO, fmt, ap);
214         va_end(ap);
215         return len;
216 }
217
218 int dbgmsg(const char *fmt, ...)
219 {
220         int len;
221         va_list ap;
222         va_start(ap, fmt);
223         len = logmsg(LOG_DBG, fmt, ap);
224         va_end(ap);
225         return len;
226 }
227
228 int verrormsg(const char *fmt, va_list ap)
229 {
230         return logmsg(LOG_ERR, fmt, ap);
231 }
232
233 int vwarnmsg(const char *fmt, va_list ap)
234 {
235         return logmsg(LOG_ERR, fmt, ap);
236 }
237
238 int vinfomsg(const char *fmt, va_list ap)
239 {
240         return logmsg(LOG_ERR, fmt, ap);
241 }
242
243 int vdbgmsg(const char *fmt, va_list ap)
244 {
245         return logmsg(LOG_ERR, fmt, ap);
246 }
247
248
249 #if defined(MSDOS) || defined(__MSDOS__)
250 #include <conio.h>
251
252 #define UART1_BASE      0x3f8
253 #define UART2_BASE      0x2f8
254
255 #define UART_DATA       0
256 #define UART_DIVLO      0
257 #define UART_DIVHI      1
258 #define UART_FIFO       2
259 #define UART_LCTL       3
260 #define UART_MCTL       4
261 #define UART_LSTAT      5
262
263 #define DIV_9600                        (115200 / 9600)
264 #define DIV_38400                       (115200 / 38400)
265 #define LCTL_8N1                        0x03
266 #define LCTL_DLAB                       0x80
267 #define FIFO_ENABLE_CLEAR       0x07
268 #define MCTL_DTR_RTS_OUT2       0x0b
269 #define LST_TRIG_EMPTY          0x20
270
271 static unsigned int iobase;
272
273 static int setup_serial(int sdev)
274 {
275         if(sdev < 0 || sdev > 1) {
276                 return -1;
277         }
278         iobase = sdev == 0 ? UART1_BASE : UART2_BASE;
279
280         /* set clock divisor */
281         outp(iobase | UART_LCTL, LCTL_DLAB);
282         outp(iobase | UART_DIVLO, DIV_9600 & 0xff);
283         outp(iobase | UART_DIVHI, DIV_9600 >> 8);
284         /* set format 8n1 */
285         outp(iobase | UART_LCTL, LCTL_8N1);
286         /* assert RTS and DTR */
287         outp(iobase | UART_MCTL, MCTL_DTR_RTS_OUT2);
288         return 0;
289 }
290
291 void ser_putchar(int c)
292 {
293         if(c == '\n') {
294                 ser_putchar('\r');
295         }
296
297         while((inp(iobase | UART_LSTAT) & LST_TRIG_EMPTY) == 0);
298         outp(iobase | UART_DATA, c);
299 }
300
301 void ser_puts(const char *s)
302 {
303         while(*s) {
304                 ser_putchar(*s++);
305         }
306 }
307
308 void ser_printf(const char *fmt, ...)
309 {
310         va_list ap;
311         char buf[512];
312
313         va_start(ap, fmt);
314         vsprintf(buf, fmt, ap);
315         va_end(ap);
316
317         ser_puts(buf);
318 }
319 #endif