146 lines
2.7 KiB
C++
146 lines
2.7 KiB
C++
//
|
|
// Serial Daemon (similar to ser2net)
|
|
// Neal Probert
|
|
//
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <signal.h>
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/wait.h>
|
|
#include <arpa/inet.h>
|
|
|
|
#include "LogDaemon.h"
|
|
|
|
#include "netlib.h"
|
|
|
|
extern int optind, opterr, optopt;
|
|
|
|
//
|
|
// any port
|
|
//
|
|
|
|
static const char *helptext =
|
|
"logd [options]\n"
|
|
"\t-d debug (foreground with stdio)\n"
|
|
"\t-h help\n"
|
|
"\t-i infile input log format:/path/to/file.log\n"
|
|
"\t-o logfile output log format:/path/to/file.log\n"
|
|
"\t-S service UDP service\n";
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
char c; /* we have character */
|
|
|
|
LogDaemon Log;
|
|
|
|
/* options */
|
|
int debug = 0; // run in foreground, leaves stdio open
|
|
|
|
/* connections */
|
|
const char *file = NULL;
|
|
const char *log = NULL;
|
|
const char *service = LOGD_PORT;
|
|
|
|
// add command line args
|
|
while ( (c = getopt( argc, argv, "bdhi:o:S:" )) > 0 )
|
|
{
|
|
switch ( c )
|
|
{
|
|
case 'b':
|
|
Log.SetBinary();
|
|
break;
|
|
case 'd':
|
|
debug = 1;
|
|
break;
|
|
case 'h':
|
|
puts( helptext );
|
|
return 0;
|
|
case 'i':
|
|
file = optarg;
|
|
break;
|
|
case 'o':
|
|
log = optarg;
|
|
break;
|
|
case 'S':
|
|
service = optarg;
|
|
break;
|
|
default:
|
|
fprintf( stderr, "Unknown option -%c\n", c );
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
if ( log )
|
|
{
|
|
if ( Log.LogOpen( log ) < 0 )
|
|
{
|
|
fprintf( stderr, "Unable to open log file: %s!\n", log );
|
|
exit(1);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
fprintf( stderr, "Must specify a MySQL connection: mysql://user:pass@host/database/table\n" );
|
|
exit(1);
|
|
}
|
|
|
|
if ( file )
|
|
{
|
|
char buf[4096];
|
|
int n = 0;
|
|
|
|
if ( strcmp( file, "-") == 0 )
|
|
{
|
|
while ( fgets(buf, sizeof(buf), stdin) )
|
|
{
|
|
Log.LogWrite( buf );
|
|
n++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// import
|
|
FILE *import = fopen( file, "r" );
|
|
if ( import )
|
|
{
|
|
// read into database
|
|
while ( fgets( buf, sizeof(buf), import ) )
|
|
{
|
|
// check for header?
|
|
|
|
Log.LogWrite( buf );
|
|
n++;
|
|
}
|
|
printf( "Imported %d lines into log\n", n );
|
|
log_printf( "Imported %d lines into log\n", n );
|
|
}
|
|
else
|
|
{
|
|
fprintf( stderr, "Unable to import log data: %s\n", file );
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
// UDP listener
|
|
if ( Log.StartService( LOGD_NAME, "0.0.0.0", service, debug ) >= 0 )
|
|
return Log.RunService();
|
|
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|