Flowgrind
Advanced TCP traffic generator
debug.h File Reference

Debugging routines for Flowgrind controller and daemon. More...

#include "config.h"
#include <limits.h>
#include <pthread.h>

Go to the source code of this file.

Macros

#define DEBUG_MSG(LVL, MSG, ...)
 Print debug message to standard error. More...
 

Functions

int debug_timestamp (char **resultp)
 Helper function for DEBUG_MSG macro. More...
 
void decrease_debuglevel (void)
 Decrease debug level. More...
 
void increase_debuglevel (void)
 Decrease debug level. More...
 

Variables

unsigned debug_level
 Global debug level for flowgrind controller and daemon. More...
 

Detailed Description

Debugging routines for Flowgrind controller and daemon.

Definition in file debug.h.

Macro Definition Documentation

#define DEBUG_MSG (   LVL,
  MSG,
  ... 
)
Value:
do { \
char *timestamp = NULL; \
debug_timestamp(&timestamp); \
if (debug_level >= LVL) \
fprintf(stderr, "%s %s:%d [%d/%d] " MSG "\n", \
timestamp, __FUNCTION__, __LINE__, getpid(), \
(unsigned)pthread_self()%USHRT_MAX, ##__VA_ARGS__); \
free(timestamp); \
} while (0)
int debug_timestamp(char **resultp)
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

Print debug message to standard error.

If the debug level is higher than the given debug level LVL, print debug message MSG together with current time, the delta in time since the last and first debug call, the function in which the debug call occurs, and the process and thread PID.

Definition at line 49 of file debug.h.

Function Documentation

int debug_timestamp ( char **  resultp)

Helper function for DEBUG_MSG macro.

Write string with the current time in seconds and nanoseconds since the Epoch together with the delta in time since the last and first call the function.

Parameters
[in,out]resultpdestination string to write to
Returns
return 0 for success, or -1 for failure

Definition at line 56 of file debug.c.

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 }
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
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 decrease_debuglevel ( void  )
inline

Decrease debug level.

Definition at line 44 of file debug.c.

45 {
46  debug_level--;
47  printf("DEBUG_LEVEL=%u", debug_level);
48 }
unsigned debug_level
Global debug level for flowgrind controller and daemon.
Definition: debug.h:60
void increase_debuglevel ( void  )
inline

Decrease debug level.

Definition at line 50 of file debug.c.

51 {
52  debug_level++;
53  printf("DEBUG_LEVEL=%u\n", debug_level);
54 }
unsigned debug_level
Global debug level for flowgrind controller and daemon.
Definition: debug.h:60

Variable Documentation

unsigned debug_level

Global debug level for flowgrind controller and daemon.

Definition at line 60 of file debug.h.