diff options
| author | Brian Cully <bjc@kublai.com> | 2012-02-06 19:44:47 -0500 |
|---|---|---|
| committer | Brian Cully <github.20.shmit@spamgourmet.com> | 2012-02-06 19:46:56 -0500 |
| commit | 50f3c688a7cd4819680dbd19b9933c6cb5e62ca7 (patch) | |
| tree | 5d9bf2edac40d874ec05bafc1e1e38b443879ce3 /lib/src/log.c | |
| parent | d3b118481833acd44d724c06ce8c9d6b6ff2838a (diff) | |
| download | mysqlerl-50f3c688a7cd4819680dbd19b9933c6cb5e62ca7.tar.gz mysqlerl-50f3c688a7cd4819680dbd19b9933c6cb5e62ca7.zip | |
Move most files into lib, add top-level makefile
Diffstat (limited to 'lib/src/log.c')
| -rw-r--r-- | lib/src/log.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/src/log.c b/lib/src/log.c new file mode 100644 index 0000000..80aa755 --- /dev/null +++ b/lib/src/log.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2008, Brian Cully <bjc@kublai.com> + */ + +#include "log.h" + +#include <stdio.h> +#include <stdarg.h> +#include <time.h> +#include <unistd.h> + +const char *LOGPATH = "/tmp/mysqlerl.log"; +static FILE *logfile = NULL; + +void +openlog() +{ + logfile = fopen(LOGPATH, "a"); +} + +void +closelog() +{ + fclose(logfile); +} + +void +logmsg(const char *format, ...) +{ + FILE *out = logfile; + char timebuf[32] = "\0"; + struct tm now_tm; + time_t now_time; + va_list args; + + va_start(args, format); + + if (logfile == NULL) + out = stderr; + + if (time(&now_time) == (time_t)-1) { + (void)fprintf(out, "LOGERROR - Failed to fetch time: "); + } else { + (void)localtime_r(&now_time, &now_tm); + if (strftime(timebuf, sizeof(timebuf), "%Y%m%d %H:%M:%S ", &now_tm) == 0) { + (void)fprintf(out, "LOGERROR - Failed to parse time (now: %d): ", + (int)now_time); + } else { + (void)fprintf(out, "%s", timebuf); + } + } + (void)fprintf(out, "[%d]: ", getpid()); + (void)vfprintf(out, format, args); + (void)fprintf(out, "\n"); + + fflush(out); + + va_end(args); +} + |
