File: log.c

package info (click to toggle)
arpalert 2.0.12-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 3,508 kB
  • sloc: ansic: 4,452; sh: 517; makefile: 116; perl: 35
file content (150 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download | duplicates (3)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (c) 2005-2010 Thierry FOURNIER
 * $Id: log.c 690 2008-03-31 18:36:43Z  $
 *
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef USE_SYSLOG
#include <syslog.h>
#endif

#include "arpalert.h"
#include "log.h"
#include "loadconfig.h"

extern int errno;

FILE *lf = NULL;
const char *mois[12] = {
	"Jan",
	"Feb",
	"Mar",
	"Apr",
	"May",
	"Jun",
	"Jul",
	"Aug",
	"Sep",
	"Oct",
	"Nov",
	"Dec"
};

int syslog_initialized = FALSE;
int file_initialized = FALSE;

// used for compose log
#define LOG_MAX_SIZE 512
char log_msg[LOG_MAX_SIZE];

void initlog(void){
	#ifdef USE_SYSLOG
	if(config[CF_USESYSLOG].valeur.integer == TRUE){
		openlog("arpalert", LOG_CONS, LOG_DAEMON);
		syslog_initialized = TRUE;
	}
	#endif
	if(config[CF_LOGFILE].valeur.string != NULL &&
	   config[CF_LOGFILE].valeur.string[0] != 0){
		lf = fopen(config[CF_LOGFILE].valeur.string, "a");
		if(lf == NULL){
			logmsg(LOG_ERR, "[%s %d] fopen[%d] (%s): %s",
			       __FILE__, __LINE__,
			       errno, config[CF_LOGFILE].valeur.string,
			       strerror(errno));
			exit(1);
		}
	}
	file_initialized = TRUE;
}

void logfile_reload(void) {
	int ret_code;

	// check if logfile is used
	if(config[CF_LOGFILE].valeur.string == NULL ||
	   config[CF_LOGFILE].valeur.string[0] == '\0') {
		return;
	}

	// if opened, close
	if (lf != NULL) {
		ret_code = fclose(lf);
		if (ret_code != 0) {
			logmsg(LOG_ERR, "[%s %d] fclose(%s): %s",
			       __FILE__, __LINE__,
			       config[CF_LOGFILE].valeur.string,
			       strerror(errno));
		}
	}

	// open logs
	lf = fopen(config[CF_LOGFILE].valeur.string, "a");
	if(lf == NULL){
		logmsg(LOG_ERR, "[%s %d] fopen[%d] (%s): %s",
		       __FILE__, __LINE__,
		       errno, config[CF_LOGFILE].valeur.string,
		       strerror(errno));
	}
}

void logmsg(int priority, const char *fmt, ...){
	va_list ap;
	struct tm *tm;

	// check if I do log this priority
	if(priority > config[CF_LOGLEVEL].valeur.integer){
		return;
	}

	//get current time
	tm = localtime((time_t *)(&current_t.tv_sec));

	va_start(ap, fmt);
	vsnprintf(log_msg, LOG_MAX_SIZE, fmt, ap);
	va_end(ap);

	#ifdef USE_SYSLOG
	if(config[CF_USESYSLOG].valeur.integer == TRUE &&
	   syslog_initialized == TRUE){
		syslog(priority, "%s", log_msg); 
	}
	#endif

	if(config[CF_LOGFILE].valeur.string != NULL &&
	   config[CF_LOGFILE].valeur.string[0] != 0 &&
	   file_initialized == TRUE){
		fprintf(lf, "%s % 2d %02d:%02d:%02d arpalert: %s\n",
		        mois[tm->tm_mon],
		        tm->tm_mday,
		        tm->tm_hour,
		        tm->tm_min,
		        tm->tm_sec, 
		        //for year: tm->tm_year+1900,
		        log_msg);
		fflush(lf);
	}

	if(is_forked == FALSE){
		printf("%s % 2d %02d:%02d:%02d arpalert: %s\n", 
		        mois[tm->tm_mon],
		        tm->tm_mday,
		        tm->tm_hour,
		        tm->tm_min,
		        tm->tm_sec, 
		        log_msg);
	}
}