RPi: Log refactor

This commit is contained in:
Marcelo Aquino
2016-10-18 16:55:47 -02:00
parent 75aad5de35
commit 2be804a1ef
13 changed files with 196 additions and 80 deletions
+2 -2
View File
@@ -32,7 +32,7 @@ void hwInit()
MY_SERIALDEVICE.begin(MY_BAUD_RATE); MY_SERIALDEVICE.begin(MY_BAUD_RATE);
#ifdef MY_LINUX_SERIAL_GROUPNAME #ifdef MY_LINUX_SERIAL_GROUPNAME
if (!MY_SERIALDEVICE.setGroupPerm(MY_LINUX_SERIAL_GROUPNAME)) { if (!MY_SERIALDEVICE.setGroupPerm(MY_LINUX_SERIAL_GROUPNAME)) {
mys_log(LOG_ERR, "Unable to change permission for serial port device.\n"); logError("Unable to change permission for serial port device.\n");
exit(1); exit(1);
} }
#endif #endif
@@ -125,7 +125,7 @@ void hwDebugPrint(const char *fmt, ...)
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
mys_log_v(LOG_DEBUG, fmt, args); vlogDebug(fmt, args);
va_end(args); va_end(args);
} }
#endif #endif
+3 -3
View File
@@ -29,7 +29,7 @@ static SoftEeprom eeprom = SoftEeprom(MY_LINUX_CONFIG_FILE, 1024); // ATMega328
void hwInit() void hwInit()
{ {
if (!bcm2835_init()) { if (!bcm2835_init()) {
mys_log(LOG_ERR, "Failed to initialized bcm2835.\n"); logError("Failed to initialized bcm2835.\n");
exit(1); exit(1);
} }
@@ -37,7 +37,7 @@ void hwInit()
MY_SERIALDEVICE.begin(MY_BAUD_RATE); MY_SERIALDEVICE.begin(MY_BAUD_RATE);
#ifdef MY_LINUX_SERIAL_GROUPNAME #ifdef MY_LINUX_SERIAL_GROUPNAME
if (!MY_SERIALDEVICE.setGroupPerm(MY_LINUX_SERIAL_GROUPNAME)) { if (!MY_SERIALDEVICE.setGroupPerm(MY_LINUX_SERIAL_GROUPNAME)) {
mys_log(LOG_ERR, "Unable to change permission for serial port device.\n"); logError("Unable to change permission for serial port device.\n");
exit(1); exit(1);
} }
#endif #endif
@@ -130,7 +130,7 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
mys_log_v(LOG_DEBUG, fmt, args); vlogDebug(fmt, args);
va_end(args); va_end(args);
} }
#endif #endif
+14 -11
View File
@@ -32,9 +32,9 @@
void handle_sigint(int sig) void handle_sigint(int sig)
{ {
if (sig == SIGINT) { if (sig == SIGINT) {
mys_log(LOG_NOTICE, "Received SIGINT\n\n"); logNotice("Received SIGINT\n\n");
} else if (sig == SIGTERM) { } else if (sig == SIGTERM) {
mys_log(LOG_NOTICE, "Received SIGTERM\n\n"); logNotice("Received SIGTERM\n\n");
} else { } else {
return; return;
} }
@@ -47,6 +47,8 @@ void handle_sigint(int sig)
MY_SERIALDEVICE.end(); MY_SERIALDEVICE.end();
#endif #endif
closelog();
exit(0); exit(0);
} }
@@ -57,7 +59,7 @@ static int daemonize(void)
/* Fork off the parent process */ /* Fork off the parent process */
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
mys_log(LOG_ERR, "fork: %s\n", strerror(errno)); logError("fork: %s\n", strerror(errno));
return -1; return -1;
} }
/* If we got a good PID, then we can exit the parent process. */ /* If we got a good PID, then we can exit the parent process. */
@@ -73,25 +75,25 @@ static int daemonize(void)
/* Create a new SID for the child process */ /* Create a new SID for the child process */
sid = setsid(); sid = setsid();
if (sid < 0) { if (sid < 0) {
mys_log(LOG_ERR, "setsid: %s\n", strerror(errno)); logError("setsid: %s\n", strerror(errno));
return -1; return -1;
} }
/* Change the current working directory. This prevents the current /* Change the current working directory. This prevents the current
directory from being locked; hence not being able to remove it. */ directory from being locked; hence not being able to remove it. */
if ((chdir("/")) < 0) { if ((chdir("/")) < 0) {
mys_log(LOG_ERR, "chdir(\"/\"): %s\n", strerror(errno)); logError("chdir(\"/\"): %s\n", strerror(errno));
return -1; return -1;
} }
if (freopen( "/dev/null", "r", stdin) == NULL) { if (freopen( "/dev/null", "r", stdin) == NULL) {
mys_log(LOG_ERR, "freopen: %s\n", strerror(errno)); logError("freopen: %s\n", strerror(errno));
} }
if (freopen( "/dev/null", "r", stdout) == NULL) { if (freopen( "/dev/null", "r", stdout) == NULL) {
mys_log(LOG_ERR, "freopen: %s\n", strerror(errno)); logError("freopen: %s\n", strerror(errno));
} }
if (freopen( "/dev/null", "r", stderr) == NULL) { if (freopen( "/dev/null", "r", stderr) == NULL) {
mys_log(LOG_ERR, "freopen: %s\n", strerror(errno)); logError("freopen: %s\n", strerror(errno));
} }
return 0; return 0;
@@ -137,9 +139,10 @@ int main(int argc, char *argv[])
log_opts |= LOG_PERROR; log_opts |= LOG_PERROR;
} }
if (!debug) { if (!debug) {
// Ignore debug type messages
setlogmask(LOG_UPTO (LOG_INFO)); setlogmask(LOG_UPTO (LOG_INFO));
} }
openlog(NULL, log_opts, LOG_USER); logOpen(log_opts, LOG_USER);
if (!foreground && !debug) { if (!foreground && !debug) {
if (daemonize() != 0) { if (daemonize() != 0) {
@@ -147,8 +150,8 @@ int main(int argc, char *argv[])
} }
} }
mys_log(LOG_INFO, "Starting gateway...\n"); logInfo("Starting gateway...\n");
mys_log(LOG_INFO, "Protocol version - %s\n", MYSENSORS_LIBRARY_VERSION); logInfo("Protocol version - %s\n", MYSENSORS_LIBRARY_VERSION);
_begin(); // Startup MySensors library _begin(); // Startup MySensors library
+6 -6
View File
@@ -51,7 +51,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
sprintf(port_str, "%hu", port); sprintf(port_str, "%hu", port);
if ((rv = getaddrinfo(host, port_str, &hints, &servinfo)) != 0) { 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; return -1;
} }
@@ -59,13 +59,13 @@ int EthernetClient::connect(const char* host, uint16_t port) {
for (p = servinfo; p != NULL; p = p->ai_next) { for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) { p->ai_protocol)) == -1) {
mys_log(LOG_ERR, "socket: %s\n", strerror(errno)); logError("socket: %s\n", strerror(errno));
continue; continue;
} }
if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd); close(sockfd);
mys_log(LOG_ERR, "connect: %s\n", strerror(errno)); logError("connect: %s\n", strerror(errno));
continue; continue;
} }
@@ -73,7 +73,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
} }
if (p == NULL) { if (p == NULL) {
mys_log(LOG_ERR, "failed to connect\n"); logError("failed to connect\n");
return -1; 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); void *addr = &(((struct sockaddr_in*)p->ai_addr)->sin_addr);
inet_ntop(p->ai_family, addr, s, sizeof s); 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 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) { while (size > 0) {
rc = send(_sock, buf + bytes, size, MSG_NOSIGNAL | MSG_DONTWAIT); rc = send(_sock, buf + bytes, size, MSG_NOSIGNAL | MSG_DONTWAIT);
if (rc == -1) { if (rc == -1) {
mys_log(LOG_ERR, "send: %s\n", strerror(errno)); logError("send: %s\n", strerror(errno));
close(_sock); close(_sock);
_sock = -1; _sock = -1;
break; break;
+11 -11
View File
@@ -57,26 +57,26 @@ void EthernetServer::begin(IPAddress address)
sprintf(portstr, "%d", port); sprintf(portstr, "%d", port);
if ((rv = getaddrinfo(address.toString().c_str(), portstr, &hints, &servinfo)) != 0) { 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; return;
} }
// loop through all the results and bind to the first we can // loop through all the results and bind to the first we can
for (p = servinfo; p != NULL; p = p->ai_next) { for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { 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; continue;
} }
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { 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); freeaddrinfo(servinfo);
return; return;
} }
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd); close(sockfd);
mys_log(LOG_ERR, "bind: %s\n", strerror(errno)); logError("bind: %s\n", strerror(errno));
continue; continue;
} }
@@ -84,13 +84,13 @@ void EthernetServer::begin(IPAddress address)
} }
if (p == NULL) { if (p == NULL) {
mys_log(LOG_ERR, "Failed to bind!\n"); logError("Failed to bind!\n");
freeaddrinfo(servinfo); freeaddrinfo(servinfo);
return; return;
} }
if (listen(sockfd, ETHERNETSERVER_BACKLOG) == -1) { if (listen(sockfd, ETHERNETSERVER_BACKLOG) == -1) {
mys_log(LOG_ERR, "listen: %s\n", strerror(errno)); logError("listen: %s\n", strerror(errno));
freeaddrinfo(servinfo); freeaddrinfo(servinfo);
return; return;
} }
@@ -102,7 +102,7 @@ void EthernetServer::begin(IPAddress address)
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
void *addr = &(ipv4->sin_addr); void *addr = &(ipv4->sin_addr);
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); 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() bool EthernetServer::hasClient()
@@ -142,7 +142,7 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)
client.stop(); client.stop();
clients[i] = clients.back(); clients[i] = clients.back();
clients.pop_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) { if (no_free_slots) {
mys_log(LOG_DEBUG, "Max number of ethernet clients reached.\n"); logDebug("Max number of ethernet clients reached.\n");
return; return;
} }
} }
@@ -203,7 +203,7 @@ void EthernetServer::_accept()
new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size); new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
if (new_fd == -1) { if (new_fd == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) { if (errno != EAGAIN && errno != EWOULDBLOCK) {
mys_log(LOG_ERR, "accept: %s\n", strerror(errno)); logError("accept: %s\n", strerror(errno));
} }
return; return;
} }
@@ -213,5 +213,5 @@ void EthernetServer::_accept()
void *addr = &(((struct sockaddr_in*)&client_addr)->sin_addr); void *addr = &(((struct sockaddr_in*)&client_addr)->sin_addr);
inet_ntop(client_addr.ss_family, addr, ipstr, sizeof ipstr); 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);
} }
+16 -16
View File
@@ -39,7 +39,7 @@ SerialPort::SerialPort(const char *port, bool isPty) : serialPort(std::string(po
void SerialPort::begin(int bauds) void SerialPort::begin(int bauds)
{ {
if (!open(bauds)) { if (!open(bauds)) {
mys_log(LOG_ERR, "Failed to open serial port.\n"); logError("Failed to open serial port.\n");
exit(1); exit(1);
} }
} }
@@ -52,29 +52,29 @@ bool SerialPort::open(int bauds)
if (isPty) { if (isPty) {
sd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY); sd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY);
if (sd < 0) { 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; return false;
} }
if (grantpt(sd) != 0) { 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; return false;
} }
if (unlockpt(sd) != 0) { 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; return false;
} }
/* create a symlink with predictable name to the PTY device */ /* create a symlink with predictable name to the PTY device */
unlink(serialPort.c_str()); // remove the symlink if it already exists unlink(serialPort.c_str()); // remove the symlink if it already exists
if (symlink(ptsname(sd), serialPort.c_str()) != 0) { 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; return false;
} }
} else { } else {
if ((sd = ::open(serialPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY)) == -1) { 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; return false;
} }
@@ -104,7 +104,7 @@ bool SerialPort::open(int bauds)
// Get the current options of the port // Get the current options of the port
if (tcgetattr(sd, &options) < 0) { 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; return false;
} }
@@ -127,13 +127,13 @@ bool SerialPort::open(int bauds)
// Set parameters // Set parameters
if (tcsetattr(sd, TCSANOW, &options) < 0) { 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; return false;
} }
// flush // flush
if (tcflush(sd, TCIOFLUSH) < 0) { 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; return false;
} }
@@ -152,7 +152,7 @@ bool SerialPort::setGroupPerm(const char *groupName)
if (sd != -1 && groupName != NULL) { if (sd != -1 && groupName != NULL) {
devGrp = getgrnam(groupName); devGrp = getgrnam(groupName);
if (devGrp == NULL) { 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; return false;
} }
@@ -164,13 +164,13 @@ bool SerialPort::setGroupPerm(const char *groupName)
ret = chown(dev, -1, devGrp->gr_gid); ret = chown(dev, -1, devGrp->gr_gid);
if (ret == -1) { 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; return false;
} }
ret = chmod(dev, ttyPermissions); ret = chmod(dev, ttyPermissions);
if (ret != 0) { 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; return false;
} }
@@ -184,7 +184,7 @@ int SerialPort::available()
int nbytes = 0; int nbytes = 0;
if (ioctl(sd, FIONREAD, &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); exit(-1);
} }
return nbytes; return nbytes;
@@ -196,7 +196,7 @@ int SerialPort::read()
int ret = ::read(sd, &c, 1); int ret = ::read(sd, &c, 1);
if (ret < 0) { 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) { } else if (ret == 1) {
return c; return c;
} }
@@ -208,7 +208,7 @@ size_t SerialPort::write(uint8_t b)
{ {
int ret = ::write(sd, &b, 1); int ret = ::write(sd, &b, 1);
if (ret < 0) { if (ret < 0) {
mys_log(LOG_ERR, "Serial - write failed: %s\n", strerror(errno)); logError("Serial - write failed: %s\n", strerror(errno));
} }
return ret; return ret;
} }
@@ -217,7 +217,7 @@ size_t SerialPort::write(const uint8_t *buffer, size_t size)
{ {
int ret = ::write(sd, buffer, size); int ret = ::write(sd, buffer, size);
if (ret < 0) { if (ret < 0) {
mys_log(LOG_ERR, "Serial - write failed: %s\n", strerror(errno)); logError("Serial - write failed: %s\n", strerror(errno));
} }
return ret; return ret;
} }
+6 -6
View File
@@ -43,22 +43,22 @@ SoftEeprom::SoftEeprom(const char *fileName, size_t length)
if (stat(_fileName, &fileInfo) != 0) { if (stat(_fileName, &fileInfo) != 0) {
//File does not exist. Create it. //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); std::ofstream myFile(_fileName, std::ios::out | std::ios::binary);
if (!myFile) { 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); exit(1);
} }
myFile.write((const char*)_values, _length); myFile.write((const char*)_values, _length);
myFile.close(); myFile.close();
} else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) { } 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); exit(1);
} else { } else {
//Read config into local memory. //Read config into local memory.
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary); std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
if (!myFile) { 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); exit(1);
} }
myFile.read((char*)_values, _length); myFile.read((char*)_values, _length);
@@ -81,7 +81,7 @@ void SoftEeprom::readBlock(void* buf, void* addr, size_t length)
//Read config into local memory. //Read config into local memory.
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary); std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
if (!myFile) { 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); exit(1);
} }
myFile.read((char*)_values, _length); 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); std::ofstream myFile(_fileName, std::ios::out | std::ios::in | std::ios::binary);
if (!myFile) { 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; return;
} }
myFile.seekp(offs); myFile.seekp(offs);
+106 -6
View File
@@ -1,22 +1,122 @@
#include "log.h"
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <syslog.h> #include <syslog.h>
#include <stdarg.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 void
#ifdef __GNUC__ #ifdef __GNUC__
__attribute__((format(printf, 2, 3))) __attribute__((format(printf, 1, 2)))
#endif #endif
mys_log(int level, const char *fmt, ...) logInfo(const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); 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); va_end(args);
} }
+16 -3
View File
@@ -2,14 +2,27 @@
#define LOG_H #define LOG_H
#include <stdarg.h> #include <stdarg.h>
#include <syslog.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern void mys_log_v(int level, const char *fmt, va_list args); extern void logOpen(int options, int facility);
extern void mys_log(int level, const char *fmt, ...) __attribute__((format(printf,2,3)));
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 #ifdef __cplusplus
} }
+1 -1
View File
@@ -38,7 +38,7 @@ uint8_t SPIClass::is_initialized() {
void SPIClass::begin() { void SPIClass::begin() {
if (!initialized) { if (!initialized) {
if (!bcm2835_spi_begin()) { if (!bcm2835_spi_begin()) {
mys_log(LOG_ERR, "You need to be root to use SPI.\n"); logError("You need root privilege to use SPI.\n");
exit(1); exit(1);
} }
} }
+1 -1
View File
@@ -44,7 +44,7 @@ uint8_t TwoWire::transmitting = 0;
void TwoWire::begin() void TwoWire::begin()
{ {
if (!bcm2835_i2c_begin()) { if (!bcm2835_i2c_begin()) {
mys_log(LOG_ERR, "You need to be root to use I2C.\n"); logError("You need root privilege to use I2C.\n");
exit(1); exit(1);
} }
} }
+3 -3
View File
@@ -1290,7 +1290,7 @@ static void *mapmem(const char *msg, size_t size, int fd, off_t off)
{ {
void *map = mmap(NULL, size, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, off); void *map = mmap(NULL, size, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, off);
if (map == MAP_FAILED) if (map == MAP_FAILED)
mys_log(LOG_ERR, "bcm2835_init: %s mmap failed: %s\n", msg, strerror(errno)); logError("bcm2835_init: %s mmap failed: %s\n", msg, strerror(errno));
return map; return map;
} }
@@ -1351,7 +1351,7 @@ int bcm2835_init(void)
/* Open the master /dev/mem device */ /* Open the master /dev/mem device */
if ((memfd = open("/dev/mem", O_RDWR | O_SYNC) ) < 0) if ((memfd = open("/dev/mem", O_RDWR | O_SYNC) ) < 0)
{ {
mys_log(LOG_ERR, "bcm2835_init: Unable to open /dev/mem: %s\n", logError("bcm2835_init: Unable to open /dev/mem: %s\n",
strerror(errno)) ; strerror(errno)) ;
goto exit; goto exit;
} }
@@ -1381,7 +1381,7 @@ int bcm2835_init(void)
/* Open the master /dev/mem device */ /* Open the master /dev/mem device */
if ((memfd = open("/dev/gpiomem", O_RDWR | O_SYNC) ) < 0) if ((memfd = open("/dev/gpiomem", O_RDWR | O_SYNC) ) < 0)
{ {
mys_log(LOG_ERR, "bcm2835_init: Unable to open /dev/gpiomem: %s\n", logError("bcm2835_init: Unable to open /dev/gpiomem: %s\n",
strerror(errno)) ; strerror(errno)) ;
goto exit; goto exit;
} }
+11 -11
View File
@@ -131,7 +131,7 @@ void *interruptHandler(void *args) {
(void)piHiPri(55); // Only effective if we run as root (void)piHiPri(55); // Only effective if we run as root
if ((fd = sysFds[gpioPin]) == -1) { if ((fd = sysFds[gpioPin]) == -1) {
mys_log(LOG_ERR, "Failed to attach interrupt for pin %d\n", gpioPin); logError("Failed to attach interrupt for pin %d\n", gpioPin);
return NULL; return NULL;
} }
@@ -143,7 +143,7 @@ void *interruptHandler(void *args) {
// Wait for it ... // Wait for it ...
ret = poll(&polls, 1, -1); ret = poll(&polls, 1, -1);
if (ret < 0) { if (ret < 0) {
mys_log(LOG_ERR, "Error waiting for interrupt: %s\n", strerror(errno)); logError("Error waiting for interrupt: %s\n", strerror(errno));
break; break;
} }
// Do a dummy read to clear the interrupt // Do a dummy read to clear the interrupt
@@ -163,7 +163,7 @@ void *interruptHandler(void *args) {
void rpi_util::pinMode(uint8_t physPin, uint8_t mode) { void rpi_util::pinMode(uint8_t physPin, uint8_t mode) {
uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin]; uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
if (gpioPin == 255) { if (gpioPin == 255) {
mys_log(LOG_ERR, "pinMode: invalid pin: %d\n", physPin); logError("pinMode: invalid pin: %d\n", physPin);
return; return;
} }
// Check if SPI is in use and target pin is related to SPI // Check if SPI is in use and target pin is related to SPI
@@ -177,7 +177,7 @@ void rpi_util::pinMode(uint8_t physPin, uint8_t mode) {
void rpi_util::digitalWrite(uint8_t physPin, uint8_t value) { void rpi_util::digitalWrite(uint8_t physPin, uint8_t value) {
uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin]; uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
if (gpioPin == 255) { if (gpioPin == 255) {
mys_log(LOG_ERR, "digitalWrite: invalid pin: %d\n", physPin); logError("digitalWrite: invalid pin: %d\n", physPin);
return; return;
} }
// Check if SPI is in use and target pin is related to SPI // Check if SPI is in use and target pin is related to SPI
@@ -195,7 +195,7 @@ void rpi_util::digitalWrite(uint8_t physPin, uint8_t value) {
uint8_t rpi_util::digitalRead(uint8_t physPin) { uint8_t rpi_util::digitalRead(uint8_t physPin) {
uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin]; uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
if (gpioPin == 255) { if (gpioPin == 255) {
mys_log(LOG_ERR, "digitalRead: invalid pin: %d\n", physPin); logError("digitalRead: invalid pin: %d\n", physPin);
return 0; return 0;
} }
// Check if SPI is in use and target pin is related to SPI // Check if SPI is in use and target pin is related to SPI
@@ -214,7 +214,7 @@ void rpi_util::attachInterrupt(uint8_t physPin, void (*func)(), uint8_t mode) {
uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin]; uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
if (gpioPin == 255) { if (gpioPin == 255) {
mys_log(LOG_ERR, "attachInterrupt: invalid pin: %d\n", physPin); logError("attachInterrupt: invalid pin: %d\n", physPin);
return; return;
} }
@@ -229,7 +229,7 @@ void rpi_util::attachInterrupt(uint8_t physPin, void (*func)(), uint8_t mode) {
// Export pin for interrupt // Export pin for interrupt
if ((fd = fopen("/sys/class/gpio/export", "w")) == NULL) { if ((fd = fopen("/sys/class/gpio/export", "w")) == NULL) {
mys_log(LOG_ERR, "attachInterrupt: Unable to export pin %d for interrupt: %s\n", physPin, strerror(errno)); logError("attachInterrupt: Unable to export pin %d for interrupt: %s\n", physPin, strerror(errno));
exit(1); exit(1);
} }
fprintf(fd, "%d\n", gpioPin); fprintf(fd, "%d\n", gpioPin);
@@ -257,7 +257,7 @@ void rpi_util::attachInterrupt(uint8_t physPin, void (*func)(), uint8_t mode) {
case RISING: fprintf(fd, "rising\n"); break; case RISING: fprintf(fd, "rising\n"); break;
case NONE: fprintf(fd, "none\n"); break; case NONE: fprintf(fd, "none\n"); break;
default: default:
mys_log(LOG_ERR, "attachInterrupt: Invalid mode\n"); logError("attachInterrupt: Invalid mode\n");
fclose(fd); fclose(fd);
return; return;
} }
@@ -275,7 +275,7 @@ void rpi_util::attachInterrupt(uint8_t physPin, void (*func)(), uint8_t mode) {
ioctl(sysFds[gpioPin], FIONREAD, &count); ioctl(sysFds[gpioPin], FIONREAD, &count);
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
if (read(sysFds[gpioPin], &c, 1) == -1) { if (read(sysFds[gpioPin], &c, 1) == -1) {
mys_log(LOG_ERR, "attachInterrupt: failed to read pin status: %s\n", strerror(errno)); logError("attachInterrupt: failed to read pin status: %s\n", strerror(errno));
} }
} }
@@ -290,7 +290,7 @@ void rpi_util::attachInterrupt(uint8_t physPin, void (*func)(), uint8_t mode) {
void rpi_util::detachInterrupt(uint8_t physPin) { void rpi_util::detachInterrupt(uint8_t physPin) {
uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin]; uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
if (gpioPin == 255) { if (gpioPin == 255) {
mys_log(LOG_ERR, "detachInterrupt: invalid pin: %d\n", physPin); logError("detachInterrupt: invalid pin: %d\n", physPin);
return; return;
} }
@@ -309,7 +309,7 @@ void rpi_util::detachInterrupt(uint8_t physPin) {
FILE *fp = fopen("/sys/class/gpio/unexport", "w"); FILE *fp = fopen("/sys/class/gpio/unexport", "w");
if (fp == NULL) { if (fp == NULL) {
mys_log(LOG_ERR, "Unable to unexport pin %d for interrupt\n", gpioPin); logError("Unable to unexport pin %d for interrupt\n", gpioPin);
exit(1); exit(1);
} }
fprintf(fp, "%d", gpioPin); fprintf(fp, "%d", gpioPin);