From 6598e10b4535f0c7e74e22e5d3b79eb546973d2a Mon Sep 17 00:00:00 2001
From: Andreas Faerber <andreas.faerber@web.de>
Date: Sun, 13 Jun 2010 22:17:40 +0200
Subject: [PATCH] Add syslog-ng support
Broadcast console output as syslog debug messages, stripped of trailing newlines.
Disabled by default.
---
src/system/boot/loader/stdio.cpp | 63 +++++++++++++++++++++++++++++++++++++-
1 files changed, 62 insertions(+), 1 deletions(-)
diff --git a/src/system/boot/loader/stdio.cpp b/src/system/boot/loader/stdio.cpp
index ca430ea..96bba2b 100644
a
|
b
|
|
6 | 6 | |
7 | 7 | #include <boot/vfs.h> |
8 | 8 | #include <boot/stdio.h> |
| 9 | #include <boot/net/NetStack.h> |
| 10 | #include <boot/net/UDP.h> |
9 | 11 | #include <util/kernel_cpp.h> |
10 | 12 | |
11 | 13 | #include <errno.h> |
… |
… |
|
21 | 23 | //extern FILE *stdin; |
22 | 24 | |
23 | 25 | |
| 26 | //#define ENABLE_SYSLOG |
| 27 | |
| 28 | |
24 | 29 | #undef errno |
25 | 30 | int errno; |
26 | 31 | |
… |
… |
_errnop(void)
|
32 | 37 | } |
33 | 38 | |
34 | 39 | |
| 40 | #ifdef ENABLE_SYSLOG |
| 41 | |
| 42 | static UDPSocket *sSyslogSocket = NULL; |
| 43 | |
| 44 | static void |
| 45 | sendToSyslog(const char *message, int length) |
| 46 | { |
| 47 | // Lazy-initialize the socket |
| 48 | if (sSyslogSocket == NULL) { |
| 49 | // Check if the network stack has been initialized yet |
| 50 | if (NetStack::Default() != NULL) { |
| 51 | sSyslogSocket = new(std::nothrow) UDPSocket; |
| 52 | sSyslogSocket->Bind(INADDR_ANY, 60514); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if (sSyslogSocket == NULL) |
| 57 | return; |
| 58 | |
| 59 | // Strip trailing newlines |
| 60 | while (length > 0) { |
| 61 | if (message[length - 1] != '\n' |
| 62 | && message[length - 1] != '\r') { |
| 63 | break; |
| 64 | } |
| 65 | length--; |
| 66 | } |
| 67 | if (length <= 0) |
| 68 | return; |
| 69 | |
| 70 | char buffer[1500]; |
| 71 | // same comment as in vfprintf applies... |
| 72 | const int facility = 0; // kernel |
| 73 | int severity = 7; // debug |
| 74 | int offset = snprintf(buffer, sizeof(buffer), "<%d>1 - - Haiku - - - \xEF\xBB\xBF", |
| 75 | facility * 8 + severity); |
| 76 | length = std::min(length, (int)sizeof(buffer) - offset); |
| 77 | memcpy(buffer + offset, message, length); |
| 78 | sSyslogSocket->Send(INADDR_BROADCAST, 514, buffer, offset + length); |
| 79 | } |
| 80 | |
| 81 | #endif |
| 82 | |
| 83 | |
35 | 84 | int |
36 | 85 | vfprintf(FILE *file, const char *format, va_list list) |
37 | 86 | { |
… |
… |
vfprintf(FILE *file, const char *format, va_list list)
|
41 | 90 | |
42 | 91 | int length = vsnprintf(buffer, sizeof(buffer), format, list); |
43 | 92 | length = std::min(length, (int)sizeof(buffer) - 1); |
44 | | if (length > 0) |
| 93 | if (length > 0) { |
45 | 94 | node->Write(buffer, length); |
| 95 | #ifdef ENABLE_SYSLOG |
| 96 | sendToSyslog(buffer, length); |
| 97 | #endif |
| 98 | } |
46 | 99 | |
47 | 100 | return length; |
48 | 101 | } |
… |
… |
fputc(int c, FILE *file)
|
93 | 146 | // we only support direct console output right now... |
94 | 147 | status = ((ConsoleNode *)file)->Write(&character, 1); |
95 | 148 | |
| 149 | #ifdef ENABLE_SYSLOG |
| 150 | sendToSyslog(&character, 1); |
| 151 | #endif |
| 152 | |
96 | 153 | if (status > 0) |
97 | 154 | return character; |
98 | 155 | |
… |
… |
fputs(const char *string, FILE *file)
|
109 | 166 | status_t status = ((ConsoleNode *)file)->Write(string, strlen(string)); |
110 | 167 | fputc('\n', file); |
111 | 168 | |
| 169 | #ifdef ENABLE_SYSLOG |
| 170 | sendToSyslog(string, strlen(string)); |
| 171 | #endif |
| 172 | |
112 | 173 | return status; |
113 | 174 | } |
114 | 175 | |