1#include "log.h"
2#include "sntp-opts.h"
3
4int init = 0;
5int filelog = 0;
6
7FILE *log_file;
8
9
10void log_msg(char *message, char type) {
11	if(init) {
12		time_t cur_time = time(NULL);
13		char *timestamp = ctime(&cur_time);
14
15		fprintf(log_file, "%s: %s\n", timestamp, message);
16		fflush(log_file);
17	}
18	else {
19		switch(type) {
20			case 0:
21				type = LOG_CONS;
22				break;
23
24			case 1:
25				type = LOG_DEBUG | LOG_CONS;
26				break;
27
28			case 2:
29				type = LOG_WARNING | LOG_CONS;
30				break;
31		}
32
33		syslog(type, "%s", message);
34	}
35}
36
37void debug_msg(char *message) {
38	if(HAVE_OPT(FILELOG)) {
39		time_t cur_time = time(NULL);
40		char *timestamp = ctime(&cur_time);
41
42		fprintf(stderr, "%s: %s\n", timestamp, message);
43	}
44	else {
45		syslog(LOG_DEBUG
46#ifdef LOG_PERROR
47			| LOG_PERROR
48#endif
49			| LOG_CONS, "%s", message);
50	}
51}
52
53void init_log(
54	const char *logfile
55	)
56{
57	char error_msg[80];
58
59	log_file = fopen(logfile, "a");
60	if (log_file == NULL) {
61		filelog = 0;
62
63		snprintf(error_msg, 80, "init_log(): Cannot open logfile %s", logfile);
64		debug_msg(error_msg);
65
66		return;
67	} else {
68		filelog = 1;
69		init = 1;
70		atexit(cleanup_log);
71	}
72}
73
74void cleanup_log(void) {
75	init = 0;
76	fflush(log_file);
77	fclose(log_file);
78}
79