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