dbg.h 1.76 KB
Newer Older
1 2 3 4 5 6
// From: http://c.learncodethehardway.org/book/ex20.html

#ifndef PRRT_DBG_H
#define PRRT_DBG_H

#include <stdio.h>
7
#include <stdint.h>
8 9 10
#include <errno.h>
#include <string.h>

11
#define DEBUG_ALL             0
Andreas Schmidt's avatar
Andreas Schmidt committed
12
#define DEBUG_BBR             1
13
#define DEBUG_BLOCK           0
Andreas Schmidt's avatar
Andreas Schmidt committed
14
#define DEBUG_PACKET          0
15
#define DEBUG_RECEIVER        0
16
#define DEBUG_SENDER          0
Andreas Schmidt's avatar
Andreas Schmidt committed
17 18
#define DEBUG_SOCKET          0
#define DEBUG_DATARECEIVER    1
Andreas Schmidt's avatar
Andreas Schmidt committed
19
#define DEBUG_CLEANUP         0
Andreas Schmidt's avatar
Andreas Schmidt committed
20
#define DEBUG_DATATRANSMITTER 0
Andreas Schmidt's avatar
Andreas Schmidt committed
21
#define DEBUG_HARDSTAMPING    0
22
#define DEBUG_FEEDBACK        0
Andreas Schmidt's avatar
Andreas Schmidt committed
23
#define DEBUG_TIMERECEIVER    1
Stefan Reif's avatar
Stefan Reif committed
24

25
#ifdef DEBUG
Stefan Reif's avatar
Stefan Reif committed
26
#define debug(DOMAIN, M, ...) do { if (DEBUG_ALL||(DOMAIN)) fprintf(stderr, "DEBUG %-20s %s:%d: " M "\n", #DOMAIN + 6, __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
27 28
#else
#define debug(DOMAIN, M, ...)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#endif

#define clean_errno() (errno == 0 ? "None" : strerror(errno))

#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }

#define sentinel(M, ...)  { log_err(M, ##__VA_ARGS__); errno=0; goto error; }

#define check_mem(A) check((A), "Out of memory.")

#define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }

47 48 49 50 51 52 53 54
// Source: http://stackoverflow.com/questions/3898107/how-does-this-code-calculate-the-number-of-cpu-cycles-elapsed
static __inline__ uint64_t rdtsc(void)
{
  uint64_t x;
  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
  return x;
}

55
#endif //PRRT_DBG_H