1 // Implementation of cprintf console output for user environments,
2 // based on printfmt() and the sys_cputs() system call.
4 // cprintf is a debugging statement, not a generic output statement.
5 // It is very important that it always go to the console, especially when
6 // debugging file descriptor code!
10 #include <inc/stdarg.h>
14 // Collect up to 256 characters into a buffer
15 // and perform ONE system call to print all of them,
16 // in order to make the lines output to the console atomic
17 // and prevent interrupts from causing context switches
18 // in the middle of a console output line and such.
20 int idx
; // current buffer index
21 int cnt
; // total bytes printed so far
27 putch(int ch
, void *ptr
)
29 struct printbuf
*b
= (struct printbuf
*) ptr
;
30 b
->buf
[b
->idx
++] = ch
;
31 if (b
->idx
== 256-1) {
32 sys_cputs(b
->buf
, b
->idx
);
39 vcprintf(const char *fmt
, va_list ap
)
45 vprintfmt(putch
, &b
, fmt
, ap
);
46 sys_cputs(b
.buf
, b
.idx
);
52 cprintf(const char *fmt
, ...)
58 cnt
= vcprintf(fmt
, ap
);