Skip to Content
The Cooperative Association for Internet Data Analysis
DONATE
CONTACT US
HOME
RESEARCH
DATA
TOOLS
PUBLICATIONS
WORKSHOPS
PROJECTS
FUNDING
www.caida.org
>
tools
:
measurement
:
corsaro
: docs
corsaro_log.c
1
/*
2
* corsaro
3
*
4
* Alistair King, CAIDA, UC San Diego
5
* corsaro-info@caida.org
6
*
7
* corsaro_log and timestamp_str functions adapted from scamper:
8
* http://www.wand.net.nz/scamper
9
*
10
* Copyright (C) 2012 The Regents of the University of California.
11
*
12
* This file is part of corsaro.
13
*
14
* corsaro is free software: you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation, either version 3 of the License, or
17
* (at your option) any later version.
18
*
19
* corsaro is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with corsaro. If not, see <http://www.gnu.org/licenses/>.
26
*
27
*/
28
29
#include "config.h"
30
#include "
corsaro_int.h
"
31
32
#include <assert.h>
33
#include <stdarg.h>
34
#include <inttypes.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
39
#ifdef HAVE_SYS_TIME_H
40
#include <time.h>
41
#endif
42
#ifdef HAVE_TIME_H
43
#include <sys/time.h>
44
#endif
45
46
#include "
corsaro_file.h
"
47
#include "
corsaro_io.h
"
48
#include "
utils.h
"
49
50
#include "
corsaro_log.h
"
51
52
static
char
*timestamp_str(
char
*buf,
const
size_t
len)
53
{
54
struct
timeval tv;
55
struct
tm *tm;
56
int
ms;
57
time_t t;
58
59
buf[0] =
'\0'
;
60
gettimeofday_wrap(&tv);
61
t = tv.tv_sec;
62
if
((tm = localtime(&t)) == NULL)
return
buf;
63
64
ms = tv.tv_usec / 1000;
65
snprintf(buf, len,
"[%02d:%02d:%02d:%03d] "
,
66
tm->tm_hour, tm->tm_min, tm->tm_sec, ms);
67
68
return
buf;
69
}
70
71
void
generic_log(
const
char
*func,
corsaro_file_t
*logfile,
const
char
*format,
72
va_list ap)
73
{
74
char
message[512];
75
char
ts[16];
76
char
fs[64];
77
78
assert(format != NULL);
79
80
vsnprintf(message,
sizeof
(message), format, ap);
81
82
timestamp_str(ts,
sizeof
(ts));
83
84
if
(func != NULL) snprintf(fs,
sizeof
(fs),
"%s: "
, func);
85
else
fs[0] =
'\0'
;
86
87
if
(logfile == NULL)
88
{
89
fprintf(stderr,
"%s%s%s\n"
, ts, fs, message);
90
fflush(stderr);
91
}
92
else
93
{
94
/* we're special. we know that corsaro_file_printf can do without corsaro */
95
corsaro_file_printf(NULL, logfile,
"%s%s%s\n"
, ts, fs, message);
96
corsaro_file_flush(NULL, logfile);
97
98
#ifdef DEBUG
99
/* we've been asked to dump debugging information */
100
fprintf(stderr,
"%s%s%s\n"
, ts, fs, message);
101
fflush(stderr);
102
#endif
103
}
104
}
105
106
void
corsaro_log_va(
const
char
*func,
corsaro_t
*
corsaro
,
107
const
char
*format, va_list args)
108
{
109
corsaro_file_t
*lf = (corsaro == NULL) ? NULL : corsaro->
logfile
;
110
generic_log(func, lf, format, args);
111
}
112
113
void
corsaro_log(
const
char
*func,
corsaro_t
*
corsaro
,
const
char
*format, ...)
114
{
115
va_list ap;
116
va_start(ap, format);
117
corsaro_log_va(func, corsaro, format, ap);
118
va_end(ap);
119
}
120
121
void
corsaro_log_in(
const
char
*func,
corsaro_in_t
*
corsaro
,
const
char
*format, ...)
122
{
123
#ifdef DEBUG
124
va_list ap;
125
va_start(ap, format);
126
generic_log(func, NULL, format, ap);
127
va_end(ap);
128
#endif
129
}
130
131
void
corsaro_log_file(
const
char
*func,
corsaro_file_t
*logfile,
132
const
char
*format, ...)
133
{
134
va_list ap;
135
va_start(ap, format);
136
generic_log(func, logfile, format, ap);
137
va_end(ap);
138
}
139
140
int
corsaro_log_init(
corsaro_t
*
corsaro
)
141
{
142
if
((corsaro->
logfile
= corsaro_io_prepare_file_full(corsaro,
CORSARO_IO_LOG_NAME
,
143
CORSARO_FILE_MODE_ASCII
,
144
CORSARO_FILE_COMPRESS_NONE
,
145
0, O_CREAT)) == NULL)
146
{
147
fprintf(stderr,
"could not open log for writing"
);
148
return
-1;
149
}
150
return
0;
151
}
152
153
int
corsaro_log_in_init(
corsaro_in_t
*
corsaro
)
154
{
155
/* nothing to do, corsaro_log_in only logs to stderr, and iff --enable-debug
156
is passed to configure */
157
return
0;
158
}
159
160
void
corsaro_log_close(
corsaro_t
*
corsaro
)
161
{
162
if
(corsaro->
logfile
!= NULL)
163
{
164
corsaro_file_close(NULL, corsaro->
logfile
);
165
corsaro->
logfile
= NULL;
166
}
167
}
168
169
void
corsaro_log_in_close(
corsaro_in_t
*
corsaro
)
170
{
171
/* nothing to be done */
172
}
173
174