]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/io/console/misc.cc
minor doc updates
[polintos/scott/priv.git] / kernel / io / console / misc.cc
1 // io/console/misc.cc -- Generic code relating to kernel debug consoles
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
8 // 
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
14
15 #include <kern/console.h>
16 #include <kern/libc.h>
17 #include <util/spinlock.h>
18
19 namespace IO {
20 namespace Console {
21         void Console::write(const char *data, size_t len)
22         {
23                 u64 len64 = len;
24                 write(Array<octet, ORBMM>((octet *)data, len), &len64);
25         }
26         
27         // All consoles will set this to themselves; the last console
28         // to be initialized will be used.  At some point, the console
29         // used will be configurable (including the ability to broadcast
30         // to multiple consoles).
31         
32         Console *primary_console;
33 }
34 }
35
36 static const size_t printf_buffer_size = 4096;
37 static char printf_buffer[printf_buffer_size];
38 Lock::SpinLock printf_lock;
39
40 // Note: this will not retry with a larger buffer if overflow
41 // happens.  If you really need that, you'll need to call snprintf
42 // and primary_console->write yourself.
43
44 size_t printf(const char *str, ...)
45 {
46         Lock::AutoSpinLockRecIRQ autolock(printf_lock);
47
48         va_list args;
49         va_start(args, str);
50         size_t ret = vsnprintf(printf_buffer, printf_buffer_size, str, args);
51         va_end(args);
52         
53         if (ret > printf_buffer_size)
54                 ret = printf_buffer_size;
55         
56         IO::Console::primary_console->write(printf_buffer, ret);
57         return ret;
58 }