Syslog client for mbed-os 5
Fork of logger by
logger.cpp@0:7d428b9b277e, 2011-04-16 (annotated)
- Committer:
- okini3939
- Date:
- Sat Apr 16 08:48:55 2011 +0000
- Revision:
- 0:7d428b9b277e
- Child:
- 1:f7e32e99f366
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 | /** |
okini3939 | 0:7d428b9b277e | 16 | * @brief init |
okini3939 | 0:7d428b9b277e | 17 | * @param p_eth EthernetNetIf class |
okini3939 | 0:7d428b9b277e | 18 | * @param host syslog collctor (server) |
okini3939 | 0:7d428b9b277e | 19 | */ |
okini3939 | 0:7d428b9b277e | 20 | logger::logger (EthernetNetIf *p_eth, char *host) { |
okini3939 | 0:7d428b9b277e | 21 | IpAddr addr; |
okini3939 | 0:7d428b9b277e | 22 | char name[20]; |
okini3939 | 0:7d428b9b277e | 23 | |
okini3939 | 0:7d428b9b277e | 24 | addr = p_eth->getIp(); |
okini3939 | 0:7d428b9b277e | 25 | sprintf(name, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); |
okini3939 | 0:7d428b9b277e | 26 | |
okini3939 | 0:7d428b9b277e | 27 | logger(p_eth, host, name); |
okini3939 | 0:7d428b9b277e | 28 | } |
okini3939 | 0:7d428b9b277e | 29 | |
okini3939 | 0:7d428b9b277e | 30 | /** |
okini3939 | 0:7d428b9b277e | 31 | * @brief init |
okini3939 | 0:7d428b9b277e | 32 | * @param p_eth EthernetNetIf class |
okini3939 | 0:7d428b9b277e | 33 | * @param host syslog collctor (server) hostname or IP address |
okini3939 | 0:7d428b9b277e | 34 | * @param myname My hostname or IP address |
okini3939 | 0:7d428b9b277e | 35 | */ |
okini3939 | 0:7d428b9b277e | 36 | logger::logger (EthernetNetIf *p_eth, char *host, char *myname) { |
okini3939 | 0:7d428b9b277e | 37 | int ip0, ip1, ip2, ip3; |
okini3939 | 0:7d428b9b277e | 38 | |
okini3939 | 0:7d428b9b277e | 39 | eth = p_eth; |
okini3939 | 0:7d428b9b277e | 40 | strncpy(ident, myname, sizeof(ident)); |
okini3939 | 0:7d428b9b277e | 41 | |
okini3939 | 0:7d428b9b277e | 42 | if (host[0] >= '0' && host[0] <= '9') { |
okini3939 | 0:7d428b9b277e | 43 | sscanf(host, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); |
okini3939 | 0:7d428b9b277e | 44 | remote.setIp(IpAddr(ip0, ip1, ip2, ip3)); |
okini3939 | 0:7d428b9b277e | 45 | remote.setName(NULL); |
okini3939 | 0:7d428b9b277e | 46 | } else { |
okini3939 | 0:7d428b9b277e | 47 | remote.setIp(NULL); |
okini3939 | 0:7d428b9b277e | 48 | remote.setName(host); |
okini3939 | 0:7d428b9b277e | 49 | } |
okini3939 | 0:7d428b9b277e | 50 | remote.setPort(LOG_UDPPORT); |
okini3939 | 0:7d428b9b277e | 51 | |
okini3939 | 0:7d428b9b277e | 52 | udpsock = new UDPSocket; |
okini3939 | 0:7d428b9b277e | 53 | udpsock->bind(Host(eth->getIp(), LOG_UDPPORT)); |
okini3939 | 0:7d428b9b277e | 54 | } |
okini3939 | 0:7d428b9b277e | 55 | |
okini3939 | 0:7d428b9b277e | 56 | /** |
okini3939 | 0:7d428b9b277e | 57 | * @brief Send the message |
okini3939 | 0:7d428b9b277e | 58 | * @param tag Process name |
okini3939 | 0:7d428b9b277e | 59 | * @param content Message |
okini3939 | 0:7d428b9b277e | 60 | */ |
okini3939 | 0:7d428b9b277e | 61 | void logger::send (char *tag, char *content) { |
okini3939 | 0:7d428b9b277e | 62 | send(LOG_NOTICE, LOG_USER, tag, content); |
okini3939 | 0:7d428b9b277e | 63 | } |
okini3939 | 0:7d428b9b277e | 64 | |
okini3939 | 0:7d428b9b277e | 65 | /** |
okini3939 | 0:7d428b9b277e | 66 | * @brief Send the message |
okini3939 | 0:7d428b9b277e | 67 | * @param sev Severity |
okini3939 | 0:7d428b9b277e | 68 | * @param fac Facility |
okini3939 | 0:7d428b9b277e | 69 | * @param tag Process name |
okini3939 | 0:7d428b9b277e | 70 | * @param content Message |
okini3939 | 0:7d428b9b277e | 71 | */ |
okini3939 | 0:7d428b9b277e | 72 | void logger::send (LOG_SEVERITY sev, LOG_FACILITY fac, char *tag, char *content) { |
okini3939 | 0:7d428b9b277e | 73 | int pri, len; |
okini3939 | 0:7d428b9b277e | 74 | time_t ctTime; |
okini3939 | 0:7d428b9b277e | 75 | struct tm *t; |
okini3939 | 0:7d428b9b277e | 76 | char logmsg[LOG_LEN]; |
okini3939 | 0:7d428b9b277e | 77 | |
okini3939 | 0:7d428b9b277e | 78 | ctTime = time(NULL); |
okini3939 | 0:7d428b9b277e | 79 | t = localtime(&ctTime); |
okini3939 | 0:7d428b9b277e | 80 | pri = (fac * 8) | sev; |
okini3939 | 0:7d428b9b277e | 81 | |
okini3939 | 0:7d428b9b277e | 82 | 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 | 83 | |
okini3939 | 0:7d428b9b277e | 84 | len = strlen(tag); |
okini3939 | 0:7d428b9b277e | 85 | if (len > 32) len = 32; |
okini3939 | 0:7d428b9b277e | 86 | strncat(logmsg, tag, len); |
okini3939 | 0:7d428b9b277e | 87 | |
okini3939 | 0:7d428b9b277e | 88 | strcat(logmsg, " "); |
okini3939 | 0:7d428b9b277e | 89 | |
okini3939 | 0:7d428b9b277e | 90 | len = strlen(content); |
okini3939 | 0:7d428b9b277e | 91 | if (len > LOG_LEN - strlen(logmsg) - 1) len = LOG_LEN - strlen(logmsg) - 1; |
okini3939 | 0:7d428b9b277e | 92 | strncat(logmsg, content, len); |
okini3939 | 0:7d428b9b277e | 93 | |
okini3939 | 0:7d428b9b277e | 94 | udpsock->sendto(logmsg, strlen(logmsg), &remote); |
okini3939 | 0:7d428b9b277e | 95 | } |