Browse Source

Time support

master
maze 4 years ago
parent
commit
780e7e544d
2 changed files with 41 additions and 1 deletions
  1. +34
    -1
      Syslog.cpp
  2. +7
    -0
      Syslog.h

+ 34
- 1
Syslog.cpp View File

@ -1,6 +1,6 @@
#if defined(ESP8266)
#include <Esp.h>
#include <WiFiUdp.h>
#include "WiFiUdp.h"
#endif
#include "Syslog.hpp"
@ -42,6 +42,7 @@ void Syslog::operator() (Priority prio, const char *format, ...) {
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
Syslog::prio(prio);
Syslog::time();
Serial.println(buf);
send(priority(prio), buf);
}
@ -53,6 +54,7 @@ void Syslog::operator() (Priority prio, const __FlashStringHelper *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);
}
@ -60,6 +62,7 @@ void Syslog::operator() (Priority prio, const __FlashStringHelper *format, ...)
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());
}
@ -67,6 +70,7 @@ void Syslog::operator() (Priority prio, String message) {
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);
}
@ -74,6 +78,7 @@ void Syslog::log(Priority prio, const char *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());
}
@ -85,6 +90,7 @@ void Syslog::logf(Priority prio, const char *format, ...) {
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
Syslog::prio(prio);
Syslog::time();
Serial.println(buf);
send(priority(prio), buf);
}
@ -96,6 +102,7 @@ void Syslog::logf(Priority prio, const __FlashStringHelper *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);
}
@ -240,3 +247,29 @@ void Syslog::send(uint16_t prio, const __FlashStringHelper *message) {
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(' ');
}

+ 7
- 0
Syslog.h View File

@ -1,5 +1,7 @@
#pragma once
#include <functional>
#include <stdarg.h>
#include <HardwareSerial.h>
#include <WString.h>
@ -76,6 +78,7 @@ public:
void log(Priority, String);
void logf(Priority, const char *, ...);
void logf(Priority, const __FlashStringHelper *, ...);
void time();
Syslog& setLevel(Priority);
Syslog& setFacility(uint16_t);
Syslog& setHostname(const char *);
@ -83,6 +86,9 @@ public:
Syslog& setApp(const char *);
Syslog& setApp(String &);
Syslog& setRemote(UDP &, const char *, int = 514);
Syslog& setTime(std::function<uint32_t(void)> func) {
timeFunc = func;
}
public:
const char *server = nullptr;
@ -91,6 +97,7 @@ public:
const char *app = nullptr;
uint16_t mask = LOG_INFO;
uint16_t facility = LOG_KERN;
std::function<uint32_t(void)> timeFunc = nullptr;
private:
char buf[SYSLOG_BUF_LEN];


Loading…
Cancel
Save