Ticket #6168: 0001-Add-syslog-ng-support.patch

File 0001-Add-syslog-ng-support.patch, 2.9 KB (added by andreasf, 10 years ago)

proposed patch

  • src/system/boot/loader/stdio.cpp

    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  
    66
    77#include <boot/vfs.h>
    88#include <boot/stdio.h>
     9#include <boot/net/NetStack.h>
     10#include <boot/net/UDP.h>
    911#include <util/kernel_cpp.h>
    1012
    1113#include <errno.h>
     
    2123//extern FILE *stdin;
    2224
    2325
     26//#define ENABLE_SYSLOG
     27
     28
    2429#undef errno
    2530int errno;
    2631
    _errnop(void)  
    3237}
    3338
    3439
     40#ifdef ENABLE_SYSLOG
     41
     42static UDPSocket *sSyslogSocket = NULL;
     43
     44static void
     45sendToSyslog(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
    3584int
    3685vfprintf(FILE *file, const char *format, va_list list)
    3786{
    vfprintf(FILE *file, const char *format, va_list list)  
    4190
    4291    int length = vsnprintf(buffer, sizeof(buffer), format, list);
    4392    length = std::min(length, (int)sizeof(buffer) - 1);
    44     if (length > 0)
     93    if (length > 0) {
    4594        node->Write(buffer, length);
     95#ifdef ENABLE_SYSLOG
     96        sendToSyslog(buffer, length);
     97#endif
     98    }
    4699
    47100    return length;
    48101}
    fputc(int c, FILE *file)  
    93146    // we only support direct console output right now...
    94147    status = ((ConsoleNode *)file)->Write(&character, 1);
    95148
     149#ifdef ENABLE_SYSLOG
     150    sendToSyslog(&character, 1);
     151#endif
     152
    96153    if (status > 0)
    97154        return character;
    98155
    fputs(const char *string, FILE *file)  
    109166    status_t status = ((ConsoleNode *)file)->Write(string, strlen(string));
    110167    fputc('\n', file);
    111168
     169#ifdef ENABLE_SYSLOG
     170    sendToSyslog(string, strlen(string));
     171#endif
     172
    112173    return status;
    113174}
    114175