Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
arcao committed Nov 3, 2016
1 parent 3b75b05 commit b1f9388
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: python

cache:
directories:
- "~/.platformio"

install:
- pip install -U platformio

env:
- BOARD=esp01
- BOARD=nodemcuv2

script:
- for e in examples/*; do
platformio ci --board=$BOARD --lib=. $e/*;
done
52 changes: 52 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#######################################
# Syntax Coloring Map syslog lib
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################
Syslog KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
log KEYWORD2 log method

#######################################
# Instances (KEYWORD2)
#######################################


#######################################
# Constants (LITERAL1)
#######################################
LOG_EMERG LITERAL1 system is unusable
LOG_ALERT LITERAL1 action must be taken immediately
LOG_CRIT LITERAL1 critical conditions
LOG_ERR LITERAL1 error conditions
LOG_WARNING LITERAL1 warning conditions
LOG_NOTICE LITERAL1 normal but significant condition
LOG_INFO LITERAL1 informational
LOG_DEBUG LITERAL1 debug-level messages

LOG_KERN LITERAL1 kernel messages
LOG_USER LITERAL1 random user-level messages
LOG_MAIL LITERAL1 mail system
LOG_DAEMON LITERAL1 system daemons
LOG_AUTH LITERAL1 security/authorization messages
LOG_SYSLOG LITERAL1 messages generated internally by syslogd
LOG_LPR LITERAL1 line printer subsystem
LOG_NEWS LITERAL1 network news subsystem
LOG_UUCP LITERAL1 UUCP subsystem
LOG_CRON LITERAL1 clock daemon
LOG_AUTHPRIV LITERAL1 security/authorization messages (private)
LOG_FTP LITERAL1 ftp daemon

LOG_LOCAL0 LITERAL1 reserved for local use
LOG_LOCAL1 LITERAL1 reserved for local use
LOG_LOCAL2 LITERAL1 reserved for local use
LOG_LOCAL3 LITERAL1 reserved for local use
LOG_LOCAL4 LITERAL1 reserved for local use
LOG_LOCAL5 LITERAL1 reserved for local use
LOG_LOCAL6 LITERAL1 reserved for local use
LOG_LOCAL7 LITERAL1 reserved for local use
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=ESP8266 Syslog
version=1.0
author=Martin Sloup <[email protected]>
maintainer=Martin Sloup <[email protected]>
sentence=A library for logging to Syslog server in IETF format (rfc5424)
paragraph=
category=Data Storage
url=https://github.com/arcao/ESP8266_Syslog
architectures=esp8266
includes=syslog.h
51 changes: 51 additions & 0 deletions src/syslog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Arduino.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "syslog.h"

Syslog::Syslog(const char* server, int port, const char* localHostname, const char* appName, int defaultLevel) {
this->_server = server;
this->_port = port;
this->_localHostname = localHostname;
this->_appName = (appName == NULL) ? "-" : appName;
this->_defaultLevel = defaultLevel;
}

void Syslog::log(int level, const char *fmt, ...) {
char buffer[SYSLOG_PACKET_SIZE];
char message[SYSLOG_MESSAGE_SIZE];
va_list args;

time_t rawtime;
struct tm* timeInfo;

time(&rawtime);
timeInfo = localtime(&rawtime);

va_start(args, fmt);
vsnprintf(message, SYSLOG_MESSAGE_SIZE, fmt, args);
va_end(args);

if (level <= LOG_DEBUG) {
level = level | this->_defaultLevel;
}

// Doc: https://tools.ietf.org/html/rfc5424
int len = sprintf(buffer, "<%d>1 %04d-%02d-%02dT%02d:%02d:%02d %s %s - - - %s",
level,
timeInfo->tm_year + 1900,
timeInfo->tm_mon + 1,
timeInfo->tm_mday,
timeInfo->tm_hour,
timeInfo->tm_min,
timeInfo->tm_sec,
this->_localHostname,
this->_appName,
message);

this->_udp.beginPacket(this->_server, this->_port);
this->_udp.write(buffer, len);
this->_udp.endPacket();
}

79 changes: 79 additions & 0 deletions src/syslog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once
#ifndef SYSLOG_H
#define SYSLOG_H

#include "Arduino.h"
#include <WiFiUdp.h>

#define SYSLOG_PACKET_SIZE 480
#define SYSLOG_MESSAGE_SIZE 256

/*
* priorities/facilities are encoded into a single 32-bit quantity, where the
* bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
* (0-big number). Both the priorities and the facilities map roughly
* one-to-one to strings in the syslogd(8) source code. This mapping is
* included in this file.
*
* priorities (these are ordered)
*/
#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions */
#define LOG_ERR 3 /* error conditions */
#define LOG_WARNING 4 /* warning conditions */
#define LOG_NOTICE 5 /* normal but significant condition */
#define LOG_INFO 6 /* informational */
#define LOG_DEBUG 7 /* debug-level messages */

#define LOG_PRIMASK 0x07 /* mask to extract priority part (internal) */
/* extract priority */
#define LOG_PRI(p) ((p) & LOG_PRIMASK)
#define LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri))

/* facility codes */
#define LOG_KERN (0<<3) /* kernel messages */
#define LOG_USER (1<<3) /* random user-level messages */
#define LOG_MAIL (2<<3) /* mail system */
#define LOG_DAEMON (3<<3) /* system daemons */
#define LOG_AUTH (4<<3) /* security/authorization messages */
#define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */
#define LOG_LPR (6<<3) /* line printer subsystem */
#define LOG_NEWS (7<<3) /* network news subsystem */
#define LOG_UUCP (8<<3) /* UUCP subsystem */
#define LOG_CRON (9<<3) /* clock daemon */
#define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */
#define LOG_FTP (11<<3) /* ftp daemon */

/* other codes through 15 reserved for system use */
#define LOG_LOCAL0 (16<<3) /* reserved for local use */
#define LOG_LOCAL1 (17<<3) /* reserved for local use */
#define LOG_LOCAL2 (18<<3) /* reserved for local use */
#define LOG_LOCAL3 (19<<3) /* reserved for local use */
#define LOG_LOCAL4 (20<<3) /* reserved for local use */
#define LOG_LOCAL5 (21<<3) /* reserved for local use */
#define LOG_LOCAL6 (22<<3) /* reserved for local use */
#define LOG_LOCAL7 (23<<3) /* reserved for local use */

#define LOG_NFACILITIES 24 /* current number of facilities */
#define LOG_FACMASK 0x03f8 /* mask to extract facility part */
/* facility of pri */
#define LOG_FAC(p) (((p) & LOG_FACMASK) >> 3)

class Syslog {
private:
const char* _server;
int _port;
const char* _localHostname;
const char* _appName;
int _defaultLevel;

WiFiUDP _udp;

public:
Syslog(const char* server, int port, const char* localHostname, const char* appName, int defaultLevel = LOG_KERN);
void log(int level, const char *fmt, ...);
};

#endif

0 comments on commit b1f9388

Please sign in to comment.