Skip to Content
[CAIDA - Center for Applied Internet Data Analysis logo]
The Center for Applied Internet Data Analysis
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 <sys/time.h>
41 #endif
42 #ifdef HAVE_TIME_H
43 #include <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,
122  const char *format, ...)
123 {
124 #ifdef DEBUG
125  va_list ap;
126  va_start(ap, format);
127  generic_log(func, NULL, format, ap);
128  va_end(ap);
129 #endif
130 }
131 
132 void corsaro_log_file(const char *func, corsaro_file_t *logfile,
133  const char *format, ...)
134 {
135  va_list ap;
136  va_start(ap, format);
137  generic_log(func, logfile, format, ap);
138  va_end(ap);
139 }
140 
142 {
143  if((corsaro->logfile = corsaro_io_prepare_file_full(corsaro,
145  &corsaro->interval_start,
148  0, O_CREAT)) == NULL)
149  {
150  fprintf(stderr, "could not open log for writing\n");
151  return -1;
152  }
153  return 0;
154 }
155 
157 {
158  /* nothing to do, corsaro_log_in only logs to stderr, and iff --enable-debug
159  is passed to configure */
160  return 0;
161 }
162 
164 {
165  if(corsaro->logfile != NULL)
166  {
167  corsaro_file_close(NULL, corsaro->logfile);
168  corsaro->logfile = NULL;
169  }
170 }
171 
173 {
174  /* nothing to be done */
175 }
176 
177 
Header file dealing with the corsaro logging sub-system.
An opaque structure defining an corsaro output file.
Definition: corsaro_file.h:60
off_t corsaro_file_printf(struct corsaro *corsaro, corsaro_file_t *file, const char *format,...)
Print a string to an corsaro file.
Definition: corsaro_file.c:214
ASCII IO mode.
Definition: corsaro.h:153
void corsaro_log_va(const char *func, corsaro_t *corsaro, const char *format, va_list args)
Write a formatted string to the logfile associated with an corsaro object.
Definition: corsaro_log.c:106
corsaro_file_t * corsaro_io_prepare_file_full(corsaro_t *corsaro, const char *plugin_name, corsaro_interval_t *interval, corsaro_file_mode_t mode, corsaro_file_compress_t compress, int compress_level, int flags)
Uses the given settings to open an corsaro file for the given plugin.
Definition: corsaro_io.c:699
void corsaro_file_close(struct corsaro *corsaro, corsaro_file_t *file)
Closes an corsaro output file and frees the writer structure.
Definition: corsaro_file.c:230
Header file for common utility functions.
#define CORSARO_IO_LOG_NAME
The name to use for the log 'plugin' file.
Definition: corsaro_io.h:68
void corsaro_log_close(corsaro_t *corsaro)
Close the log file for an corsaro output object.
Definition: corsaro_log.c:163
void corsaro_file_flush(struct corsaro *corsaro, corsaro_file_t *file)
Force all buffered data for the file to be written out.
Definition: corsaro_file.c:224
Header file dealing with the low-level file IO.
void corsaro_log_in(const char *func, corsaro_in_t *corsaro, const char *format,...)
Write a formatted string to the logfile associated with an corsaro input object.
Definition: corsaro_log.c:121
int corsaro_log_init(corsaro_t *corsaro)
Initialize the logging sub-system for an corsaro output object.
Definition: corsaro_log.c:141
corsaro_interval_t interval_start
State for the current interval.
Definition: corsaro_int.h:292
void corsaro_log_in_close(corsaro_in_t *corsaro)
Close the log file for an corsaro input object.
Definition: corsaro_log.c:172
void corsaro_log_file(const char *func, corsaro_file_t *logfile, const char *format,...)
Write a formatted string to a generic log file.
Definition: corsaro_log.c:132
Corsaro input state.
Definition: corsaro_int.h:323
Header file dealing with the corsaro file IO.
corsaro_file_t * logfile
The file to write log output to.
Definition: corsaro_int.h:263
void gettimeofday_wrap(struct timeval *tv)
Convenience function to get the current time of day.
Definition: utils.c:71
Corsaro output state.
Definition: corsaro_int.h:230
No compression.
Definition: corsaro.h:169
int corsaro_log_in_init(corsaro_in_t *corsaro)
Initialize the logging sub-system for an corsaro input object.
Definition: corsaro_log.c:156
void corsaro_log(const char *func, corsaro_t *corsaro, const char *format,...)
Write a formatted string to the logfile associated with an corsaro object.
Definition: corsaro_log.c:113
Header file dealing with the internal corsaro functions.