RPi: Log refactor
This commit is contained in:
@@ -51,7 +51,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
|
||||
|
||||
sprintf(port_str, "%hu", port);
|
||||
if ((rv = getaddrinfo(host, port_str, &hints, &servinfo)) != 0) {
|
||||
mys_log(LOG_ERR, "getaddrinfo: %s\n", gai_strerror(rv));
|
||||
logError("getaddrinfo: %s\n", gai_strerror(rv));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -59,13 +59,13 @@ int EthernetClient::connect(const char* host, uint16_t port) {
|
||||
for (p = servinfo; p != NULL; p = p->ai_next) {
|
||||
if ((sockfd = socket(p->ai_family, p->ai_socktype,
|
||||
p->ai_protocol)) == -1) {
|
||||
mys_log(LOG_ERR, "socket: %s\n", strerror(errno));
|
||||
logError("socket: %s\n", strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
|
||||
close(sockfd);
|
||||
mys_log(LOG_ERR, "connect: %s\n", strerror(errno));
|
||||
logError("connect: %s\n", strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
|
||||
}
|
||||
|
||||
if (p == NULL) {
|
||||
mys_log(LOG_ERR, "failed to connect\n");
|
||||
logError("failed to connect\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
|
||||
|
||||
void *addr = &(((struct sockaddr_in*)p->ai_addr)->sin_addr);
|
||||
inet_ntop(p->ai_family, addr, s, sizeof s);
|
||||
mys_log(LOG_DEBUG, "connected to %s\n", s);
|
||||
logDebug("connected to %s\n", s);
|
||||
|
||||
freeaddrinfo(servinfo); // all done with this structure
|
||||
|
||||
@@ -107,7 +107,7 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {
|
||||
while (size > 0) {
|
||||
rc = send(_sock, buf + bytes, size, MSG_NOSIGNAL | MSG_DONTWAIT);
|
||||
if (rc == -1) {
|
||||
mys_log(LOG_ERR, "send: %s\n", strerror(errno));
|
||||
logError("send: %s\n", strerror(errno));
|
||||
close(_sock);
|
||||
_sock = -1;
|
||||
break;
|
||||
|
||||
@@ -57,26 +57,26 @@ void EthernetServer::begin(IPAddress address)
|
||||
|
||||
sprintf(portstr, "%d", port);
|
||||
if ((rv = getaddrinfo(address.toString().c_str(), portstr, &hints, &servinfo)) != 0) {
|
||||
mys_log(LOG_ERR, "getaddrinfo: %s\n", gai_strerror(rv));
|
||||
logError("getaddrinfo: %s\n", gai_strerror(rv));
|
||||
return;
|
||||
}
|
||||
|
||||
// loop through all the results and bind to the first we can
|
||||
for (p = servinfo; p != NULL; p = p->ai_next) {
|
||||
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
||||
mys_log(LOG_ERR, "socket: %s\n", strerror(errno));
|
||||
logError("socket: %s\n", strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
|
||||
mys_log(LOG_ERR, "setsockopt: %s\n", strerror(errno));
|
||||
logError("setsockopt: %s\n", strerror(errno));
|
||||
freeaddrinfo(servinfo);
|
||||
return;
|
||||
}
|
||||
|
||||
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
|
||||
close(sockfd);
|
||||
mys_log(LOG_ERR, "bind: %s\n", strerror(errno));
|
||||
logError("bind: %s\n", strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -84,13 +84,13 @@ void EthernetServer::begin(IPAddress address)
|
||||
}
|
||||
|
||||
if (p == NULL) {
|
||||
mys_log(LOG_ERR, "Failed to bind!\n");
|
||||
logError("Failed to bind!\n");
|
||||
freeaddrinfo(servinfo);
|
||||
return;
|
||||
}
|
||||
|
||||
if (listen(sockfd, ETHERNETSERVER_BACKLOG) == -1) {
|
||||
mys_log(LOG_ERR, "listen: %s\n", strerror(errno));
|
||||
logError("listen: %s\n", strerror(errno));
|
||||
freeaddrinfo(servinfo);
|
||||
return;
|
||||
}
|
||||
@@ -102,7 +102,7 @@ void EthernetServer::begin(IPAddress address)
|
||||
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
|
||||
void *addr = &(ipv4->sin_addr);
|
||||
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
|
||||
mys_log(LOG_DEBUG, "Listening for connections on %s:%s\n", ipstr, portstr);
|
||||
logDebug("Listening for connections on %s:%s\n", ipstr, portstr);
|
||||
}
|
||||
|
||||
bool EthernetServer::hasClient()
|
||||
@@ -142,7 +142,7 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)
|
||||
client.stop();
|
||||
clients[i] = clients.back();
|
||||
clients.pop_back();
|
||||
mys_log(LOG_DEBUG, "Client disconnected.\n");
|
||||
logDebug("Client disconnected.\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +194,7 @@ void EthernetServer::_accept()
|
||||
}
|
||||
}
|
||||
if (no_free_slots) {
|
||||
mys_log(LOG_DEBUG, "Max number of ethernet clients reached.\n");
|
||||
logDebug("Max number of ethernet clients reached.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -203,7 +203,7 @@ void EthernetServer::_accept()
|
||||
new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
|
||||
if (new_fd == -1) {
|
||||
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||
mys_log(LOG_ERR, "accept: %s\n", strerror(errno));
|
||||
logError("accept: %s\n", strerror(errno));
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -213,5 +213,5 @@ void EthernetServer::_accept()
|
||||
|
||||
void *addr = &(((struct sockaddr_in*)&client_addr)->sin_addr);
|
||||
inet_ntop(client_addr.ss_family, addr, ipstr, sizeof ipstr);
|
||||
mys_log(LOG_DEBUG, "New connection from %s\n", ipstr);
|
||||
logDebug("New connection from %s\n", ipstr);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ SerialPort::SerialPort(const char *port, bool isPty) : serialPort(std::string(po
|
||||
void SerialPort::begin(int bauds)
|
||||
{
|
||||
if (!open(bauds)) {
|
||||
mys_log(LOG_ERR, "Failed to open serial port.\n");
|
||||
logError("Failed to open serial port.\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -52,29 +52,29 @@ bool SerialPort::open(int bauds)
|
||||
if (isPty) {
|
||||
sd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (sd < 0) {
|
||||
mys_log(LOG_ERR, "Couldn't open a PTY: %s\n", strerror(errno));
|
||||
logError("Couldn't open a PTY: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (grantpt(sd) != 0) {
|
||||
mys_log(LOG_ERR, "Couldn't grant permission to the PTY: %s\n", strerror(errno));
|
||||
logError("Couldn't grant permission to the PTY: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (unlockpt(sd) != 0) {
|
||||
mys_log(LOG_ERR, "Couldn't unlock the PTY: %s\n", strerror(errno));
|
||||
logError("Couldn't unlock the PTY: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
/* create a symlink with predictable name to the PTY device */
|
||||
unlink(serialPort.c_str()); // remove the symlink if it already exists
|
||||
if (symlink(ptsname(sd), serialPort.c_str()) != 0) {
|
||||
mys_log(LOG_ERR, "Couldn't create a symlink '%s' to PTY! (%d) %s\n", serialPort.c_str(), errno, strerror(errno));
|
||||
logError("Couldn't create a symlink '%s' to PTY! (%d) %s\n", serialPort.c_str(), errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((sd = ::open(serialPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY)) == -1) {
|
||||
mys_log(LOG_ERR, "Unable to open the serial port %s\n", serialPort.c_str());
|
||||
logError("Unable to open the serial port %s\n", serialPort.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ bool SerialPort::open(int bauds)
|
||||
|
||||
// Get the current options of the port
|
||||
if (tcgetattr(sd, &options) < 0) {
|
||||
mys_log(LOG_ERR, "Couldn't get term attributes: %s\n", strerror(errno));
|
||||
logError("Couldn't get term attributes: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -127,13 +127,13 @@ bool SerialPort::open(int bauds)
|
||||
|
||||
// Set parameters
|
||||
if (tcsetattr(sd, TCSANOW, &options) < 0) {
|
||||
mys_log(LOG_ERR, "Couldn't set term attributes: %s\n", strerror(errno));
|
||||
logError("Couldn't set term attributes: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
// flush
|
||||
if (tcflush(sd, TCIOFLUSH) < 0) {
|
||||
mys_log(LOG_ERR, "Couldn't flush serial: %s\n", strerror(errno));
|
||||
logError("Couldn't flush serial: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ bool SerialPort::setGroupPerm(const char *groupName)
|
||||
if (sd != -1 && groupName != NULL) {
|
||||
devGrp = getgrnam(groupName);
|
||||
if (devGrp == NULL) {
|
||||
mys_log(LOG_ERR, "getgrnam: %s failed. (%d) %s\n", groupName, errno, strerror(errno));
|
||||
logError("getgrnam: %s failed. (%d) %s\n", groupName, errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -164,13 +164,13 @@ bool SerialPort::setGroupPerm(const char *groupName)
|
||||
|
||||
ret = chown(dev, -1, devGrp->gr_gid);
|
||||
if (ret == -1) {
|
||||
mys_log(LOG_ERR, "Could not change PTY owner! (%d) %s\n", errno, strerror(errno));
|
||||
logError("Could not change PTY owner! (%d) %s\n", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = chmod(dev, ttyPermissions);
|
||||
if (ret != 0) {
|
||||
mys_log(LOG_ERR, "Could not change PTY permissions! (%d) %s\n", errno, strerror(errno));
|
||||
logError("Could not change PTY permissions! (%d) %s\n", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -184,7 +184,7 @@ int SerialPort::available()
|
||||
int nbytes = 0;
|
||||
|
||||
if (ioctl(sd, FIONREAD, &nbytes) < 0) {
|
||||
mys_log(LOG_ERR, "Failed to get byte count on serial.\n");
|
||||
logError("Failed to get byte count on serial.\n");
|
||||
exit(-1);
|
||||
}
|
||||
return nbytes;
|
||||
@@ -196,7 +196,7 @@ int SerialPort::read()
|
||||
|
||||
int ret = ::read(sd, &c, 1);
|
||||
if (ret < 0) {
|
||||
mys_log(LOG_ERR, "Serial - read failed: %s\n", strerror(errno));
|
||||
logError("Serial - read failed: %s\n", strerror(errno));
|
||||
} else if (ret == 1) {
|
||||
return c;
|
||||
}
|
||||
@@ -208,7 +208,7 @@ size_t SerialPort::write(uint8_t b)
|
||||
{
|
||||
int ret = ::write(sd, &b, 1);
|
||||
if (ret < 0) {
|
||||
mys_log(LOG_ERR, "Serial - write failed: %s\n", strerror(errno));
|
||||
logError("Serial - write failed: %s\n", strerror(errno));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -217,7 +217,7 @@ size_t SerialPort::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
int ret = ::write(sd, buffer, size);
|
||||
if (ret < 0) {
|
||||
mys_log(LOG_ERR, "Serial - write failed: %s\n", strerror(errno));
|
||||
logError("Serial - write failed: %s\n", strerror(errno));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -43,22 +43,22 @@ SoftEeprom::SoftEeprom(const char *fileName, size_t length)
|
||||
|
||||
if (stat(_fileName, &fileInfo) != 0) {
|
||||
//File does not exist. Create it.
|
||||
mys_log(LOG_INFO, "Config file %s does not exist, creating new config file.\n", _fileName);
|
||||
logInfo("Config file %s does not exist, creating new config file.\n", _fileName);
|
||||
std::ofstream myFile(_fileName, std::ios::out | std::ios::binary);
|
||||
if (!myFile) {
|
||||
mys_log(LOG_ERR, "Unable to create config file %s.\n", _fileName);
|
||||
logError("Unable to create config file %s.\n", _fileName);
|
||||
exit(1);
|
||||
}
|
||||
myFile.write((const char*)_values, _length);
|
||||
myFile.close();
|
||||
} else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) {
|
||||
mys_log(LOG_ERR, "Config file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n", _fileName, _length);
|
||||
logError("Config file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n", _fileName, _length);
|
||||
exit(1);
|
||||
} else {
|
||||
//Read config into local memory.
|
||||
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
|
||||
if (!myFile) {
|
||||
mys_log(LOG_ERR, "Unable to open config to file %s for reading.\n", _fileName);
|
||||
logError("Unable to open config to file %s for reading.\n", _fileName);
|
||||
exit(1);
|
||||
}
|
||||
myFile.read((char*)_values, _length);
|
||||
@@ -81,7 +81,7 @@ void SoftEeprom::readBlock(void* buf, void* addr, size_t length)
|
||||
//Read config into local memory.
|
||||
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
|
||||
if (!myFile) {
|
||||
mys_log(LOG_ERR, "Unable to open config to file %s for reading.\n", _fileName);
|
||||
logError("Unable to open config to file %s for reading.\n", _fileName);
|
||||
exit(1);
|
||||
}
|
||||
myFile.read((char*)_values, _length);
|
||||
@@ -104,7 +104,7 @@ void SoftEeprom::writeBlock(void* buf, void* addr, size_t length)
|
||||
|
||||
std::ofstream myFile(_fileName, std::ios::out | std::ios::in | std::ios::binary);
|
||||
if (!myFile) {
|
||||
mys_log(LOG_ERR, "Unable to write config to file %s.\n", _fileName);
|
||||
logError("Unable to write config to file %s.\n", _fileName);
|
||||
return;
|
||||
}
|
||||
myFile.seekp(offs);
|
||||
|
||||
+106
-6
@@ -1,22 +1,122 @@
|
||||
#include "log.h"
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <syslog.h>
|
||||
#include <stdarg.h>
|
||||
#include "log.h"
|
||||
|
||||
void mys_log_v(int level, const char *fmt, va_list args)
|
||||
// Default values
|
||||
static const int log_opts = LOG_CONS | LOG_PERROR; // print syslog to stderror
|
||||
static const int log_facility = LOG_USER;
|
||||
|
||||
static uint8_t log_open = 0;
|
||||
|
||||
void logOpen(int options, int facility)
|
||||
{
|
||||
vsyslog(level, fmt, args);
|
||||
openlog(NULL, options, facility);
|
||||
log_open = 1;
|
||||
}
|
||||
|
||||
void vlogInfo(const char *fmt, va_list args)
|
||||
{
|
||||
if (!log_open) {
|
||||
logOpen(log_opts, log_facility);
|
||||
}
|
||||
vsyslog(LOG_INFO, fmt, args);
|
||||
}
|
||||
|
||||
void
|
||||
#ifdef __GNUC__
|
||||
__attribute__((format(printf, 2, 3)))
|
||||
__attribute__((format(printf, 1, 2)))
|
||||
#endif
|
||||
mys_log(int level, const char *fmt, ...)
|
||||
logInfo(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
mys_log_v(level, fmt, args);
|
||||
vlogInfo(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void vlogError(const char *fmt, va_list args)
|
||||
{
|
||||
if (!log_open) {
|
||||
logOpen(log_opts, log_facility);
|
||||
}
|
||||
vsyslog(LOG_ERR, fmt, args);
|
||||
}
|
||||
|
||||
void
|
||||
#ifdef __GNUC__
|
||||
__attribute__((format(printf, 1, 2)))
|
||||
#endif
|
||||
logError(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
vlogError(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void vlogNotice(const char *fmt, va_list args)
|
||||
{
|
||||
if (!log_open) {
|
||||
logOpen(log_opts, log_facility);
|
||||
}
|
||||
vsyslog(LOG_NOTICE, fmt, args);
|
||||
}
|
||||
|
||||
void
|
||||
#ifdef __GNUC__
|
||||
__attribute__((format(printf, 1, 2)))
|
||||
#endif
|
||||
logNotice(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
vlogNotice(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void vlogDebug(const char *fmt, va_list args)
|
||||
{
|
||||
if (!log_open) {
|
||||
logOpen(log_opts, log_facility);
|
||||
}
|
||||
vsyslog(LOG_DEBUG, fmt, args);
|
||||
}
|
||||
|
||||
void
|
||||
#ifdef __GNUC__
|
||||
__attribute__((format(printf, 1, 2)))
|
||||
#endif
|
||||
logDebug(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
vlogDebug(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void vlogWarning(const char *fmt, va_list args)
|
||||
{
|
||||
if (!log_open) {
|
||||
logOpen(log_opts, log_facility);
|
||||
}
|
||||
vsyslog(LOG_WARNING, fmt, args);
|
||||
}
|
||||
|
||||
void
|
||||
#ifdef __GNUC__
|
||||
__attribute__((format(printf, 1, 2)))
|
||||
#endif
|
||||
logWarning(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
vlogWarning(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
+16
-3
@@ -2,14 +2,27 @@
|
||||
#define LOG_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void mys_log_v(int level, const char *fmt, va_list args);
|
||||
extern void mys_log(int level, const char *fmt, ...) __attribute__((format(printf,2,3)));
|
||||
extern void logOpen(int options, int facility);
|
||||
|
||||
extern void vlogInfo(const char *fmt, va_list args);
|
||||
extern void logInfo(const char *fmt, ...) __attribute__((format(printf,1,2)));
|
||||
|
||||
extern void vlogError(const char *fmt, va_list args);
|
||||
extern void logError(const char *fmt, ...) __attribute__((format(printf,1,2)));
|
||||
|
||||
extern void vlogNotice(const char *fmt, va_list args);
|
||||
extern void logNotice(const char *fmt, ...) __attribute__((format(printf,1,2)));
|
||||
|
||||
extern void vlogDebug(const char *fmt, va_list args);
|
||||
extern void logDebug(const char *fmt, ...) __attribute__((format(printf,1,2)));
|
||||
|
||||
extern void vlogWarning(const char *fmt, va_list args);
|
||||
extern void logWarning(const char *fmt, ...) __attribute__((format(printf,1,2)));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user