e65c12bdb4efbc26b5608c4986bdbc8efd59546f
[reactos.git] / freeldr / freeldr / debug.c
1 /*
2 * FreeLoader
3 * Copyright (C) 2001 Brian Palmer <brianp@sginet.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 2 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, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "freeldr.h"
21 #include "debug.h"
22 #include "stdlib.h"
23 #include "rs232.h"
24 #include "parseini.h"
25
26 #ifdef DEBUG
27
28 ULONG DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY;
29
30 #define SCREEN 0
31 #define RS232 1
32
33 #define COM1 1
34 #define COM2 2
35 #define COM3 3
36 #define COM4 4
37
38 ULONG DebugPort = RS232; //SCREEN;
39 ULONG ComPort = COM1;
40 ULONG BaudRate = 19200;
41
42 VOID DebugInit(VOID)
43 {
44 if (DebugPort == RS232)
45 {
46 Rs232PortInitialize(ComPort, BaudRate);
47 }
48 }
49
50 void DebugPrint(ULONG Mask, char *format, ...)
51 {
52 int *dataptr = (int *) &format;
53 char c, *ptr, str[16];
54 char buffer[512];
55 char *p = buffer;
56 int i;
57
58 // Mask out unwanted debug messages
59 if (!(Mask & DebugPrintMask))
60 {
61 return;
62 }
63
64 dataptr++;
65
66 while ((c = *(format++)))
67 {
68 if (c != '%')
69 {
70 *p = c;
71 p++;
72 }
73 else
74 switch (c = *(format++))
75 {
76 case 'd': case 'u': case 'x':
77 *convert_to_ascii(str, c, *((unsigned long *) dataptr++)) = 0;
78
79 ptr = str;
80
81 while (*ptr)
82 {
83 *p = *(ptr++);
84 p++;
85 }
86 break;
87
88 case 'c':
89 *p = (*(dataptr++))&0xff;
90 p++;
91 break;
92
93 case 's':
94 ptr = (char *)(*(dataptr++));
95
96 while ((c = *(ptr++)))
97 {
98 *p = c;
99 p++;
100 }
101 break;
102 }
103 }
104 *p=0;
105
106
107 if (DebugPort == RS232)
108 {
109 for (i=0; buffer[i] != 0; i++)
110 {
111 Rs232PortPutByte(buffer[i]);
112 if (buffer[i] == '\n')
113 {
114 Rs232PortPutByte('\r');
115 }
116 }
117 }
118 else
119 {
120 print(buffer);
121 }
122 }
123
124 #endif // defined DEBUG