aboutsummaryrefslogtreecommitdiffstats
path: root/src/log.c
blob: c1d4a986131c9f6efb46380402f6ececd9ac7e6f (plain)
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
/*
 * 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, timebuf);
    }
  }
  (void)fprintf(out, "[%d]: ", getpid());
  (void)vfprintf(out, format, args);
  (void)fprintf(out, "\n");

  fflush(out);

  va_end(args);
}