-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
add syslog function to wled #4664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KrX3D
wants to merge
19
commits into
wled:main
Choose a base branch
from
KrX3D:syslog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
25c4c5b
add syslog function to wled
KrX3D afccadd
Update wled00/syslog.cpp
KrX3D a48b05e
added coderabbitai suggestions like:
KrX3D 070b6b2
inject html to settings_sync.htm only when syslog enabled to save siz…
KrX3D cbee84e
coderabbitai improvements
KrX3D 4c9b735
hide feature was removed since it no longer needed
KrX3D 3007dcf
more coderabbitai improvements
KrX3D 14910fc
more coderabbitai improvements
KrX3D 0f50d28
more coderabbitai improvements
KrX3D f72815d
removed configuration options that do not add value
KrX3D ad5d0ab
doSerializeConfig -> configNeedsWrite
KrX3D e56abf2
remvoed Facility, Severity from GUI
KrX3D dada672
- Fix ompiling porblem with esp32_dev board
KrX3D 056ac1c
moved strings to flash - suggestion by DjordjeMandic
KrX3D 11f5dd2
removed (commented out) not used features - request netmindz
KrX3D 51bd04a
added comments by coderabbitai request
KrX3D a77b85a
changes suggested by netmindz
KrX3D 238e1c4
syslog inject fix / coderabbit improments
KrX3D 77c8f6d
removed forgotten commented out code
KrX3D File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| #include "wled.h" | ||
| #ifdef WLED_ENABLE_SYSLOG | ||
|
|
||
| #include "syslog.h" | ||
|
|
||
| static const __FlashStringHelper* protoNames[] = { F("BSD"), F("RFC5424"), F("RAW") }; | ||
| static const char* facilityNames[] = { | ||
| "KERN", "USER", "MAIL", "DAEM", | ||
| "AUTH", "SYSL", "LPR", "NEWS", | ||
| "UUCP", "CRON", "APRV", "FTP", | ||
| "NTP", "AUDT", "ALRT", "CLCK", | ||
| "LCL0", "LCL1", "LCL2", "LCL3", | ||
| "LCL4", "LCL5", "LCL6", "LCL7" | ||
| }; | ||
|
|
||
| static const char* severityNames[] = { | ||
| "EMERG","ALERT","CRIT","ERR","WARNING","NOTICE","INFO","DEBUG" | ||
| }; | ||
|
|
||
| SyslogPrinter::SyslogPrinter() : | ||
| _facility(SYSLOG_LOCAL4), | ||
| _severity(SYSLOG_DEBUG), | ||
| _protocol(SYSLOG_PROTO_BSD), | ||
| _appName("WLED"), | ||
| _bufferIndex(0) {} | ||
KrX3D marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| void SyslogPrinter::begin(const char* host, uint16_t port, | ||
| uint8_t facility, uint8_t severity, uint8_t protocol) { | ||
|
|
||
| DEBUG_PRINTF_P(PSTR("===== WLED SYSLOG CONFIGURATION =====\n")); | ||
| DEBUG_PRINTF_P(PSTR(" Hostname: %s\n"), syslogHost); | ||
| DEBUG_PRINTF_P(PSTR(" Cached IP: %s\n"), syslogHostIP.toString().c_str()); | ||
| DEBUG_PRINTF_P(PSTR(" Port: %u\n"), (unsigned)syslogPort); | ||
| DEBUG_PRINTF_P(PSTR(" Protocol: %u (%s)\n"), | ||
| protocol, | ||
| protocol <= 2 ? (const char*)protoNames[protocol] : "UNKNOWN" | ||
| ); | ||
| DEBUG_PRINTF_P(PSTR(" Facility: %u (%s)\n"), | ||
| (unsigned)facility, | ||
| (facility < sizeof(facilityNames)/sizeof(facilityNames[0])) | ||
| ? facilityNames[facility] | ||
| : PSTR("UNKNOWN")); | ||
| DEBUG_PRINTF_P(PSTR(" Severity: %u (%s)\n"), | ||
| (unsigned)severity, | ||
| (severity < sizeof(severityNames)/sizeof(severityNames[0])) | ||
| ? severityNames[severity] | ||
| : PSTR("UNKNOWN")); | ||
| DEBUG_PRINTF_P(PSTR("======================================\n")); | ||
KrX3D marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| strlcpy(syslogHost, host, sizeof(syslogHost)); | ||
| syslogPort = port; | ||
KrX3D marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _facility = facility; | ||
| _severity = severity; | ||
| _protocol = protocol; | ||
|
|
||
| // clear any cached IP so resolveHostname() will run next write() | ||
| syslogHostIP = IPAddress(0,0,0,0); | ||
| } | ||
|
|
||
| void SyslogPrinter::setAppName(const String &appName) { | ||
| _appName = appName; | ||
| } | ||
|
|
||
| bool SyslogPrinter::resolveHostname() { | ||
| if (!WLED_CONNECTED || !syslogEnabled) return false; | ||
|
|
||
| // If we already have an IP or can parse the hostname as an IP, use that | ||
| if (syslogHostIP || syslogHostIP.fromString(syslogHost)) { | ||
| return true; | ||
| } | ||
|
|
||
| // Otherwise resolve the hostname | ||
| #ifdef ESP8266 | ||
| WiFi.hostByName(syslogHost, syslogHostIP, 750); | ||
| #else | ||
| #ifdef WLED_USE_ETHERNET | ||
| ETH.hostByName(syslogHost, syslogHostIP); | ||
| #else | ||
| WiFi.hostByName(syslogHost, syslogHostIP); | ||
| #endif | ||
| #endif | ||
|
|
||
| return syslogHostIP != IPAddress(0, 0, 0, 0); | ||
| } | ||
|
|
||
| void SyslogPrinter::flushBuffer() { | ||
| if (_bufferIndex == 0) return; | ||
|
|
||
| // Trim any trailing CR so syslog server won’t show “#015” | ||
| if (_bufferIndex > 0 && _buffer[_bufferIndex-1] == '\r') _bufferIndex--; | ||
|
|
||
| // Null-terminate | ||
| _buffer[_bufferIndex] = '\0'; | ||
|
|
||
| // Send the buffer with default severity | ||
| write((const uint8_t*)_buffer, _bufferIndex, _severity); | ||
|
|
||
| // Reset buffer index | ||
| _bufferIndex = 0; | ||
| } | ||
|
|
||
| size_t SyslogPrinter::write(uint8_t c) { | ||
| // Store in buffer regardless of connection status | ||
| if (_bufferIndex < sizeof(_buffer) - 1) { | ||
| _buffer[_bufferIndex++] = c; | ||
| } | ||
|
|
||
| // If newline or buffer full, flush | ||
| if (c == '\n' || _bufferIndex >= sizeof(_buffer) - 1) { | ||
| flushBuffer(); | ||
| } | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| size_t SyslogPrinter::write(const uint8_t *buf, size_t size) { | ||
| return write(buf, size, _severity); | ||
| } | ||
|
|
||
| size_t SyslogPrinter::write(const uint8_t *buf, size_t size, uint8_t severity) { | ||
| if (!WLED_CONNECTED || buf == nullptr || !syslogEnabled) return 0; | ||
| if (!resolveHostname()) return 0; | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| syslogUdp.beginPacket(syslogHostIP, syslogPort); | ||
|
|
||
| // Calculate priority value | ||
| uint8_t pri = (_facility << 3) | severity; | ||
|
|
||
| // Handle different syslog protocol formats | ||
| switch (_protocol) { | ||
| case SYSLOG_PROTO_BSD: | ||
| // RFC 3164 format: <PRI>TIMESTAMP HOSTNAME APP-NAME: MESSAGE | ||
| syslogUdp.printf("<%d>", pri); | ||
|
|
||
| if (ntpEnabled && ntpConnected) { | ||
| // Month abbreviation | ||
| const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | ||
| "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
KrX3D marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| syslogUdp.printf("%s %2d %02d:%02d:%02d ", | ||
| months[month(localTime) - 1], | ||
| day(localTime), | ||
| hour(localTime), | ||
| minute(localTime), | ||
| second(localTime)); | ||
| } else { | ||
| // No valid time available | ||
| syslogUdp.print("Jan 01 00:00:00 "); | ||
KrX3D marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Add hostname and app name | ||
| syslogUdp.print(serverDescription); | ||
| syslogUdp.print(" "); | ||
| syslogUdp.print(_appName); | ||
| syslogUdp.print(": "); | ||
|
|
||
| // Add message content | ||
| size = syslogUdp.write(buf, size); | ||
| break; | ||
|
|
||
| case SYSLOG_PROTO_RFC5424: | ||
| // RFC 5424 format: <PRI>VERSION TIMESTAMP HOSTNAME APP-NAME PROCID MSGID STRUCTURED-DATA MSG | ||
| syslogUdp.printf("<%d>1 ", pri); // Version is always 1 | ||
|
|
||
| if (ntpEnabled && ntpConnected) { | ||
| syslogUdp.printf("%04d-%02d-%02dT%02d:%02d:%02dZ ", | ||
| year(localTime), | ||
| month(localTime), | ||
| day(localTime), | ||
| hour(localTime), | ||
| minute(localTime), | ||
| second(localTime)); | ||
| } else { | ||
| // No valid time available | ||
| syslogUdp.print("1970-01-01T00:00:00Z "); | ||
KrX3D marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Add hostname, app name, and other fields (using - for empty fields) | ||
| syslogUdp.print(serverDescription); | ||
| syslogUdp.print(" "); | ||
| syslogUdp.print(_appName); | ||
| syslogUdp.print(" - - - "); // PROCID, MSGID, and STRUCTURED-DATA are empty | ||
|
|
||
| // Add message content | ||
| size = syslogUdp.write(buf, size); | ||
| break; | ||
|
|
||
| case SYSLOG_PROTO_RAW: | ||
| default: | ||
| // Just send the raw message (like original NetDebug) | ||
| size = syslogUdp.write(buf, size); | ||
| break; | ||
| } | ||
|
|
||
| syslogUdp.endPacket(); | ||
| return size; | ||
| } | ||
|
|
||
| SyslogPrinter Syslog; | ||
| #endif // WLED_ENABLE_SYSLOG | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will add size to all builds, not acceptable for a debug function IMHO
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I would agree that allowing the user to select the facility and level is overkill
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should solve that size problem when not using syslog:
#4664 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While that is an interesting approach to the problem, I still see no point in allowing the user to define nonsensical values. For example , WLED is not a printer, so what is the point of the user being able to select LPR as the facility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi i just added the full list i found online. So which should i remove/keep? i read somewhere that SYSLOG_LOCAL4 (LOCAL4) is normaly used for user output, thats why i also set is as default.
i would think of least those ?
and maybe change th edefault to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i removed the one i mentioned