Add syslog output support to bootloader

Broadcast console output as syslog debug messages, stripped of trailing newlines.
Disabled by default.

Patch from #6168.

Change-Id: I1754381477dcde00bdcfa4482017daf5eb682cd3
Reviewed-on: https://review.haiku-os.org/c/912
Reviewed-by: Stephan Aßmus <[email protected]>
This commit is contained in:
Andreas Faerber
2019-01-27 16:51:32 +00:00
committed by Stephan Aßmus
parent f551180bf9
commit 9091349139
+63 -1
View File
@@ -6,6 +6,8 @@
#include <boot/vfs.h>
#include <boot/stdio.h>
#include <boot/net/NetStack.h>
#include <boot/net/UDP.h>
#include <errno.h>
#include <stdarg.h>
@@ -20,6 +22,9 @@
//extern FILE *stdin;
//#define ENABLE_SYSLOG
#undef errno
int errno;
@@ -31,6 +36,51 @@ _errnop(void)
}
#ifdef ENABLE_SYSLOG
static UDPSocket *sSyslogSocket = NULL;
static void
sendToSyslog(const char *message, int length)
{
// Lazy-initialize the socket
if (sSyslogSocket == NULL) {
// Check if the network stack has been initialized yet
if (NetStack::Default() != NULL) {
sSyslogSocket = new(std::nothrow) UDPSocket;
sSyslogSocket->Bind(INADDR_ANY, 60514);
}
}
if (sSyslogSocket == NULL)
return;
// Strip trailing newlines
while (length > 0) {
if (message[length - 1] != '\n'
&& message[length - 1] != '\r') {
break;
}
length--;
}
if (length <= 0)
return;
char buffer[1500];
// same comment as in vfprintf applies...
const int facility = 0; // kernel
int severity = 7; // debug
int offset = snprintf(buffer, sizeof(buffer),
"<%d>1 - - Haiku - - - \xEF\xBB\xBF",
facility * 8 + severity);
length = std::min(length, (int)sizeof(buffer) - offset);
memcpy(buffer + offset, message, length);
sSyslogSocket->Send(INADDR_BROADCAST, 514, buffer, offset + length);
}
#endif
int
vfprintf(FILE *file, const char *format, va_list list)
{
@@ -40,8 +90,12 @@ vfprintf(FILE *file, const char *format, va_list list)
int length = vsnprintf(buffer, sizeof(buffer), format, list);
length = std::min(length, (int)sizeof(buffer) - 1);
if (length > 0)
if (length > 0) {
node->Write(buffer, length);
#ifdef ENABLE_SYSLOG
sendToSyslog(buffer, length);
#endif
}
return length;
}
@@ -92,6 +146,10 @@ fputc(int c, FILE *file)
// we only support direct console output right now...
status = ((ConsoleNode *)file)->Write(&character, 1);
#ifdef ENABLE_SYSLOG
sendToSyslog(&character, 1);
#endif
if (status > 0)
return character;
@@ -108,6 +166,10 @@ fputs(const char *string, FILE *file)
status_t status = ((ConsoleNode *)file)->Write(string, strlen(string));
fputc('\n', file);
#ifdef ENABLE_SYSLOG
sendToSyslog(string, strlen(string));
#endif
return status;
}