Flowgrind
Advanced TCP traffic generator
debug.c
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2013 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 "debug.h"
30 
31 #ifdef DEBUG
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif /* HAVE_CONFIG_H */
36 
37 #include <stdio.h>
38 #include <time.h>
39 #include <string.h>
40 
41 #include "fg_time.h"
42 #include "fg_string.h"
43 
44 inline void decrease_debuglevel()
45 {
46  debug_level--;
47  printf("DEBUG_LEVEL=%u", debug_level);
48 }
49 
50 inline void increase_debuglevel()
51 {
52  debug_level++;
53  printf("DEBUG_LEVEL=%u\n", debug_level);
54 }
55 
56 int debug_timestamp(char **strp)
57 {
58  struct timespec now = {.tv_sec = 0, .tv_nsec = 0};
59  static struct timespec first = {.tv_sec = 0, .tv_nsec = 0};
60  static struct timespec last = {.tv_sec = 0, .tv_nsec = 0};
61 
62  gettime(&now);
63 
64  /* Save time of first call */
65  if (!first.tv_sec && !first.tv_nsec)
66  first = last = now;
67 
68  char timestamp[30] = "";
69  ctimespec_r(&now, timestamp, sizeof(timestamp), true);
70 
71  if (asprintf(strp, "%s [+%8.6lf] (%8.6lf)", timestamp,
72  time_diff(&last, &now), time_diff(&first, &now)) == -1)
73  return -1;
74 
75  last = now;
76  return 0;
77 }
78 
79 #endif /* DEBUG */
const char * ctimespec_r(const struct timespec *tp, char *buf, size_t size, bool ns)
Converts timespec struct tp into a null-terminated string.
Definition: fg_time.c:66
int gettime(struct timespec *tp)
Returns the current wall-clock time with nanosecond precision.
Definition: fg_time.c:145
Debugging routines for Flowgrind controller and daemon.
void decrease_debuglevel()
Decrease debug level.
Definition: debug.c:44
double time_diff(const struct timespec *tp1, const struct timespec *tp2)
Returns the time difference between two the specific points in time tp1 and tp2.
Definition: fg_time.c:95
void increase_debuglevel()
Decrease debug level.
Definition: debug.c:50
Functions to manipulate strings used by Flowgrind.
int debug_timestamp(char **strp)
Helper function for DEBUG_MSG macro.
Definition: debug.c:56
unsigned debug_level
Global debug level for flowgrind controller and daemon.
Definition: debug.h:60
Timing related routines used by Flowgrind.