You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

275 lines
6.1 KiB

#if defined(ESP8266)
#include <Esp.h>
#include "WiFiUdp.h"
#endif
#include "Syslog.hpp"
#if !defined(NO_GLOBAL_SYSLOG)
Syslog syslog;
#endif
static const char *syslog_nil = "-";
static inline uint16_t priority(Priority prio) {
switch (prio) {
case PANIC:
return LOG_EMERG;
case ERROR:
return LOG_ERR;
case WARN:
return LOG_WARNING;
case INFO:
return LOG_INFO;
case DEBUG:
return LOG_DEBUG;
case TRACE:
return LOG_DEBUG;
default:
return LOG_INFO;
}
}
Syslog::Syslog() {}
Syslog::~Syslog() {
}
void Syslog::operator() (Priority prio, const char *format, ...) {
if (LOG_PRI(priority(prio)) > mask) return;
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
Syslog::prio(prio);
Syslog::time();
Serial.println(buf);
send(priority(prio), buf);
}
void Syslog::operator() (Priority prio, const __FlashStringHelper *format, ...) {
if (LOG_PRI(priority(prio)) > mask) return;
va_list args;
va_start(args, format);
vsnprintf_P(buf, sizeof(buf), (PGM_P) format, args);
va_end(args);
Syslog::prio(prio);
Syslog::time();
Serial.println(buf);
send(priority(prio), buf);
}
void Syslog::operator() (Priority prio, String message) {
if (LOG_PRI(priority(prio)) > mask) return;
Syslog::prio(prio);
Syslog::time();
Serial.println(message);
send(priority(prio), message.c_str());
}
void Syslog::log(Priority prio, const char *message) {
if (LOG_PRI(priority(prio)) > mask) return;
Syslog::prio(prio);
Syslog::time();
Serial.println(message);
send(priority(prio), message);
}
void Syslog::log(Priority prio, String message) {
if (LOG_PRI(priority(prio)) > mask) return;
Syslog::prio(prio);
Syslog::time();
Serial.println(message);
send(priority(prio), message.c_str());
}
void Syslog::logf(Priority prio, const char *format, ...) {
if (LOG_PRI(priority(prio)) > mask) return;
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
Syslog::prio(prio);
Syslog::time();
Serial.println(buf);
send(priority(prio), buf);
}
void Syslog::logf(Priority prio, const __FlashStringHelper *format, ...) {
if (LOG_PRI(priority(prio)) > mask) return;
va_list args;
va_start(args, format);
vsnprintf_P(buf, sizeof(buf), (PGM_P) format, args);
va_end(args);
Syslog::prio(prio);
Syslog::time();
Serial.println(buf);
send(priority(prio), buf);
}
Syslog& Syslog::setLevel(Priority prio) {
mask = priority(prio);
return *this;
}
Syslog& Syslog::setFacility(uint16_t v) {
facility = v;
return *this;
}
Syslog& Syslog::setHostname(const char *name) {
if (name == nullptr) {
hostname = syslog_nil;
} else {
hostname = name;
}
return *this;
}
Syslog& Syslog::setHostname(String& name) {
hostname = name.c_str();
return *this;
}
Syslog& Syslog::setApp(const char *name) {
if (name == nullptr) {
app = syslog_nil;
} else {
app = name;
}
return *this;
}
Syslog& Syslog::setApp(String& name) {
app = name.c_str();
return *this;
}
Syslog& Syslog::setRemote(UDP &udp, const char *host, int portnum) {
client = &udp;
server = host;
port = portnum;
return *this;
}
void Syslog::prio(Priority prio) {
switch (prio) {
case PANIC:
Serial.print(F("[\x1b[1;31mPANIC\x1b[0m] "));
break;
case ERROR:
Serial.print(F("[\x1b[1;31mERROR\x1b[0m] "));
break;
case WARN:
Serial.print(F("[\x1b[1;31mWARNS\x1b[0m] "));
break;
case INFO:
Serial.print(F("[\x1b[1;32mINFO \x1b[0m] "));
break;
case DEBUG:
Serial.print(F("[\x1b[1;33mDEBUG\x1b[0m] "));
break;
case TRACE:
Serial.print(F("[\x1b[1;33mTRACE\x1b[0m] "));
break;
}
}
void Syslog::send(uint16_t prio, const char *message) {
if (client == nullptr || server == nullptr) {
return;
}
if (!(prio & LOG_FACMASK)) {
prio = LOG_MAKEPRI(LOG_FAC(facility), prio);
}
if (!client->beginPacket(server, port)) {
Serial.print("failed to send packet to ");
Serial.print(server);
Serial.print(' ');
Serial.println(port);
return;
}
client->print('<');
client->print(prio);
client->print(F(">1 - "));
if (hostname == nullptr) {
client->print('-');
} else {
client->print(hostname);
}
client->print(' ');
if (app == nullptr) {
client->print('-');
} else {
client->print(app);
}
client->print(F(" - - - \xEF\xBB\xBF"));
client->print(message);
client->endPacket();
}
void Syslog::send(uint16_t prio, const __FlashStringHelper *message) {
if (client == nullptr || server == nullptr) {
return;
}
if (!(prio & LOG_FACMASK)) {
prio = LOG_MAKEPRI(LOG_FAC(facility), prio);
}
if (!client->beginPacket(server, port)) {
Serial.print("failed to send packet to ");
Serial.print(server);
Serial.print(' ');
Serial.println(port);
return;
}
client->print('<');
client->print(prio);
client->print(F(">1 - "));
if (hostname == nullptr) {
client->print('-');
} else {
client->print(hostname);
}
client->print(' ');
if (app == nullptr) {
client->print('-');
} else {
client->print(app);
}
client->print(F(" - - - \xEF\xBB\xBF"));
client->print(message);
client->endPacket();
}
void Syslog::time() {
uint32_t epoch = 0;
if (timeFunc != nullptr) {
epoch = timeFunc();
}
uint32_t h = (epoch % 86400) / 3600;
uint32_t m = (epoch % 3600) / 60;
uint32_t s = (epoch % 60);
if (h < 10) {
Serial.print('0');
}
Serial.print(h);
Serial.print(':');
if (m < 10) {
Serial.print('0');
}
Serial.print(m);
Serial.print(':');
if (s < 10) {
Serial.print('0');
}
Serial.print(s);
Serial.print(' ');
}