RPi: Log refactor
This commit is contained in:
@@ -32,7 +32,7 @@ void hwInit()
|
||||
MY_SERIALDEVICE.begin(MY_BAUD_RATE);
|
||||
#ifdef 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);
|
||||
}
|
||||
#endif
|
||||
@@ -125,7 +125,7 @@ void hwDebugPrint(const char *fmt, ...)
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
mys_log_v(LOG_DEBUG, fmt, args);
|
||||
vlogDebug(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
#endif
|
||||
|
||||
+3
-3
@@ -29,7 +29,7 @@ static SoftEeprom eeprom = SoftEeprom(MY_LINUX_CONFIG_FILE, 1024); // ATMega328
|
||||
void hwInit()
|
||||
{
|
||||
if (!bcm2835_init()) {
|
||||
mys_log(LOG_ERR, "Failed to initialized bcm2835.\n");
|
||||
logError("Failed to initialized bcm2835.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ void hwInit()
|
||||
MY_SERIALDEVICE.begin(MY_BAUD_RATE);
|
||||
#ifdef 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);
|
||||
}
|
||||
#endif
|
||||
@@ -130,7 +130,7 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
mys_log_v(LOG_DEBUG, fmt, args);
|
||||
vlogDebug(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
#endif
|
||||
|
||||
+14
-11
@@ -32,9 +32,9 @@
|
||||
void handle_sigint(int sig)
|
||||
{
|
||||
if (sig == SIGINT) {
|
||||
mys_log(LOG_NOTICE, "Received SIGINT\n\n");
|
||||
logNotice("Received SIGINT\n\n");
|
||||
} else if (sig == SIGTERM) {
|
||||
mys_log(LOG_NOTICE, "Received SIGTERM\n\n");
|
||||
logNotice("Received SIGTERM\n\n");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@@ -47,6 +47,8 @@ void handle_sigint(int sig)
|
||||
MY_SERIALDEVICE.end();
|
||||
#endif
|
||||
|
||||
closelog();
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@@ -57,7 +59,7 @@ static int daemonize(void)
|
||||
/* Fork off the parent process */
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
mys_log(LOG_ERR, "fork: %s\n", strerror(errno));
|
||||
logError("fork: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
/* 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 */
|
||||
sid = setsid();
|
||||
if (sid < 0) {
|
||||
mys_log(LOG_ERR, "setsid: %s\n", strerror(errno));
|
||||
logError("setsid: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Change the current working directory. This prevents the current
|
||||
directory from being locked; hence not being able to remove it. */
|
||||
if ((chdir("/")) < 0) {
|
||||
mys_log(LOG_ERR, "chdir(\"/\"): %s\n", strerror(errno));
|
||||
logError("chdir(\"/\"): %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
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) {
|
||||
mys_log(LOG_ERR, "freopen: %s\n", strerror(errno));
|
||||
logError("freopen: %s\n", strerror(errno));
|
||||
}
|
||||
if (freopen( "/dev/null", "r", stderr) == NULL) {
|
||||
mys_log(LOG_ERR, "freopen: %s\n", strerror(errno));
|
||||
logError("freopen: %s\n", strerror(errno));
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -137,9 +139,10 @@ int main(int argc, char *argv[])
|
||||
log_opts |= LOG_PERROR;
|
||||
}
|
||||
if (!debug) {
|
||||
// Ignore debug type messages
|
||||
setlogmask(LOG_UPTO (LOG_INFO));
|
||||
}
|
||||
openlog(NULL, log_opts, LOG_USER);
|
||||
logOpen(log_opts, LOG_USER);
|
||||
|
||||
if (!foreground && !debug) {
|
||||
if (daemonize() != 0) {
|
||||
@@ -147,8 +150,8 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
mys_log(LOG_INFO, "Starting gateway...\n");
|
||||
mys_log(LOG_INFO, "Protocol version - %s\n", MYSENSORS_LIBRARY_VERSION);
|
||||
logInfo("Starting gateway...\n");
|
||||
logInfo("Protocol version - %s\n", MYSENSORS_LIBRARY_VERSION);
|
||||
|
||||
_begin(); // Startup MySensors library
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ uint8_t SPIClass::is_initialized() {
|
||||
void SPIClass::begin() {
|
||||
if (!initialized) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ uint8_t TwoWire::transmitting = 0;
|
||||
void TwoWire::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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1351,7 +1351,7 @@ int bcm2835_init(void)
|
||||
/* Open the master /dev/mem device */
|
||||
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)) ;
|
||||
goto exit;
|
||||
}
|
||||
@@ -1381,7 +1381,7 @@ int bcm2835_init(void)
|
||||
/* Open the master /dev/mem device */
|
||||
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)) ;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
+11
-11
@@ -131,7 +131,7 @@ void *interruptHandler(void *args) {
|
||||
(void)piHiPri(55); // Only effective if we run as root
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ void *interruptHandler(void *args) {
|
||||
// Wait for it ...
|
||||
ret = poll(&polls, 1, -1);
|
||||
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;
|
||||
}
|
||||
// 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) {
|
||||
uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
|
||||
if (gpioPin == 255) {
|
||||
mys_log(LOG_ERR, "pinMode: invalid pin: %d\n", physPin);
|
||||
logError("pinMode: invalid pin: %d\n", physPin);
|
||||
return;
|
||||
}
|
||||
// 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) {
|
||||
uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
|
||||
if (gpioPin == 255) {
|
||||
mys_log(LOG_ERR, "digitalWrite: invalid pin: %d\n", physPin);
|
||||
logError("digitalWrite: invalid pin: %d\n", physPin);
|
||||
return;
|
||||
}
|
||||
// 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 gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
|
||||
if (gpioPin == 255) {
|
||||
mys_log(LOG_ERR, "digitalRead: invalid pin: %d\n", physPin);
|
||||
logError("digitalRead: invalid pin: %d\n", physPin);
|
||||
return 0;
|
||||
}
|
||||
// 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];
|
||||
if (gpioPin == 255) {
|
||||
mys_log(LOG_ERR, "attachInterrupt: invalid pin: %d\n", physPin);
|
||||
logError("attachInterrupt: invalid pin: %d\n", physPin);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ void rpi_util::attachInterrupt(uint8_t physPin, void (*func)(), uint8_t mode) {
|
||||
|
||||
// Export pin for interrupt
|
||||
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);
|
||||
}
|
||||
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 NONE: fprintf(fd, "none\n"); break;
|
||||
default:
|
||||
mys_log(LOG_ERR, "attachInterrupt: Invalid mode\n");
|
||||
logError("attachInterrupt: Invalid mode\n");
|
||||
fclose(fd);
|
||||
return;
|
||||
}
|
||||
@@ -275,7 +275,7 @@ void rpi_util::attachInterrupt(uint8_t physPin, void (*func)(), uint8_t mode) {
|
||||
ioctl(sysFds[gpioPin], FIONREAD, &count);
|
||||
for (i = 0; i < count; ++i) {
|
||||
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) {
|
||||
uint8_t gpioPin = (physPin > 63)? 255 : physToGpio[physPin];
|
||||
if (gpioPin == 255) {
|
||||
mys_log(LOG_ERR, "detachInterrupt: invalid pin: %d\n", physPin);
|
||||
logError("detachInterrupt: invalid pin: %d\n", physPin);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -309,7 +309,7 @@ void rpi_util::detachInterrupt(uint8_t physPin) {
|
||||
|
||||
FILE *fp = fopen("/sys/class/gpio/unexport", "w");
|
||||
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);
|
||||
}
|
||||
fprintf(fp, "%d", gpioPin);
|
||||
|
||||
Reference in New Issue
Block a user