Syslog client for mbed-os 5

Fork of logger by Suga koubou

Revision:
2:ce978c9ea3e8
Parent:
1:f7e32e99f366
Child:
3:91ee7ead8536
diff -r f7e32e99f366 -r ce978c9ea3e8 logger.cpp
--- a/logger.cpp	Sat Apr 16 15:34:45 2011 +0000
+++ b/logger.cpp	Fri Oct 14 12:06:05 2016 +0000
@@ -9,44 +9,26 @@
  */
 
 #include "logger.h"
-
-static const char mstr[12][4] = {"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+#include <cstdio>
 
-logger::logger (EthernetNetIf *p_eth, char *host) {
-    IpAddr addr;
-    char name[20];
+static const char mstr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 
-    addr = p_eth->getIp();
-    sprintf(name, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
-
-    logger(p_eth, host, name);
+Logger::Logger(NetworkInterface *netif, const char *host) {
+    Logger(netif, host, netif->get_ip_address());
 }
 
-logger::logger (EthernetNetIf *p_eth, char *host, char *myname) {
-    int ip0, ip1, ip2, ip3;
-
-    eth = p_eth;
-    strncpy(ident, myname, sizeof(ident));
-
-    if (host[0] >= '0' && host[0] <= '9') {
-        sscanf(host, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
-        remote.setIp(IpAddr(ip0, ip1, ip2, ip3));
-        remote.setName(NULL);
-    } else {
-        remote.setIp(NULL);
-        remote.setName(host);
-    }
-    remote.setPort(LOG_UDPPORT);
-
-    udpsock = new UDPSocket;
-    udpsock->bind(Host(eth->getIp(), LOG_UDPPORT));
+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 (char *tag, char *content) {
+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, char *tag, char *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;
@@ -56,7 +38,9 @@
     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);
+    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;
@@ -68,5 +52,5 @@
     if (len > LOG_LEN - strlen(logmsg) - 1) len = LOG_LEN - strlen(logmsg) - 1;
     strncat(logmsg, content, len);
 
-    udpsock->sendto(logmsg, strlen(logmsg), &remote);
+    _udpsock.sendto(_remote, logmsg, strlen(logmsg));
 }