Syslog client for mbed-os 5
Fork of logger by
logger.cpp@1:f7e32e99f366, 2011-04-16 (annotated)
- Committer:
- okini3939
- Date:
- Sat Apr 16 15:34:45 2011 +0000
- Revision:
- 1:f7e32e99f366
- Parent:
- 0:7d428b9b277e
- Child:
- 2:ce978c9ea3e8
Who changed what in which revision?
User | Revision | Line number | New 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" |
okini3939 | 0:7d428b9b277e | 12 | |
okini3939 | 0:7d428b9b277e | 13 | static const char mstr[12][4] = {"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
okini3939 | 0:7d428b9b277e | 14 | |
okini3939 | 0:7d428b9b277e | 15 | logger::logger (EthernetNetIf *p_eth, char *host) { |
okini3939 | 0:7d428b9b277e | 16 | IpAddr addr; |
okini3939 | 0:7d428b9b277e | 17 | char name[20]; |
okini3939 | 0:7d428b9b277e | 18 | |
okini3939 | 0:7d428b9b277e | 19 | addr = p_eth->getIp(); |
okini3939 | 0:7d428b9b277e | 20 | sprintf(name, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); |
okini3939 | 0:7d428b9b277e | 21 | |
okini3939 | 0:7d428b9b277e | 22 | logger(p_eth, host, name); |
okini3939 | 0:7d428b9b277e | 23 | } |
okini3939 | 0:7d428b9b277e | 24 | |
okini3939 | 0:7d428b9b277e | 25 | logger::logger (EthernetNetIf *p_eth, char *host, char *myname) { |
okini3939 | 0:7d428b9b277e | 26 | int ip0, ip1, ip2, ip3; |
okini3939 | 0:7d428b9b277e | 27 | |
okini3939 | 0:7d428b9b277e | 28 | eth = p_eth; |
okini3939 | 0:7d428b9b277e | 29 | strncpy(ident, myname, sizeof(ident)); |
okini3939 | 0:7d428b9b277e | 30 | |
okini3939 | 0:7d428b9b277e | 31 | if (host[0] >= '0' && host[0] <= '9') { |
okini3939 | 0:7d428b9b277e | 32 | sscanf(host, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); |
okini3939 | 0:7d428b9b277e | 33 | remote.setIp(IpAddr(ip0, ip1, ip2, ip3)); |
okini3939 | 0:7d428b9b277e | 34 | remote.setName(NULL); |
okini3939 | 0:7d428b9b277e | 35 | } else { |
okini3939 | 0:7d428b9b277e | 36 | remote.setIp(NULL); |
okini3939 | 0:7d428b9b277e | 37 | remote.setName(host); |
okini3939 | 0:7d428b9b277e | 38 | } |
okini3939 | 0:7d428b9b277e | 39 | remote.setPort(LOG_UDPPORT); |
okini3939 | 0:7d428b9b277e | 40 | |
okini3939 | 0:7d428b9b277e | 41 | udpsock = new UDPSocket; |
okini3939 | 0:7d428b9b277e | 42 | udpsock->bind(Host(eth->getIp(), LOG_UDPPORT)); |
okini3939 | 0:7d428b9b277e | 43 | } |
okini3939 | 0:7d428b9b277e | 44 | |
okini3939 | 0:7d428b9b277e | 45 | void logger::send (char *tag, char *content) { |
okini3939 | 0:7d428b9b277e | 46 | send(LOG_NOTICE, LOG_USER, tag, content); |
okini3939 | 0:7d428b9b277e | 47 | } |
okini3939 | 0:7d428b9b277e | 48 | |
okini3939 | 0:7d428b9b277e | 49 | void logger::send (LOG_SEVERITY sev, LOG_FACILITY fac, char *tag, char *content) { |
okini3939 | 0:7d428b9b277e | 50 | int pri, len; |
okini3939 | 0:7d428b9b277e | 51 | time_t ctTime; |
okini3939 | 0:7d428b9b277e | 52 | struct tm *t; |
okini3939 | 0:7d428b9b277e | 53 | char logmsg[LOG_LEN]; |
okini3939 | 0:7d428b9b277e | 54 | |
okini3939 | 0:7d428b9b277e | 55 | ctTime = time(NULL); |
okini3939 | 0:7d428b9b277e | 56 | t = localtime(&ctTime); |
okini3939 | 0:7d428b9b277e | 57 | pri = (fac * 8) | sev; |
okini3939 | 0:7d428b9b277e | 58 | |
okini3939 | 0:7d428b9b277e | 59 | 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); |
okini3939 | 0:7d428b9b277e | 60 | |
okini3939 | 0:7d428b9b277e | 61 | len = strlen(tag); |
okini3939 | 0:7d428b9b277e | 62 | if (len > 32) len = 32; |
okini3939 | 0:7d428b9b277e | 63 | strncat(logmsg, tag, len); |
okini3939 | 0:7d428b9b277e | 64 | |
okini3939 | 0:7d428b9b277e | 65 | strcat(logmsg, " "); |
okini3939 | 0:7d428b9b277e | 66 | |
okini3939 | 0:7d428b9b277e | 67 | len = strlen(content); |
okini3939 | 0:7d428b9b277e | 68 | if (len > LOG_LEN - strlen(logmsg) - 1) len = LOG_LEN - strlen(logmsg) - 1; |
okini3939 | 0:7d428b9b277e | 69 | strncat(logmsg, content, len); |
okini3939 | 0:7d428b9b277e | 70 | |
okini3939 | 0:7d428b9b277e | 71 | udpsock->sendto(logmsg, strlen(logmsg), &remote); |
okini3939 | 0:7d428b9b277e | 72 | } |