blob: aacbeb7826ce2cc39108fe0ebcf3da1afb1ac157 (
plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
// Copyright (C) 2008 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_STACK_TRACe_
#define DLIB_STACK_TRACe_
/*!
This file defines 3 things. Two of them are preprocessor macros that
enable you to tag functions with the dlib stack trace watcher. The
third thing is a function named get_stack_trace() which returns the
current stack trace in std::string form.
To enable the stack trace you must #define DLIB_ENABLE_STACK_TRACE.
When this #define isn't set then the 3 things described above
still exist but they don't do anything.
Also note that when the stack trace is enabled it changes the DLIB_ASSERT
and DLIB_CASSERT macros so that they print stack traces when
an assert fails.
See the following example program for details:
#include <iostream>
#include <dlib/stack_trace.h>
void funct2()
{
// put this macro at the top of each function you would
// like to appear in stack traces
DLIB_STACK_TRACE;
// you may print the current stack trace as follows.
std::cout << dlib::get_stack_trace() << endl;
}
void funct()
{
// This alternate form of DLIB_STACK_TRACE allows you to specify
// the string used to name the current function. The other form
// will usually output an appropriate function name automatically
// so this may not be needed.
DLIB_STACK_TRACE_NAMED("funct");
funct2();
}
int main()
{
funct();
}
!*/
#include <string>
#include "assert.h"
// only setup the stack trace stuff if the asserts are enabled (which happens in debug mode
// basically). Also, this stuff doesn't work if you use NO_MAKEFILE
#if defined(DLIB_ENABLE_STACK_TRACE)
#ifdef NO_MAKEFILE
#error "You can't use the dlib stack trace stuff and NO_MAKEFILE at the same time"
#endif
namespace dlib
{
const std::string get_stack_trace();
}
// redefine the DLIB_CASSERT macro to include the stack trace
#undef DLIBM_CASSERT
#define DLIBM_CASSERT(_exp,_message) \
{if ( !(_exp) ) \
{ \
std::ostringstream dlib_o_out; \
dlib_o_out << "\n\nError occurred at line " << __LINE__ << ".\n"; \
dlib_o_out << "Error occurred in file " << __FILE__ << ".\n"; \
dlib_o_out << "Error occurred in function " << DLIB_FUNCTION_NAME << ".\n\n"; \
dlib_o_out << "Failing expression was " << #_exp << ".\n"; \
dlib_o_out << _message << "\n\n"; \
dlib_o_out << "Stack Trace: \n" << dlib::get_stack_trace() << "\n"; \
dlib_assert_breakpoint(); \
throw dlib::fatal_error(dlib::EBROKEN_ASSERT,dlib_o_out.str()); \
}}
namespace dlib
{
class stack_tracer
{
public:
stack_tracer (
const char* funct_name,
const char* file_name,
const int line_number
);
~stack_tracer();
};
}
#define DLIB_STACK_TRACE_NAMED(x) dlib::stack_tracer dlib_stack_tracer_object(x,__FILE__,__LINE__)
#define DLIB_STACK_TRACE dlib::stack_tracer dlib_stack_tracer_object(DLIB_FUNCTION_NAME,__FILE__,__LINE__)
#else // don't do anything if ENABLE_ASSERTS isn't defined
#define DLIB_STACK_TRACE_NAMED(x)
#define DLIB_STACK_TRACE
namespace dlib
{
inline const std::string get_stack_trace() { return std::string("stack trace not enabled");}
}
#endif
#endif // DLIB_STACK_TRACe_
|