├── README.MD ├── Syslog.cpp ├── Syslog.h ├── examples └── syslog │ └── syslog.ino └── keywords.txt /README.MD: -------------------------------------------------------------------------------- 1 | ## Library for printing messages to Syslog on ESP8266 2 | 3 | This library allows ESP8266-based devices to send messages to 4 | remote syslog servers. This is especially useful when you've put your 5 | code into production, and you no longer have access to the serial console 6 | since it isn't connected to your laptop anymore, but hidden away inside a 7 | wall somewhere. 8 | You'll need to set up a machine on your network as a syslog server. The easiest 9 | way to do this is to use a Raspberry Pi with Raspbian. Raspbian comes with 10 | rsyslogd, which supports the remote syslog protocol, but this capability is 11 | disabled by default. You'll need to uncomment the following lines from 12 | `/etc/rsyslog.conf`: 13 | ``` 14 | $ModLoad imudp 15 | $UDPServerRun 514 16 | ``` 17 | Then restart rsyslog: 18 | ``` 19 | systemctl restart rsyslog 20 | ``` 21 | Note that you can use any Linux distribution with rsyslog, not just Rasbian on a 22 | Raspberry Pi. 23 | 24 | ### Installation 25 | Use `git` to clone this repository into your Arduino libraries folder. On a Mac, 26 | this is usually `Documents/Arduino/libraries`: 27 | ``` 28 | cd ~/Documents/Arduino/libraries 29 | git clone https://www.github.com/jerryr/EspSyslog 30 | ``` 31 | -------------------------------------------------------------------------------- /Syslog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void Syslog::dolog(uint8_t pri, char *message) { 6 | uint8_t buffer[MAX_PACKET_SIZE]; 7 | uint16_t len = snprintf((char *)buffer, MAX_PACKET_SIZE, "<%d> %s %s: %s", pri, _host, 8 | _app, message); 9 | len = (len > MAX_PACKET_SIZE)?MAX_PACKET_SIZE:len; // handle truncation if message too long 10 | udp.beginPacket(_server, _port); 11 | udp.write(buffer, len); 12 | udp.endPacket(); 13 | } 14 | -------------------------------------------------------------------------------- /Syslog.h: -------------------------------------------------------------------------------- 1 | #ifndef SYSLOG_H 2 | #define SYSLOG_H 3 | #define MAX_PACKET_SIZE 500 4 | // in syslog is defined as (facility << 3) + severity 5 | // We always use facility as 1 (which means "user"), 1<<3 = 8 6 | #define PRI_DEBUG 15 // 8 + 7 7 | #define PRI_INFO 14 // 8 + 6 8 | #define PRI_WARNING 12 // 8 + 4 9 | #define PRI_ERROR 11 // 8 + 3 10 | class Syslog { 11 | public: 12 | Syslog(const char *host, const char *app, const char *server, uint16_t port=514): 13 | _host(host), 14 | _app(app), 15 | _server(server), 16 | _port(port) 17 | {} 18 | void debug(char *message) { dolog(PRI_DEBUG, message); } 19 | void info(char *message) { dolog(PRI_INFO, message); } 20 | void warn(char *message) {dolog(PRI_WARNING, message); } 21 | void error(char *message) {dolog(PRI_ERROR, message); } 22 | private: 23 | const char *_host; 24 | const char *_app; 25 | const char *_server; 26 | const int _port; 27 | void dolog(uint8_t pri, char *message); 28 | WiFiUDP udp; 29 | }; 30 | #endif 31 | -------------------------------------------------------------------------------- /examples/syslog/syslog.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define SSID "My_ssid_here" 5 | #define PASSWD "My_passwd_here" 6 | WiFiClient client; 7 | 8 | WiFiUDP udp; 9 | #include 10 | 11 | Syslog logger("gadget", "testapp", "192.168.1.200"); 12 | void setup() { 13 | Serial.begin(115200); 14 | WiFi.begin(SSID, PASSWD); 15 | while(WiFi.status() != WL_CONNECTED) { 16 | delay(500); 17 | } 18 | logger.info("Connected to wifi"); 19 | } 20 | 21 | void loop() { 22 | logger.debug("running loop"); 23 | delay(2000); 24 | } 25 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | Syslog KEYWORD1 2 | debug KEYWORD2 3 | info KEYWORD2 4 | warn KEYWORD2 5 | error KEYWORD2 6 | --------------------------------------------------------------------------------