Syslog client for mbed-os 5

Fork of logger by Suga koubou

Committer:
Colin Hogben
Date:
Fri Oct 14 15:53:04 2016 +0100
Revision:
3:91ee7ead8536
Parent:
2:ce978c9ea3e8
Child:
4:b51e0614c767
unDOSify line endings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Colin Hogben 3:91ee7ead8536 1 /*
Colin Hogben 3:91ee7ead8536 2 * syslog device library
Colin Hogben 3:91ee7ead8536 3 * Copyright (c) 2011 Hiroshi Suga
Colin Hogben 3:91ee7ead8536 4 * Released under the MIT License: http://mbed.org/license/mit
Colin Hogben 3:91ee7ead8536 5 */
Colin Hogben 3:91ee7ead8536 6
Colin Hogben 3:91ee7ead8536 7 /** @file logger.cpp
Colin Hogben 3:91ee7ead8536 8 * @brief syslog device (sender/client)
Colin Hogben 3:91ee7ead8536 9 */
Colin Hogben 3:91ee7ead8536 10
Colin Hogben 3:91ee7ead8536 11 #include "logger.h"
Colin Hogben 3:91ee7ead8536 12 #include <cstdio>
Colin Hogben 3:91ee7ead8536 13
Colin Hogben 3:91ee7ead8536 14 static const char mstr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Colin Hogben 3:91ee7ead8536 15
Colin Hogben 3:91ee7ead8536 16 Logger::Logger(NetworkInterface *netif, const char *host) {
Colin Hogben 3:91ee7ead8536 17 Logger(netif, host, netif->get_ip_address());
Colin Hogben 3:91ee7ead8536 18 }
Colin Hogben 3:91ee7ead8536 19
Colin Hogben 3:91ee7ead8536 20 Logger::Logger(NetworkInterface *netif, const char *host, const char *myname) {
Colin Hogben 3:91ee7ead8536 21 _remote = SocketAddress(host, LOG_UDPPORT);
Colin Hogben 3:91ee7ead8536 22 snprintf(_ident, sizeof(_ident), "%s", myname);
Colin Hogben 3:91ee7ead8536 23 int err = _udpsock.open(netif);
Colin Hogben 3:91ee7ead8536 24 _udpsock.bind(LOG_UDPPORT);
Colin Hogben 3:91ee7ead8536 25 }
Colin Hogben 3:91ee7ead8536 26
Colin Hogben 3:91ee7ead8536 27 void Logger::send(const char *tag, const char *content) {
Colin Hogben 3:91ee7ead8536 28 send(LOG_NOTICE, LOG_USER, tag, content);
Colin Hogben 3:91ee7ead8536 29 }
Colin Hogben 3:91ee7ead8536 30
Colin Hogben 3:91ee7ead8536 31 void Logger::send(LOG_SEVERITY sev, LOG_FACILITY fac, const char *tag, const char *content) {
Colin Hogben 3:91ee7ead8536 32 int pri, len;
Colin Hogben 3:91ee7ead8536 33 time_t ctTime;
Colin Hogben 3:91ee7ead8536 34 struct tm *t;
Colin Hogben 3:91ee7ead8536 35 char logmsg[LOG_LEN];
Colin Hogben 3:91ee7ead8536 36
Colin Hogben 3:91ee7ead8536 37 ctTime = time(NULL);
Colin Hogben 3:91ee7ead8536 38 t = localtime(&ctTime);
Colin Hogben 3:91ee7ead8536 39 pri = (fac * 8) | sev;
Colin Hogben 3:91ee7ead8536 40
Colin Hogben 3:91ee7ead8536 41 sprintf(logmsg, "<%d>%s %2d %02d:%02d:%02d %s ", pri,
Colin Hogben 3:91ee7ead8536 42 mstr[t->tm_mon - 1], t->tm_mday,
Colin Hogben 3:91ee7ead8536 43 t->tm_hour, t->tm_min, t->tm_sec, _ident);
Colin Hogben 3:91ee7ead8536 44
Colin Hogben 3:91ee7ead8536 45 len = strlen(tag);
Colin Hogben 3:91ee7ead8536 46 if (len > 32) len = 32;
Colin Hogben 3:91ee7ead8536 47 strncat(logmsg, tag, len);
Colin Hogben 3:91ee7ead8536 48
Colin Hogben 3:91ee7ead8536 49 strcat(logmsg, " ");
Colin Hogben 3:91ee7ead8536 50
Colin Hogben 3:91ee7ead8536 51 len = strlen(content);
Colin Hogben 3:91ee7ead8536 52 if (len > LOG_LEN - strlen(logmsg) - 1) len = LOG_LEN - strlen(logmsg) - 1;
Colin Hogben 3:91ee7ead8536 53 strncat(logmsg, content, len);
Colin Hogben 3:91ee7ead8536 54
Colin Hogben 3:91ee7ead8536 55 _udpsock.sendto(_remote, logmsg, strlen(logmsg));
Colin Hogben 3:91ee7ead8536 56 }