Syslog client for mbed-os 5
Fork of logger by
Logger.cpp@6:cf9acc20f954, 2016-10-20 (annotated)
- Committer:
- Colin Hogben
- Date:
- Thu Oct 20 13:57:59 2016 +0100
- Revision:
- 6:cf9acc20f954
- Parent:
- 5:641de66579d2
Add configuration parameters
Who changed what in which revision?
User | Revision | Line number | New 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 |
4:b51e0614c767 | 4 | * Copyright (c) 2016 Colin Hogben |
Colin Hogben |
3:91ee7ead8536 | 5 | * Released under the MIT License: http://mbed.org/license/mit |
Colin Hogben |
3:91ee7ead8536 | 6 | */ |
Colin Hogben |
3:91ee7ead8536 | 7 | |
Colin Hogben |
6:cf9acc20f954 | 8 | /** @file Logger.cpp |
Colin Hogben |
3:91ee7ead8536 | 9 | * @brief syslog device (sender/client) |
Colin Hogben |
3:91ee7ead8536 | 10 | */ |
Colin Hogben |
3:91ee7ead8536 | 11 | |
Colin Hogben |
5:641de66579d2 | 12 | #include "Logger.h" |
Colin Hogben |
3:91ee7ead8536 | 13 | #include <cstdio> |
Colin Hogben |
3:91ee7ead8536 | 14 | |
Colin Hogben |
6:cf9acc20f954 | 15 | #ifdef MBED_CONF_LOGGER_LINE_MAX |
Colin Hogben |
6:cf9acc20f954 | 16 | #define LOG_LEN MBED_CONF_LOGGER_LINE_MAX |
Colin Hogben |
6:cf9acc20f954 | 17 | #else |
Colin Hogben |
4:b51e0614c767 | 18 | #define LOG_LEN 256 |
Colin Hogben |
6:cf9acc20f954 | 19 | #endif |
Colin Hogben |
6:cf9acc20f954 | 20 | |
Colin Hogben |
4:b51e0614c767 | 21 | #define LOG_UDPPORT 514 |
Colin Hogben |
4:b51e0614c767 | 22 | |
Colin Hogben |
4:b51e0614c767 | 23 | static const char mstr[12][4] = { |
Colin Hogben |
4:b51e0614c767 | 24 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
Colin Hogben |
4:b51e0614c767 | 25 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
Colin Hogben |
4:b51e0614c767 | 26 | }; |
Colin Hogben |
3:91ee7ead8536 | 27 | |
Colin Hogben |
3:91ee7ead8536 | 28 | Logger::Logger(NetworkInterface *netif, const char *host) { |
Colin Hogben |
4:b51e0614c767 | 29 | _Logger(netif, host, netif->get_ip_address()); |
Colin Hogben |
3:91ee7ead8536 | 30 | } |
Colin Hogben |
3:91ee7ead8536 | 31 | |
Colin Hogben |
3:91ee7ead8536 | 32 | Logger::Logger(NetworkInterface *netif, const char *host, const char *myname) { |
Colin Hogben |
4:b51e0614c767 | 33 | _Logger(netif, host, myname); |
Colin Hogben |
4:b51e0614c767 | 34 | } |
Colin Hogben |
4:b51e0614c767 | 35 | |
Colin Hogben |
4:b51e0614c767 | 36 | void Logger::_Logger(NetworkInterface *netif, const char *host, const char *myname) { |
Colin Hogben |
3:91ee7ead8536 | 37 | _remote = SocketAddress(host, LOG_UDPPORT); |
Colin Hogben |
3:91ee7ead8536 | 38 | snprintf(_ident, sizeof(_ident), "%s", myname); |
Colin Hogben |
3:91ee7ead8536 | 39 | int err = _udpsock.open(netif); |
Colin Hogben |
4:b51e0614c767 | 40 | if (err) error("Cannot open syslog UDP socket"); |
Colin Hogben |
3:91ee7ead8536 | 41 | _udpsock.bind(LOG_UDPPORT); |
Colin Hogben |
3:91ee7ead8536 | 42 | } |
Colin Hogben |
3:91ee7ead8536 | 43 | |
Colin Hogben |
3:91ee7ead8536 | 44 | void Logger::send(const char *tag, const char *content) { |
Colin Hogben |
3:91ee7ead8536 | 45 | send(LOG_NOTICE, LOG_USER, tag, content); |
Colin Hogben |
3:91ee7ead8536 | 46 | } |
Colin Hogben |
3:91ee7ead8536 | 47 | |
Colin Hogben |
3:91ee7ead8536 | 48 | void Logger::send(LOG_SEVERITY sev, LOG_FACILITY fac, const char *tag, const char *content) { |
Colin Hogben |
3:91ee7ead8536 | 49 | char logmsg[LOG_LEN]; |
Colin Hogben |
3:91ee7ead8536 | 50 | |
Colin Hogben |
4:b51e0614c767 | 51 | time_t ctTime = time(NULL); |
Colin Hogben |
4:b51e0614c767 | 52 | struct tm *t = localtime(&ctTime); |
Colin Hogben |
4:b51e0614c767 | 53 | int pri = (fac * 8) | sev; |
Colin Hogben |
3:91ee7ead8536 | 54 | |
Colin Hogben |
4:b51e0614c767 | 55 | snprintf(logmsg, sizeof(logmsg), |
Colin Hogben |
4:b51e0614c767 | 56 | "<%d>%s %2d %02d:%02d:%02d %s %s %s", |
Colin Hogben |
4:b51e0614c767 | 57 | pri, |
Colin Hogben |
4:b51e0614c767 | 58 | mstr[t->tm_mon], t->tm_mday, |
Colin Hogben |
4:b51e0614c767 | 59 | t->tm_hour, t->tm_min, t->tm_sec, |
Colin Hogben |
4:b51e0614c767 | 60 | _ident, |
Colin Hogben |
4:b51e0614c767 | 61 | tag, |
Colin Hogben |
4:b51e0614c767 | 62 | content); |
Colin Hogben |
3:91ee7ead8536 | 63 | _udpsock.sendto(_remote, logmsg, strlen(logmsg)); |
Colin Hogben |
3:91ee7ead8536 | 64 | } |