Skip to content

Commit

Permalink
Fix syslog messages #285
Browse files Browse the repository at this point in the history
  • Loading branch information
fvanroie committed Jan 24, 2022
1 parent 80b9f06 commit f28d99d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
37 changes: 5 additions & 32 deletions src/hasp_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,6 @@ static void debugPrintLvglMemory(int level, Print* _logOutput)

static void debugPrintPriority(int level, Print* _logOutput)
{
// if(_logOutput == &syslogClient) {
// }

switch(level) {
case LOG_LEVEL_FATAL ... LOG_LEVEL_ERROR:
debugSendAnsiCode(F(TERM_COLOR_RED), _logOutput);
Expand Down Expand Up @@ -414,35 +411,12 @@ static void debugPrintPriority(int level, Print* _logOutput)
void debugPrintPrefix(uint8_t tag, int level, Print* _logOutput)
{
char buffer[10];
debug_get_tag(tag, buffer);

#if 0 && HASP_USE_SYSLOG > 0

if(_logOutput == syslogClient && syslogClient) {
if(syslogClient->beginPacket(debugSyslogHost, debugSyslogPort)) {

// IETF Doc: https://tools.ietf.org/html/rfc5424 - The Syslog Protocol
// BSD Doc: https://tools.ietf.org/html/rfc3164 - The BSD syslog Protocol

syslogClient->print(F("<"));
syslogClient->print((16 + debugSyslogFacility) * 8 + level);
syslogClient->print(F(">"));

if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
syslogClient->print(F("1 - "));
}

debug_get_tag(tag, buffer);
syslogClient->print(F("%s %s"), haspDevice.get_hostname(), buffer);

if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
syslogClient->print(F(" - - - \xEF\xBB\xBF")); // include UTF-8 BOM
} else {
syslogClient->print(F(": "));
}

debugPrintHaspMemory(level, _logOutput);
debugPrintLvglMemory(level, _logOutput);
}
#if HASP_USE_SYSLOG > 0
if(debugSyslogPrefix(tag, level, _logOutput, buffer)) {
debugPrintHaspMemory(level, _logOutput);
debugPrintLvglMemory(level, _logOutput);
return;
}
#endif // HASP_USE_SYSLOG
Expand All @@ -460,7 +434,6 @@ void debugPrintPrefix(uint8_t tag, int level, Print* _logOutput)
debugPrintPriority(level, _logOutput);
}

debug_get_tag(tag, buffer);
#ifdef ARDUINO
_logOutput->printf(PSTR(" %s: "), buffer);
#else
Expand Down
1 change: 1 addition & 0 deletions src/hasp_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void debugStop(void);
void debugPrintHaspHeader(Print* output);
void debugPrintTag(uint8_t tag, Print* _logOutput);
void debugPrintPrefix(uint8_t tag, int level, Print* _logOutput);
bool debugSyslogPrefix(uint8_t tag, int level, Print* _logOutput, const char* processname);

#ifdef __cplusplus
}
Expand Down
56 changes: 54 additions & 2 deletions src/log/hasp_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ uint8_t debugSyslogFacility = 0;
uint8_t debugSyslogProtocol = 0;

// A UDP instance to let us send and receive packets over UDP
WiFiUDP* syslogClient;
WiFiUDP* syslogClient = NULL;
#define SYSLOG_PROTO_IETF 0

// Create a new syslog instance with LOG_KERN facility
Expand Down Expand Up @@ -268,10 +268,62 @@ void debugGetHistoryLine(size_t num)
}
*/

bool debugSyslogPrefix(uint8_t tag, int level, Print* _logOutput, const char* processname)
{

#if HASP_USE_SYSLOG > 0

if(syslogClient && _logOutput == syslogClient) {
if(syslogClient->beginPacket(debugSyslogHost, debugSyslogPort)) {

// IETF Doc: https://tools.ietf.org/html/rfc5424 - The Syslog Protocol
// BSD Doc: https://tools.ietf.org/html/rfc3164 - The BSD syslog Protocol
char buffer[32 + STR_LEN_HOSTNAME];
int len;
uint priority = (16 + debugSyslogFacility) * 8 + level;

if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
len = snprintf_P(buffer, sizeof(buffer), PSTR("<%d>1 - %s %s - - \xEF\xBB\xBF"), priority,
haspDevice.get_hostname(), processname);
} else {
len = snprintf_P(buffer, sizeof(buffer), PSTR("<%d>%s %s: "), priority, haspDevice.get_hostname(),
processname);
}

if(len > 0) syslogClient->write((uint8_t*)buffer, len);

// syslogClient->print(F("<"));
// syslogClient->print((16 + debugSyslogFacility) * 8 + level);
// syslogClient->print(F(">"));

// if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
// syslogClient->print(F("1 - "));
// }

// // debug_get_tag(tag, buffer);
// char buffer[10];
// syslogClient->print(F("%s %s"), haspDevice.get_hostname(), buffer);

// if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
// syslogClient->print(F(" - - - \xEF\xBB\xBF")); // include UTF-8 BOM
// } else {
// syslogClient->print(F(": "));
// }

// debugPrintHaspMemory(level, _logOutput);
// debugPrintLvglMemory(level, _logOutput);
}
return true;
}
#endif // HASP_USE_SYSLOG

return false;
}

void debugPrintSuffix(uint8_t tag, int level, Print* _logOutput)
{
#if HASP_USE_SYSLOG > 0
if(_logOutput == syslogClient && syslogClient) {
if(syslogClient && _logOutput == syslogClient) {
syslogClient->endPacket();
return;
}
Expand Down

0 comments on commit f28d99d

Please sign in to comment.