aboutsummaryrefslogtreecommitdiffstats
path: root/c_src/log.c
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2012-02-09 14:13:57 -0500
committerBrian Cully <bjc@kublai.com>2012-02-09 14:13:57 -0500
commit8cd5a7f161743d6b6a995b92b1702909809b8cdc (patch)
tree5a12ddc4b5f0f5a8d70d126cbdc52451e55bdac9 /c_src/log.c
parenta5424fc5ebde59b8335e99a062e208a80235977f (diff)
downloadmysqlerl-8cd5a7f161743d6b6a995b92b1702909809b8cdc.tar.gz
mysqlerl-8cd5a7f161743d6b6a995b92b1702909809b8cdc.zip
Move C code into c_src
Diffstat (limited to 'c_src/log.c')
-rw-r--r--c_src/log.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/c_src/log.c b/c_src/log.c
new file mode 100644
index 0000000..80aa755
--- /dev/null
+++ b/c_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);
+}
+