-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cc
166 lines (137 loc) · 4.13 KB
/
log.cc
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2009-2011, Adrian Thurston <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsnp.h"
#include "error.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#define FATAL_EXIT_CODE 1
#define TIME_STR_SZ 64
/* FIXME: should do something about this. */
static ConfigSection *c = 0;
int openLogFile( const char *fn )
{
int fd = open( fn,
O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR );
if ( fd < 0 ) {
/* This print will only be effective in some situations, when std error
* is still avaialable. */
fprintf( stderr, "ERROR: could not open log file %s: %s\n", fn, strerror(errno) );
exit( 1 );
}
return fd;
}
void setLogFd( int fd )
{
if ( gbl.logFile != 0 )
fclose( gbl.logFile );
gbl.logFile = fdopen( fd, "at" );
}
void logToConsole( int fd )
{
gbl.logFile = fdopen( fd, "at" );
if ( gbl.logFile == 0 ) {
fprintf( stderr, "ERROR: could not open FILE wrapping fd %d\n", fd );
exit( 1 );
}
}
void fatal( const char *fmt, ... )
{
va_list args;
struct tm localTm;
char timeStr[TIME_STR_SZ];
assert( gbl.logFile != 0 );
time_t t = time(0);
localtime_r( &t, &localTm );
strftime( timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &localTm );
va_start( args, fmt );
fprintf( gbl.logFile, "%-5d %s %s FATAL: ", gbl.pid, timeStr, c != 0 ? c->name() : "-" );
vfprintf( gbl.logFile, fmt, args );
va_end( args );
fflush( gbl.logFile );
exit(1);
}
void error( const char *fmt, ... )
{
va_list args;
struct tm localTm;
char timeStr[TIME_STR_SZ];
assert( gbl.logFile != 0 );
time_t t = time(0);
localtime_r( &t, &localTm );
strftime( timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &localTm );
va_start( args, fmt );
fprintf( gbl.logFile, "%-5d %s %s ERROR: ", gbl.pid, timeStr, c != 0 ? c->name() : "-" );
vfprintf( gbl.logFile, fmt, args );
va_end( args );
fflush( gbl.logFile );
}
void warning( const char *fmt, ... )
{
va_list args;
struct tm localTm;
char timeStr[TIME_STR_SZ];
assert( gbl.logFile != 0 );
time_t t = time(0);
localtime_r( &t, &localTm );
strftime( timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &localTm );
va_start( args, fmt );
fprintf( gbl.logFile, "%-5d %s %s WARNING: ", gbl.pid, timeStr, c != 0 ? c->name() : "-" );
vfprintf( gbl.logFile, fmt, args );
va_end( args );
fflush( gbl.logFile );
}
void message( const char *fmt, ... )
{
va_list args;
struct tm localTm;
char timeStr[TIME_STR_SZ];
assert( gbl.logFile != 0 );
time_t t = time(0);
localtime_r( &t, &localTm );
strftime( timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &localTm );
va_start( args, fmt );
fprintf( gbl.logFile, "%-5d %s %s message: ", gbl.pid, timeStr, c != 0 ? c->name() : "-" );
vfprintf( gbl.logFile, fmt, args );
va_end( args );
fflush( gbl.logFile );
}
void _dsnpd_debug( long realm, const char *fmt, ... )
{
va_list args;
struct tm localTm;
char timeStr[TIME_STR_SZ];
assert( gbl.logFile != 0 );
/* Ensure the realm specified in the debug statement is enabled. */
if ( ! ( realm & gbl.enabledRealms ) )
return;
time_t t = time(0);
localtime_r( &t, &localTm );
strftime( timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &localTm );
va_start( args, fmt );
fprintf( gbl.logFile, "%-5d %s %s debug: ", gbl.pid, timeStr, c != 0 ? c->name() : "-" );
vfprintf( gbl.logFile, fmt, args );
va_end( args );
fflush( gbl.logFile );
}