6 #if defined(__MSDOS__) || defined(MSDOS)
9 static int setup_serial(int sdev);
11 void ser_putchar(int c);
12 void ser_puts(const char *s);
13 void ser_printf(const char *fmt, ...);
18 enum { LOG_FILE, LOG_STREAM, LOG_CON, LOG_CB };
19 enum { LOG_DBG, LOG_INFO, LOG_WARN, LOG_ERR };
22 void (*func)(const char*, void*);
31 struct log_callback cb;
36 static struct log_output outputs[MAX_OUTPUTS];
37 static int num_outputs;
39 void init_logger(void)
44 void cleanup_logger(void)
48 for(i=0; i<num_outputs; i++) {
49 if(outputs[i].type == LOG_FILE) {
50 fclose(outputs[i].out.fp);
56 int add_log_file(const char *fname)
61 if(num_outputs >= MAX_OUTPUTS) {
64 if(!(fp = fopen(fname, "w"))) {
69 outputs[idx].type = LOG_FILE;
70 outputs[idx].out.fp = fp;
74 int add_log_stream(FILE *fp)
78 if(num_outputs >= MAX_OUTPUTS) {
83 outputs[idx].type = LOG_STREAM;
84 outputs[idx].out.fp = fp;
88 int add_log_console(const char *devname)
90 #if defined(MSDOS) || defined(__MSDOS__)
92 if(sscanf(devname, "COM%d", &comport) != 1 || comport < 1 || comport > 2) {
97 if(num_outputs >= MAX_OUTPUTS) {
100 for(i=0; i<num_outputs; i++) {
101 if(outputs[i].type == LOG_CON && outputs[i].out.con == comport) {
105 if(setup_serial(comport) == -1) {
110 outputs[i].type = LOG_CON;
111 outputs[i].out.con = comport;
114 #elif defined(unix) || defined(__unix__)
120 int add_log_callback(void (*cbfunc)(const char*, void*), void *cls)
124 if(num_outputs >= MAX_OUTPUTS) {
129 outputs[idx].type = LOG_CB;
130 outputs[idx].out.cb.func = cbfunc;
131 outputs[idx].out.cb.cls = cls;
135 #if defined(__WATCOMC__)
137 #define vsnprintf _vsnprintf
141 static int logmsg(int type, const char *fmt, va_list ap)
143 static char buf[2048];
146 len = vsnprintf(buf, sizeof buf, fmt, ap);
148 for(i=0; i<num_outputs; i++) {
149 switch(outputs[i].type) {
152 fputs(buf, outputs[i].out.fp);
153 fflush(outputs[i].out.fp);
156 #if defined(MSDOS) || defined(__MSDOS__)
162 outputs[i].out.cb.func(buf, outputs[i].out.cb.cls);
173 int errormsg(const char *fmt, ...)
178 len = logmsg(LOG_ERR, fmt, ap);
183 int warnmsg(const char *fmt, ...)
188 len = logmsg(LOG_WARN, fmt, ap);
193 int infomsg(const char *fmt, ...)
198 len = logmsg(LOG_INFO, fmt, ap);
203 int dbgmsg(const char *fmt, ...)
208 len = logmsg(LOG_DBG, fmt, ap);
213 int verrormsg(const char *fmt, va_list ap)
215 return logmsg(LOG_ERR, fmt, ap);
218 int vwarnmsg(const char *fmt, va_list ap)
220 return logmsg(LOG_ERR, fmt, ap);
223 int vinfomsg(const char *fmt, va_list ap)
225 return logmsg(LOG_ERR, fmt, ap);
228 int vdbgmsg(const char *fmt, va_list ap)
230 return logmsg(LOG_ERR, fmt, ap);
234 #if defined(MSDOS) || defined(__MSDOS__)
237 #define UART1_BASE 0x3f8
238 #define UART2_BASE 0x2f8
248 #define DIV_9600 (115200 / 9600)
249 #define DIV_38400 (115200 / 38400)
250 #define LCTL_8N1 0x03
251 #define LCTL_DLAB 0x80
252 #define FIFO_ENABLE_CLEAR 0x07
253 #define MCTL_DTR_RTS_OUT2 0x0b
254 #define LST_TRIG_EMPTY 0x20
256 static unsigned int iobase;
258 static int setup_serial(int sdev)
260 if(sdev < 0 || sdev > 1) {
263 iobase = sdev == 0 ? UART1_BASE : UART2_BASE;
265 /* set clock divisor */
266 outp(iobase | UART_LCTL, LCTL_DLAB);
267 outp(iobase | UART_DIVLO, DIV_9600 & 0xff);
268 outp(iobase | UART_DIVHI, DIV_9600 >> 8);
270 outp(iobase | UART_LCTL, LCTL_8N1);
271 /* assert RTS and DTR */
272 outp(iobase | UART_MCTL, MCTL_DTR_RTS_OUT2);
276 void ser_putchar(int c)
282 while((inp(iobase | UART_LSTAT) & LST_TRIG_EMPTY) == 0);
283 outp(iobase | UART_DATA, c);
286 void ser_puts(const char *s)
293 void ser_printf(const char *fmt, ...)
299 vsprintf(buf, fmt, ap);