33
44#include " syslog.h"
55
6- static const __FlashStringHelper* protoNames[] = { F (" BSD" ), F (" RFC5424" ), F (" RAW" ) };
7-
8- const char * getFacilityName (uint8_t code) {
9- switch (code) {
10- case 0 : return " KERN" ;
11- case 1 : return " USER" ;
12- case 3 : return " DAEMON" ;
13- case 5 : return " SYSLOG" ;
14- case 16 : return " LCL0" ;
15- case 17 : return " LCL1" ;
16- case 18 : return " LCL2" ;
17- case 19 : return " LCL3" ;
18- case 20 : return " LCL4" ;
19- case 21 : return " LCL5" ;
20- case 22 : return " LCL6" ;
21- case 23 : return " LCL7" ;
22- default : return " UNKNOWN" ;
23- }
24- }
6+ static const char * const protoNames[] PROGMEM = {
7+ PSTR (" BSD" ),
8+ PSTR (" RFC5424" ),
9+ PSTR (" RAW" ),
10+ PSTR (" UNKNOWN" )
11+ };
12+ static const uint8_t protoCount = sizeof (protoNames)/sizeof (*protoNames);
2513
26- static const char * severityNames[] = {
27- " EMERG" ," ALERT" ," CRIT" ," ERR" ," WARNING" ," NOTICE" ," INFO" ," DEBUG"
14+ static const char * const facilityNames[] PROGMEM = {
15+ PSTR (" KERN" ), PSTR (" USER" ), PSTR (" UNKNOWN" ), PSTR (" DAEMON" ),
16+ PSTR (" UNKNOWN" ),PSTR (" SYSLOG" ), PSTR (" UNKNOWN" ), PSTR (" UNKNOWN" ),
17+ PSTR (" UNKNOWN" ),PSTR (" UNKNOWN" ),PSTR (" UNKNOWN" ), PSTR (" UNKNOWN" ),
18+ PSTR (" UNKNOWN" ),PSTR (" UNKNOWN" ),PSTR (" UNKNOWN" ), PSTR (" UNKNOWN" ),
19+ PSTR (" LCL0" ), PSTR (" LCL1" ), PSTR (" LCL2" ), PSTR (" LCL3" ),
20+ PSTR (" LCL4" ), PSTR (" LCL5" ), PSTR (" LCL6" ), PSTR (" LCL7" ),
21+ PSTR (" UNKNOWN" ) // catch-all at index 24
2822};
23+ static const uint8_t facCount = sizeof (facilityNames)/sizeof (*facilityNames);
24+
25+ static const char * const severityNames[] PROGMEM = {
26+ PSTR (" EMERG" ), PSTR (" ALERT" ), PSTR (" CRIT" ), PSTR (" ERR" ),
27+ PSTR (" WARN" ), PSTR (" NOTE" ), PSTR (" INFO" ), PSTR (" DEBUG" ),
28+ PSTR (" UNKNOWN" )
29+ };
30+ static const uint8_t sevCount = sizeof (severityNames) / sizeof (*severityNames);
2931
3032SyslogPrinter::SyslogPrinter () :
3133 _lastOperationSucceeded(true ),
@@ -40,29 +42,33 @@ void SyslogPrinter::begin(const char* host, uint16_t port,
4042 uint8_t facility, uint8_t severity, uint8_t protocol) {
4143
4244 DEBUG_PRINTF_P (PSTR (" ===== WLED SYSLOG CONFIGURATION =====\n " ));
43- DEBUG_PRINTF_P (PSTR (" Hostname: %s\n " ), syslogHost);
44- DEBUG_PRINTF_P (PSTR (" Cached IP: %s\n " ), syslogHostIP.toString ().c_str ());
45- DEBUG_PRINTF_P (PSTR (" Port: %u\n " ), (unsigned )syslogPort);
46- DEBUG_PRINTF_P (PSTR (" Protocol: %u (%s)\n " ),
47- protocol,
48- protocol <= 2 ? (const char *)protoNames[protocol] : " UNKNOWN"
49- );
50- DEBUG_PRINTF_P (PSTR (" Facility: %u (%s)\n " ),
51- (unsigned )facility,
52- getFacilityName (facility));
53- DEBUG_PRINTF_P (PSTR (" Severity: %u (%s)\n " ),
54- (unsigned )severity,
55- (severity < sizeof (severityNames)/sizeof (severityNames[0 ]))
56- ? severityNames[severity]
57- : PSTR (" UNKNOWN" ));
45+ DEBUG_PRINTF_P (PSTR (" Hostname: %s\n " ), host);
46+ DEBUG_PRINTF_P (PSTR (" Cached IP: %s\n " ), syslogHostIP.toString ().c_str ());
47+ DEBUG_PRINTF_P (PSTR (" Port: %u\n " ), (unsigned )port);
48+
49+ // Protocol
50+ uint8_t pidx = protocol < (protoCount - 1 ) ? protocol : (protoCount - 1 );
51+ const char * pstr = (const char *)pgm_read_ptr (&protoNames[pidx]);
52+ DEBUG_PRINTF_P (PSTR (" Protocol: %u (%s)\n " ), (unsigned )protocol, pstr);
53+
54+ // — Facility
55+ uint8_t fidx = facility < (facCount - 1 ) ? facility : (facCount - 1 );
56+ const char * fstr = (const char *)pgm_read_ptr (&facilityNames[fidx]);
57+ DEBUG_PRINTF_P (PSTR (" Facility: %u (%s)\n " ), (unsigned )facility, fstr);
58+
59+ // Severity
60+ uint8_t idx = severity < sevCount-1 ? severity : sevCount-1 ;
61+ const char * sevStr = (const char *)pgm_read_ptr (&severityNames[idx]);
62+ DEBUG_PRINTF_P (PSTR (" Severity: %u (%s)\n " ), (unsigned )severity, sevStr);
63+
5864 DEBUG_PRINTF_P (PSTR (" ======================================\n " ));
59-
65+
6066 strlcpy (syslogHost, host, sizeof (syslogHost));
6167 syslogPort = port;
6268 _facility = facility;
6369 _severity = severity;
6470 _protocol = protocol;
65-
71+
6672 // clear any cached IP so resolveHostname() will run next write()
6773 syslogHostIP = IPAddress (0 ,0 ,0 ,0 );
6874}
@@ -195,15 +201,17 @@ size_t SyslogPrinter::write(const uint8_t *buf, size_t size, uint8_t severity) {
195201
196202 // Handle different syslog protocol formats
197203 switch (_protocol) {
198- case SYSLOG_PROTO_BSD:
204+ case SYSLOG_PROTO_BSD:
199205 // RFC 3164 format: <PRI>TIMESTAMP HOSTNAME APP-NAME: MESSAGE
200206 syslogUdp.printf (" <%d>" , pri);
201207
202208 if (ntpEnabled && ntpConnected) {
203209 // Month abbreviation
204- const char * months[] = {" Jan" , " Feb" , " Mar" , " Apr" , " May" , " Jun" ,
205- " Jul" , " Aug" , " Sep" , " Oct" , " Nov" , " Dec" };
206-
210+ static const char * const months[] = {
211+ " Jan" ," Feb" ," Mar" ," Apr" ," May" ," Jun" ,
212+ " Jul" ," Aug" ," Sep" ," Oct" ," Nov" ," Dec"
213+ };
214+
207215 syslogUdp.printf (" %s %2d %02d:%02d:%02d " ,
208216 months[month (localTime) - 1 ],
209217 day (localTime),
@@ -212,7 +220,7 @@ size_t SyslogPrinter::write(const uint8_t *buf, size_t size, uint8_t severity) {
212220 second (localTime));
213221 } else {
214222 // No valid time available
215- syslogUdp.print (" Jan 01 00:00:00 " );
223+ syslogUdp.print (F ( " Jan 01 00:00:00 " ) );
216224 }
217225
218226 // Add hostname and app name
@@ -239,14 +247,14 @@ size_t SyslogPrinter::write(const uint8_t *buf, size_t size, uint8_t severity) {
239247 second (localTime));
240248 } else {
241249 // No valid time available
242- syslogUdp.print (" 1970-01-01T00:00:00Z " );
250+ syslogUdp.print (F ( " 1970-01-01T00:00:00Z " ) );
243251 }
244252
245253 // Add hostname, app name, and other fields (using - for empty fields)
246254 syslogUdp.print (cleanHostname);
247255 syslogUdp.print (" " );
248256 syslogUdp.print (_appName);
249- syslogUdp.print (" - - - " ); // PROCID, MSGID, and STRUCTURED-DATA are empty
257+ syslogUdp.print (F ( " - - - " ) ); // PROCID, MSGID, and STRUCTURED-DATA are empty
250258
251259 // Add message content
252260 size = syslogUdp.write (buf, size);
0 commit comments