Syslog client for mbed-os 5
Fork of logger by
logger.cpp
- Committer:
- Colin Hogben
- Date:
- 2016-10-14
- Revision:
- 3:91ee7ead8536
- Parent:
- 2:ce978c9ea3e8
- Child:
- 4:b51e0614c767
File content as of revision 3:91ee7ead8536:
/* * syslog device library * Copyright (c) 2011 Hiroshi Suga * Released under the MIT License: http://mbed.org/license/mit */ /** @file logger.cpp * @brief syslog device (sender/client) */ #include "logger.h" #include <cstdio> static const char mstr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; Logger::Logger(NetworkInterface *netif, const char *host) { Logger(netif, host, netif->get_ip_address()); } Logger::Logger(NetworkInterface *netif, const char *host, const char *myname) { _remote = SocketAddress(host, LOG_UDPPORT); snprintf(_ident, sizeof(_ident), "%s", myname); int err = _udpsock.open(netif); _udpsock.bind(LOG_UDPPORT); } void Logger::send(const char *tag, const char *content) { send(LOG_NOTICE, LOG_USER, tag, content); } void Logger::send(LOG_SEVERITY sev, LOG_FACILITY fac, const char *tag, const char *content) { int pri, len; time_t ctTime; struct tm *t; char logmsg[LOG_LEN]; ctTime = time(NULL); t = localtime(&ctTime); pri = (fac * 8) | sev; sprintf(logmsg, "<%d>%s %2d %02d:%02d:%02d %s ", pri, mstr[t->tm_mon - 1], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, _ident); len = strlen(tag); if (len > 32) len = 32; strncat(logmsg, tag, len); strcat(logmsg, " "); len = strlen(content); if (len > LOG_LEN - strlen(logmsg) - 1) len = LOG_LEN - strlen(logmsg) - 1; strncat(logmsg, content, len); _udpsock.sendto(_remote, logmsg, strlen(logmsg)); }