#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <getopt.h>
#include <gxPL.h>
#include "version-git.h"
#define LOGGER_VERSION VERSION_SHORT
#define LOG_FILE_CFG_NAME "filename"
#define LOG_APPEND_CFG_NAME "append"
#define LOGGER_VENDOR "epsirt"
#define LOGGER_DEVICE "logger"
#define DEFAULT_CONFIG_FILE "gxpl-logger.xpl"
static gxPLApplication * app;
static gxPLDevice * device;
static FILE * logfile = NULL;
static const char * log_filename = "";
static bool append_log = false;
static void prvSignalHandler (int s);
static void prvSetConfig (gxPLDevice * device);
static void prvConfigChanged (gxPLDevice * device, void * udata);
static void prvPrintMessage (gxPLApplication * app, gxPLMessage * message, void * udata);
static void prvPrintUsage (void);
int
main (int argc, char * argv[]) {
int c, ret;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h' },
{NULL, 0, NULL, 0}
};
assert (setting);
do {
c = getopt_long (argc, argv, short_options, long_options, NULL);
switch (c) {
case 'h':
prvPrintUsage();
free (setting);
exit (EXIT_SUCCESS);
break;
default:
break;
}
}
while (c != -1);
if (app == NULL) {
fprintf (stderr, "Unable to start xPL");
exit (EXIT_FAILURE);
}
assert (ret == 0);
assert (device);
assert (ret == 0);
assert (ret == 0);
assert (ret == 0);
assert (ret == 0);
assert (ret == 0);
}
prvSetConfig (device);
assert (ret == 0);
signal (SIGTERM, prvSignalHandler);
signal (SIGINT, prvSignalHandler);
assert (ret == 0);
for (;;) {
assert (ret == 0);
}
return 0;
}
static void
prvSetConfig (gxPLDevice * device) {
const char * str;
FILE * new_file = NULL;
append_log = strcasecmp (str, "true") == 0;
}
if ( (strlen (str) != 0) && (strcmp (log_filename, str) != 0)) {
if (strcasecmp (str, "stderr") == 0) {
new_file = stderr;
}
else if (strcasecmp (str, "stdout") == 0) {
new_file = stdout;
}
else {
new_file = fopen (str, (append_log ? "a" : "w"));
}
if (new_file == NULL) {
return;
}
if ( (logfile != NULL) && (logfile != stderr) && (logfile != stdout)) {
fflush (logfile);
fclose (logfile);
}
log_filename = str;
logfile = new_file;
}
}
}
static void
prvPrintMessage (gxPLApplication * app, gxPLMessage * message, void * udata) {
fprintf (logfile, " %s [xpl - message] type = %s",
}
fprintf (logfile, ", source = %s-%s.%s, target = ",
fprintf (logfile, "*");
}
else {
fprintf (logfile, " %s-%s.%s",
}
fprintf (logfile, ", class = %s, type = %s",
fprintf (logfile, "\n");
}
static void
prvConfigChanged (gxPLDevice * device, void * udata) {
prvSetConfig (device);
}
static void
prvSignalHandler (int s) {
int ret;
if ( (logfile != NULL) && (logfile != stderr) && (logfile != stdout)) {
fflush (logfile);
fclose (logfile);
}
assert (ret == 0);
printf ("\neverything was closed.\nHave a nice day !\n");
exit (EXIT_SUCCESS);
}
static void
prvPrintUsage (void) {
printf ("%s - xPL Message Logger\n", __progname);
printf ("Copyright (c) 2015-2016 epsilonRT \n\n");
printf ("Usage: %s [-i interface] [-n network] [-W timeout] [options]\n", __progname);
printf (" -i interface - use interface named interface (i.e. eth0)"
" as network interface\n");
printf (" -n network - use hardware abstraction layer to access the network"
" (i.e. udp, xbeezb... default: udp)\n");
printf (" -W timeout - set the timeout at the opening of the io layer\n");
printf (" -B baudrate - set serial baudrate (if iolayer use serial port)\n");
printf (" -r - performed iolayer reset (if supported)\n");
printf (" -d - enable debugging, it can be doubled or tripled to"
" increase the level of debug. \n");
printf (" -h - print this message\n");
}