aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2012-03-05 01:12:18 +0000
committerJonathan McCrohan <jmccrohan@gmail.com>2012-03-05 01:12:18 +0000
commite7b8d92cc55b0c931df6851064e6d36bfa4978f5 (patch)
treef324c44451da2c46829ce0d15fa61be64c1646a6
parent6a055d58464524d68880eecd9de49e68660c013d (diff)
downloadverteco-e7b8d92cc55b0c931df6851064e6d36bfa4978f5.tar.gz
Add syslog logging
-rw-r--r--src/modbuslog.c81
1 files changed, 57 insertions, 24 deletions
diff --git a/src/modbuslog.c b/src/modbuslog.c
index ece8aca..fd1f00d 100644
--- a/src/modbuslog.c
+++ b/src/modbuslog.c
@@ -38,7 +38,7 @@
#include <net/if.h>
#include <string.h>
-
+#include <syslog.h>
// handle SIGALRM by resetting it
void minute_check(int signum) {
@@ -46,7 +46,8 @@ void minute_check(int signum) {
}
// get mac address of primary interface eth0
-char *mac_address() {
+char *
+mac_address() {
int s;
struct ifreq ifr;
@@ -70,18 +71,22 @@ char *mac_address() {
int main(int argc, char *argv[]) {
- const char *configfile = "/etc/modbuslog.cfg";
-
int DEBUG = 0;
+ int SYSLOG_CONSOLE_OUTPUT = 0;
int k;
// check the argv array for strings matching -d
for (k = 1; k < argc; k++) {
if (strcmp(argv[k], "-d") == 0) {
DEBUG = 1;
+ SYSLOG_CONSOLE_OUTPUT = LOG_PERROR;
}
}
+ openlog("modbuslog", SYSLOG_CONSOLE_OUTPUT | LOG_PID | LOG_CONS, LOG_USER);
+ syslog(LOG_INFO, "modbuslog starting");
+
+ const char *configfile = "/etc/modbuslog.cfg";
config_t cfg;
//config_setting_t *setting;
@@ -100,8 +105,10 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
- fprintf(stderr, "Unable to find configfile.\n");
+ syslog(LOG_ERR, "Unable to find configfile");
return -1;
+ } else {
+ syslog(LOG_INFO, "configfile found successfully");
}
// die if core config file options aren't there
@@ -111,16 +118,15 @@ int main(int argc, char *argv[]) {
&& config_lookup_string(&cfg, "modbus.parity", &modbus_parity)
&& config_lookup_int(&cfg, "modbus.stop_bits", &modbus_stop_bits)
&& config_lookup_int(&cfg, "modbus.retry", &modbus_retry))) {
- fprintf(stderr,
- "Incomplete modbus configuration. Check configuration file.\n");
+ syslog(LOG_ERR,
+ "Incomplete modbus configuration. Check configuration file");
+ closelog();
return -1;
}
- if (DEBUG) {
- printf("%s %s %d %c %d %d\n", mac_address(), modbus_device_address,
- modbus_baud_rate, modbus_parity[0], modbus_data_bits,
- modbus_stop_bits);
- }
+ syslog(LOG_INFO, "%s %s %d %c %d %d", mac_address(), modbus_device_address,
+ modbus_baud_rate, modbus_parity[0], modbus_data_bits,
+ modbus_stop_bits);
modbus_t *ctx;
uint16_t tab_reg[4];
@@ -191,7 +197,7 @@ int main(int argc, char *argv[]) {
// if we match the required time for the reading
if (unixtime_min % intervalduration == 0) {
//printf("specified minute. %d %d\n", unixtime_min,
- // intervalduration);
+ // intervalduration);
// attempt to create new modbus connection
ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate,
@@ -200,16 +206,15 @@ int main(int argc, char *argv[]) {
modbus_set_slave(ctx, slaveid);
modbus_set_debug(ctx, DEBUG);
- if (ctx == NULL) {
- fprintf(stderr, "Unable to create libmodbus object.\n");
- //return -1;
+ if (ctx == NULL)
+ {
+ syslog(LOG_ERR, "Unable to create libmodbus object");
}
if (modbus_connect(ctx) == -1) {
- fprintf(stderr, "Connection failed: %s\n",
+ syslog(LOG_ERR, "libmodbus: Connection failed: %s",
modbus_strerror(errno));
modbus_free(ctx);
- //return -1;
}
int retry = 0;
@@ -234,10 +239,11 @@ int main(int argc, char *argv[]) {
break;
}
if (rc == -1) {
- fprintf(stderr, "ERROR: %s\n", modbus_strerror(errno));
+ syslog(LOG_ERR, "libmodbus error: %s",
+ modbus_strerror(errno));
}
retry++;
- } while ((rc != -1) && (retry < (modbus_retry+1)));
+ } while ((rc != -1) && (retry < (modbus_retry + 1)));
//MODBUS_GET_HIGH_BYTE(data);
//MODBUS_GET_LOW_BYTE(data);
@@ -264,6 +270,8 @@ int main(int argc, char *argv[]) {
fclose(fp);
} else {
// file doesn't exist. create it.
+ syslog(LOG_NOTICE, "logfile does not exist");
+ syslog(LOG_INFO, "creating file: %s", filename);
FILE *fp = fopen(filename, "w");
fprintf(
fp,
@@ -273,6 +281,8 @@ int main(int argc, char *argv[]) {
FILE *filehandle = fopen(filename, "a+");
+ syslog(LOG_DEBUG, "opening file for append: %s", filename);
+
int16_t registervalue = 0;
int p;
@@ -304,12 +314,26 @@ int main(int argc, char *argv[]) {
struct tm lc = *localtime(&unixtime_min);
int intervalid;
- FILE *intervalfile = fopen("/var/modbuslog/interval/interval.txt", "r+");
+ FILE *intervalfile = fopen(
+ "/var/modbuslog/interval/interval.txt", "r+");
+
+ if (intervalfile) {
+ fclose(intervalfile);
+ } else {
+ // file doesn't exist. create it.
+ syslog(LOG_NOTICE, "interval file does not exist");
+ syslog(LOG_INFO, "attempting to create file: %s",
+ intervalfile);
+ FILE *intervalfile = fopen(filename, "w");
+ fprintf(intervalfile, "0\n");
+ fclose(intervalfile);
+ }
+
fscanf(intervalfile, "%d", &intervalid);
//handle 32bit signed overflow
- if(intervalid==2147483647){
- intervalid=0;
- }else{
+ if (intervalid == 2147483647) {
+ intervalid = 0;
+ } else {
intervalid++;
}
rewind(intervalfile);
@@ -326,6 +350,15 @@ int main(int argc, char *argv[]) {
registervalue);
fclose(filehandle);
+ syslog(
+ LOG_DEBUG,
+ "%i|%04i%02i%02i|%02i%02i%02i|%04i%02i%02i|%02i%02i%02i|%i|%i|%i\n",
+ intervalid, utc.tm_year + 1900, utc.tm_mon + 1,
+ utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
+ lc.tm_year + 1900, lc.tm_mon + 1, lc.tm_mday,
+ lc.tm_hour, lc.tm_min, lc.tm_sec, slaveid, startaddress,
+ registervalue);
+
modbus_close(ctx);
modbus_free(ctx);