forked from rethinkdb/rethinkdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.hpp
More file actions
95 lines (81 loc) · 2.67 KB
/
Copy pathdebug.hpp
File metadata and controls
95 lines (81 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2010-2015 RethinkDB, all rights reserved.
#ifndef DEBUG_HPP_
#define DEBUG_HPP_
#include <string>
#include "containers/printf_buffer.hpp"
#include "time.hpp"
#ifndef NDEBUG
#define trace_call(fn, ...) do { \
debugf("%s:%u: %s: entered\n", __FILE__, __LINE__, stringify(fn)); \
fn(__VA_ARGS__); \
debugf("%s:%u: %s: returned\n", __FILE__, __LINE__, stringify(fn)); \
} while (0)
#define TRACEPOINT debugf("%s:%u reached\n", __FILE__, __LINE__)
#else
#define trace_call(fn, ...) fn(__VA_ARGS__)
// TRACEPOINT is not defined in release, so that TRACEPOINTS do not linger in the code unnecessarily
#endif
// HEY: Maybe debugf and log_call and TRACEPOINT should be placed in
// debugf.hpp (and debugf.cc).
/* Debugging printing API (prints current thread in addition to message) */
void debug_print_quoted_string(printf_buffer_t *buf, const uint8_t *s, size_t n);
void debugf_prefix_buf(printf_buffer_t *buf);
void debugf_dump_buf(printf_buffer_t *buf);
// Primitive debug_print declarations.
template <class T>
typename std::enable_if<std::is_arithmetic<T>::value>::type
debug_print(printf_buffer_t *buf, T x) {
debug_print(buf, std::to_string(x));
}
void debug_print(printf_buffer_t *buf, const std::string& s);
template <class T>
void debug_print(printf_buffer_t *buf, T *ptr) {
buf->appendf("%p", ptr);
}
template<class T>
std::string debug_str(const T &t) {
printf_buffer_t buf;
debug_print(&buf, t);
return buf.c_str();
}
#ifndef NDEBUG
void debugf(const char *msg, ...) ATTR_FORMAT(printf, 1, 2);
template <class T>
void debugf_print(const char *msg, const T &obj) {
printf_buffer_t buf;
debugf_prefix_buf(&buf);
buf.appendf("%s: ", msg);
debug_print(&buf, obj);
buf.appendf("\n");
debugf_dump_buf(&buf);
}
#else
#define debugf(...) ((void)0)
#define debugf_print(...) ((void)0)
#endif // NDEBUG
template <class T>
std::string debug_strprint(const T &obj) {
printf_buffer_t buf;
debug_print(&buf, obj);
return std::string(buf.data(), buf.size());
}
class debugf_in_dtor_t {
public:
explicit debugf_in_dtor_t(const char *msg, ...) ATTR_FORMAT(printf, 2, 3);
~debugf_in_dtor_t();
private:
std::string message;
};
// TODO: make this more efficient (use `clock_monotonic` and use a vector of
// integers rather than accumulating a string).
class debug_timer_t {
public:
explicit debug_timer_t(std::string _name = "");
~debug_timer_t();
microtime_t tick(const std::string &tag);
private:
microtime_t start, last;
std::string name, out;
DISABLE_COPYING(debug_timer_t);
};
#endif // DEBUG_HPP_