Syslog client for mbed-os 5

Fork of logger by Suga koubou

Committer:
infinnovation
Date:
Fri Oct 14 12:06:05 2016 +0000
Revision:
2:ce978c9ea3e8
Parent:
1:f7e32e99f366
Child:
3:91ee7ead8536
Rework for mbed-os 5

Who changed what in which revision?

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