Skip to content

Commit

Permalink
+ added syslog support
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex 'AdUser' Z committed Aug 17, 2011
1 parent e09f2dd commit 84e2e7f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions README
Expand Up @@ -4,6 +4,7 @@ this tarball.
Dependencies:
libmagic (included with 'file')
libpcre (may be included in your distribution's pcre package)
syslog (optional)

To install fsniper:
./configure
Expand Down
52 changes: 34 additions & 18 deletions src/log.c
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <time.h>
#include "log.h"
#include "util.h"
Expand All @@ -33,31 +34,39 @@
#include <efence.h>
#endif

FILE *_logfd;
FILE *_logfd = NULL;

extern int logtostdout;
extern int logtype;

int log_open()
{
char *configdir, *logfile;
char *version = PACKAGE_VERSION;
char *openstr = NULL;

if (!logtostdout)
switch (logtype)
{
configdir = get_config_dir();
logfile = malloc(strlen(configdir) + strlen("/log") + 1);
sprintf(logfile, "%s/log", configdir);
free(configdir);

_logfd = fopen(logfile, "a");

free(logfile);
case LOG_FILE :
configdir = get_config_dir();
logfile = malloc(strlen(configdir) + strlen("/log") + 1);
sprintf(logfile, "%s/log", configdir);

_logfd = fopen(logfile, "a");

free(configdir);
free(logfile);
break;
case LOG_STDOUT :
_logfd = stdout;
break;
case LOG_SYS :
openlog("fsniper", LOG_CONS, LOG_USER);
case LOG_NONE :
_logfd = fopen("/dev/null", "a");
break;
}
else
_logfd = stdout;

if (_logfd)
if (logtype != LOG_SYS && _logfd)
{
int pid = getpid();

Expand All @@ -73,13 +82,13 @@ int log_open()
free(openstr);
}

return (_logfd != NULL);
return (_logfd != NULL);
}

int log_write(char *str, ...)
{
va_list va;
int len;
int len = 0;
time_t t;
char readabletime[30];
t = time(NULL);
Expand All @@ -88,10 +97,14 @@ int log_write(char *str, ...)
fprintf(_logfd, "%s ", readabletime);

va_start(va, str);
len = vfprintf(_logfd, str, va);
if (logtype != LOG_SYS)
{
len = vfprintf(_logfd, str, va);
fflush(_logfd);
} else
vsyslog(LOG_INFO, str, va);
va_end(va);

fflush(_logfd);
return len;
}

Expand All @@ -100,5 +113,8 @@ int log_close()
if (_logfd && _logfd != stdout)
fclose(_logfd);

if (logtype == LOG_SYS)
closelog();

return 1;
}
5 changes: 5 additions & 0 deletions src/log.h
Expand Up @@ -3,6 +3,11 @@

#include <stdio.h>

#define LOG_NONE 0x0
#define LOG_FILE 0x1
#define LOG_STDOUT 0x2
#define LOG_SYS 0x3

int log_open();
int log_write(char *str, ...);
int log_close();
Expand Down
15 changes: 12 additions & 3 deletions src/main.c
Expand Up @@ -66,8 +66,7 @@ int verbose = 0;
/* synchronous mode, no forking for handlers */
int syncmode = 0;

/* whether to log to stdout or not */
int logtostdout = 0;
int logtype = 0;

/* inotify file descriptor */
int ifd;
Expand Down Expand Up @@ -277,6 +276,8 @@ int main(int argc, char** argv)
argument_register(argument, "verbose", "Turns on debug text.", 0);
argument_register(argument, "sync", "Sync mode (for debugging).", 0);
argument_register(argument, "log-to-stdout", "Log to stdout alongside the usual log file.", 0);
argument_register(argument, "log-to-file", "Log to file (usually - ~/.config/fsniper/log)", 0);
argument_register(argument, "log-to-syslog", "Log to system log.", 0);

if ((error_str = argument_parse(argument, argc, argv))) {
fprintf(stderr, "Error in arguments: %s", error_str);
Expand Down Expand Up @@ -306,8 +307,16 @@ int main(int argc, char** argv)
if (argument_exists(argument, "sync"))
syncmode = 1;

if (argument_exists(argument, "log-to-file")) {
logtype = LOG_FILE;
}

if (argument_exists(argument, "log-to-stdout")) {
logtostdout = 1;
logtype = LOG_STDOUT;
}

if (argument_exists(argument, "log-to-syslog")) {
logtype = LOG_SYS;
}

/* get config dir (must free this) */
Expand Down

0 comments on commit 84e2e7f

Please sign in to comment.