Flowgrind
Advanced TCP traffic generator
fg_log.c
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2014 Alexander Zimmermann <alexander.zimmermann@netapp.com>
8  * Copyright (C) 2010-2013 Christian Samsel <christian.samsel@rwth-aachen.de>
9  * Copyright (C) 2009 Tim Kosse <tim.kosse@gmx.de>
10  * Copyright (C) 2007-2008 Daniel Schaffrath <daniel.schaffrath@mac.com>
11  *
12  * This file is part of Flowgrind.
13  *
14  * Flowgrind 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  * Flowgrind 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 Flowgrind. If not, see <http://www.gnu.org/licenses/>.
26  *
27  */
28 
29 #include <syslog.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdbool.h>
34 #include <string.h>
35 #include <time.h>
36 
37 #include "fg_log.h"
38 #include "fg_time.h"
39 #include "fg_error.h"
40 
41 static enum log_streams log_stream = LOG_SYSLOG;
42 
43 void init_logging(enum log_streams stream)
44 {
45  log_stream = stream;
46 
47  switch (log_stream) {
48  case LOGGING_SYSLOG:
49  openlog("flowgrindd", LOG_NDELAY | LOG_CONS | LOG_PID, LOG_DAEMON);
50  break;
51  case LOGGING_STDERR:
52  case LOGGING_STDOUT:
53  break;
54  }
55 }
56 
57 void close_logging(void)
58 {
59  switch (log_stream) {
60  case LOGGING_SYSLOG:
61  closelog();
62  break;
63  case LOGGING_STDERR:
64  case LOGGING_STDOUT:
65  break;
66  }
67 }
68 
69 void logging(int priority, const char *fmt, ...)
70 {
71  va_list ap;
72 
73  va_start(ap, fmt);
74  vlogging(priority, fmt, ap);
75  va_end(ap);
76 }
77 
78 void vlogging(int priority, const char *fmt, va_list ap)
79 {
80  char timestamp[30] = "";
81  ctimenow_r(timestamp, sizeof(timestamp), false);
82 
83  switch (log_stream) {
84  case LOGGING_SYSLOG:
85  vsyslog(priority, fmt, ap);
86  break;
87  case LOGGING_STDERR:
88  fprintf(stderr, "%s ", timestamp);
89  vfprintf(stderr, fmt, ap);
90  fprintf(stderr, "\n");
91  fflush(stderr);
92  break;
93  case LOGGING_STDOUT:
94  fprintf(stdout, "%s ", timestamp);
95  vfprintf(stdout, fmt, ap);
96  fprintf(stdout, "\n");
97  fflush(stdout);
98  break;
99  }
100 
101 }
Log to stderr.
Definition: fg_log.h:41
const char * ctimenow_r(char *buf, size_t size, bool ns)
Returns the current wall-clock time as null-terminated string.
Definition: fg_time.c:47
void vlogging(int priority, const char *fmt, va_list ap)
Definition: fg_log.c:78
void init_logging(enum log_streams stream)
Open logging stream.
Definition: fg_log.c:43
Log to syslog.
Definition: fg_log.h:39
void logging(int priority, const char *fmt,...)
Definition: fg_log.c:69
Log to stdout.
Definition: fg_log.h:43
Error-reporting routines used by Flowgrind.
log_streams
Supported output streams for logging.
Definition: fg_log.h:37
static enum log_streams log_stream
Definition: fg_log.c:41
void close_logging(void)
Close logging stream.
Definition: fg_log.c:57
Timing related routines used by Flowgrind.