1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#ifndef WINDOWS
#include <syslog.h>
#else
#include "windows.h"
#endif
#include "settings.h"
int debuglevel;
#ifdef WINDOWS
static FILE *syslogfp;
static void openlog(const char *ident, int option, int facility)
{
syslogfp = fopen("syslog.txt", "a");
}
static void syslog(int priority, const char *format, ...)
{
va_list ap;
va_start(ap, format);
if (syslogfp) {
vfprintf(syslogfp, format, ap);
}
va_end(ap);
}
static void closelog(void)
{
fclose(syslogfp);
}
#endif
void debug(char *fmt, ...)
{
time_t now;
struct tm *nowtm;
char nowstr[80];
char b[4096];
va_list ap;
va_start(ap, fmt);
vsnprintf(b, sizeof b, fmt, ap);
now=time(NULL);
nowtm = localtime(&now);
strftime(nowstr, sizeof(nowstr), "%Y-%m-%d %H:%M:%S", nowtm);
if (foreground) {
fprintf(stderr, "%s: %s\n", nowstr, b);
} else {
openlog("pen", LOG_CONS, LOG_USER);
syslog(LOG_DEBUG, "%s\n", b);
closelog();
}
va_end(ap);
}
void error(char *fmt, ...)
{
char b[4096];
va_list ap;
va_start(ap, fmt);
vsnprintf(b, sizeof b, fmt, ap);
fprintf(stderr, "%s\n", b);
if (!foreground) {
openlog("pen", LOG_CONS, LOG_USER);
syslog(LOG_ERR, "%s\n", b);
closelog();
}
va_end(ap);
if (abort_on_error) abort();
else exit(EXIT_FAILURE);
}
|