1 |
michael |
5052 |
/* |
2 |
|
|
Copyright (C) 2002 Erik Fears |
3 |
|
|
|
4 |
|
|
This program is free software; you can redistribute it and/or |
5 |
|
|
modify it under the terms of the GNU General Public License |
6 |
|
|
as published by the Free Software Foundation; either version 2 |
7 |
|
|
of the License, or (at your option) any later version. |
8 |
|
|
|
9 |
|
|
This program is distributed in the hope that it will be useful, |
10 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
|
|
GNU General Public License for more details. |
13 |
|
|
|
14 |
|
|
You should have received a copy of the GNU General Public License |
15 |
|
|
along with this program; if not, write to |
16 |
|
|
|
17 |
|
|
The Free Software Foundation, Inc. |
18 |
|
|
59 Temple Place - Suite 330 |
19 |
|
|
Boston, MA 02111-1307, USA. |
20 |
|
|
|
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#include "setup.h" |
24 |
|
|
|
25 |
|
|
#include <stdio.h> |
26 |
|
|
#include <errno.h> |
27 |
|
|
#include <stdlib.h> |
28 |
|
|
#include <stdarg.h> |
29 |
|
|
#include <string.h> |
30 |
|
|
#include <time.h> |
31 |
|
|
|
32 |
|
|
#include "compat.h" |
33 |
|
|
#include "config.h" |
34 |
|
|
#include "log.h" |
35 |
michael |
5207 |
#include "main.h" |
36 |
michael |
5052 |
|
37 |
|
|
|
38 |
|
|
FILE *logfile; |
39 |
|
|
FILE *scanlogfile; |
40 |
|
|
|
41 |
|
|
void log_open(char *filename) |
42 |
|
|
{ |
43 |
|
|
logfile = fopen(filename, "a"); |
44 |
|
|
|
45 |
|
|
if(!logfile) |
46 |
|
|
{ |
47 |
|
|
perror("Cannot open log file. Aborting."); |
48 |
|
|
exit(EXIT_FAILURE); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
void log_close(void) |
54 |
|
|
{ |
55 |
|
|
fclose(logfile); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
void scanlog_open(char *filename) |
59 |
|
|
{ |
60 |
|
|
scanlogfile = fopen(filename, "a"); |
61 |
|
|
|
62 |
|
|
if(!scanlogfile) |
63 |
|
|
{ |
64 |
|
|
log_printf("Failed to open scan log file: %s", strerror(errno)); |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
void scanlog_close(void) |
69 |
|
|
{ |
70 |
|
|
if(scanlogfile) |
71 |
|
|
fclose(scanlogfile); |
72 |
|
|
} |
73 |
|
|
|
74 |
michael |
5072 |
void log_printf(const char *data, ...) |
75 |
michael |
5052 |
{ |
76 |
|
|
char data2[513]; |
77 |
|
|
char buf_present[25]; |
78 |
|
|
va_list arglist; |
79 |
|
|
time_t present; |
80 |
|
|
struct tm *tm_present; |
81 |
|
|
|
82 |
|
|
if(!OPT_DEBUG && !logfile) |
83 |
|
|
return; |
84 |
|
|
|
85 |
|
|
time(&present); |
86 |
|
|
tm_present = gmtime(&present); |
87 |
|
|
strftime(buf_present, sizeof(buf_present), "%b %d %H:%M:%S %Y", tm_present); |
88 |
|
|
|
89 |
|
|
va_start(arglist, data); |
90 |
|
|
vsnprintf(data2, 512, data, arglist); |
91 |
|
|
va_end(arglist); |
92 |
|
|
|
93 |
|
|
if(OPT_DEBUG) |
94 |
|
|
{ |
95 |
|
|
fprintf(stderr, "[%s] %s\n", buf_present, data2); |
96 |
|
|
} |
97 |
|
|
else |
98 |
|
|
{ |
99 |
|
|
fprintf(logfile, "[%s] %s\n", buf_present, data2); |
100 |
|
|
fflush(logfile); |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
|