Fixed the length of the command line.
[reactos.git] / freeldr / freeldr / debug.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 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 <rtl.h>
23 #include <comm.h>
24
25 #ifdef DEBUG
26
27 //ULONG DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY | DPRINT_FILESYSTEM |
28 // DPRINT_UI | DPRINT_DISK | DPRINT_CACHE;
29 ULONG DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY | DPRINT_FILESYSTEM |
30 DPRINT_UI | DPRINT_DISK;
31 //ULONG DebugPrintMask = DPRINT_CACHE;
32
33 #define SCREEN 0
34 #define RS232 1
35 #define BOCHS 2
36
37 #define COM1 1
38 #define COM2 2
39 #define COM3 3
40 #define COM4 4
41
42 #define BOCHS_OUTPUT_PORT 0xe9
43
44 ULONG DebugPort = RS232;
45 //ULONG DebugPort = SCREEN;
46 //ULONG DebugPort = BOCHS;
47 ULONG ComPort = COM1;
48 //ULONG BaudRate = 19200;
49 ULONG BaudRate = 115200;
50
51 BOOL DebugStartOfLine = TRUE;
52
53 VOID DebugInit(VOID)
54 {
55 if (DebugPort == RS232)
56 {
57 Rs232PortInitialize(ComPort, BaudRate);
58 }
59 }
60
61 VOID DebugPrintChar(UCHAR Character)
62 {
63 if (Character == '\n')
64 {
65 DebugStartOfLine = TRUE;
66 }
67
68 if (DebugPort == RS232)
69 {
70 if (Character == '\n')
71 {
72 Rs232PortPutByte('\r');
73 }
74 Rs232PortPutByte(Character);
75 }
76 else if (DebugPort == BOCHS)
77 {
78 WRITE_PORT_UCHAR((PUCHAR)BOCHS_OUTPUT_PORT, Character);
79 }
80 else
81 {
82 putchar(Character);
83 }
84 }
85
86 VOID DebugPrintHeader(ULONG Mask)
87 {
88 switch (Mask)
89 {
90 case DPRINT_WARNING:
91 DebugPrintChar('W');
92 DebugPrintChar('A');
93 DebugPrintChar('R');
94 DebugPrintChar('N');
95 DebugPrintChar('I');
96 DebugPrintChar('N');
97 DebugPrintChar('G');
98 DebugPrintChar(':');
99 DebugPrintChar(' ');
100 break;
101 case DPRINT_MEMORY:
102 DebugPrintChar('M');
103 DebugPrintChar('E');
104 DebugPrintChar('M');
105 DebugPrintChar('O');
106 DebugPrintChar('R');
107 DebugPrintChar('Y');
108 DebugPrintChar(':');
109 DebugPrintChar(' ');
110 break;
111 case DPRINT_FILESYSTEM:
112 DebugPrintChar('F');
113 DebugPrintChar('I');
114 DebugPrintChar('L');
115 DebugPrintChar('E');
116 DebugPrintChar('S');
117 DebugPrintChar('Y');
118 DebugPrintChar('S');
119 DebugPrintChar(':');
120 DebugPrintChar(' ');
121 break;
122 case DPRINT_INIFILE:
123 DebugPrintChar('I');
124 DebugPrintChar('N');
125 DebugPrintChar('I');
126 DebugPrintChar('F');
127 DebugPrintChar('I');
128 DebugPrintChar('L');
129 DebugPrintChar('E');
130 DebugPrintChar(':');
131 DebugPrintChar(' ');
132 break;
133 case DPRINT_UI:
134 DebugPrintChar('U');
135 DebugPrintChar('I');
136 DebugPrintChar(':');
137 DebugPrintChar(' ');
138 break;
139 case DPRINT_DISK:
140 DebugPrintChar('D');
141 DebugPrintChar('I');
142 DebugPrintChar('S');
143 DebugPrintChar('K');
144 DebugPrintChar(':');
145 DebugPrintChar(' ');
146 break;
147 case DPRINT_CACHE:
148 DebugPrintChar('C');
149 DebugPrintChar('A');
150 DebugPrintChar('C');
151 DebugPrintChar('H');
152 DebugPrintChar('E');
153 DebugPrintChar(':');
154 DebugPrintChar(' ');
155 break;
156 default:
157 DebugPrintChar('U');
158 DebugPrintChar('N');
159 DebugPrintChar('K');
160 DebugPrintChar('N');
161 DebugPrintChar('O');
162 DebugPrintChar('W');
163 DebugPrintChar('N');
164 DebugPrintChar(':');
165 DebugPrintChar(' ');
166 break;
167 }
168 }
169
170 VOID DebugPrint(ULONG Mask, char *format, ...)
171 {
172 int *dataptr = (int *) &format;
173 char c, *ptr, str[16];
174
175 // Mask out unwanted debug messages
176 if (!(Mask & DebugPrintMask))
177 {
178 return;
179 }
180
181 // Print the header if we have started a new line
182 if (DebugStartOfLine)
183 {
184 DebugPrintHeader(Mask);
185 DebugStartOfLine = FALSE;
186 }
187
188 dataptr++;
189
190 while ((c = *(format++)))
191 {
192 if (c != '%')
193 {
194 DebugPrintChar(c);
195 }
196 else
197 {
198 switch (c = *(format++))
199 {
200 case 'd': case 'u': case 'x':
201
202 *convert_to_ascii(str, c, *((unsigned long *) dataptr++)) = 0;
203
204 ptr = str;
205
206 while (*ptr)
207 {
208 DebugPrintChar(*(ptr++));
209 }
210 break;
211
212 case 'c':
213
214 DebugPrintChar((*(dataptr++))&0xff);
215 break;
216
217 case 's':
218
219 ptr = (char *)(*(dataptr++));
220
221 while ((c = *(ptr++)))
222 {
223 DebugPrintChar(c);
224 }
225 break;
226 }
227 }
228 }
229
230
231 if (DebugPort == SCREEN)
232 {
233 //getch();
234 }
235
236 }
237
238 #endif // defined DEBUG