aboutsummaryrefslogtreecommitdiff
path: root/src/log.c
blob: 9cb66433176e5f5df35022ad0d4565d7dbeff72a (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <syslog.h>

#include "log.h"

#define LOG_BUFSIZ 128

static FILE* logfile = NULL;


int log_init(char* file)
{
  if (!file) {
    openlog("naskpass", LOG_NDELAY | LOG_PID, LOG_DAEMON);
    return 0;
  } else {
    logfile = fopen(file, "a+");
    return (logfile ? 0 : errno);
  }
}

void log_free(void)
{
  if (!logfile) {
    closelog();
  } else {
    fclose(logfile);
    logfile = NULL;
  }
}

int logs(const char* format, ...)
{
  int ret;
  va_list vargs;

  va_start(vargs, format);
  if (!logfile) {
    vsyslog(LOG_DEBUG, format, vargs);
    ret = 0;
  } else {
    ret = vfprintf(logfile, format, vargs);
    fflush(logfile);
  }
  va_end(vargs);
  return ret;
}