File: stack_trace.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (89 lines) | stat: -rw-r--r-- 2,500 bytes parent folder | download | duplicates (6)
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
// Copyright (C) 2008  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_STACK_TRACE_CPp_
#define DLIB_STACK_TRACE_CPp_

#if defined(DLIB_ENABLE_STACK_TRACE) && !defined(NO_MAKEFILE)

#include <sstream>
#include <cstring>
#include "stack_trace.h"
#include "threads.h"
#include "stack.h"
#include "memory_manager.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    namespace 
    {
        struct stack_tracer_data
        {
            stack_tracer_data(
            ) :  funct_name(0),
                 file_name(0),
                 line_number(0)
            {}
            const char* funct_name;
            const char* file_name;
            int line_number;
        };

        thread_specific_data<stack<stack_tracer_data,memory_manager<char>::kernel_2a>::kernel_1a>& get_dlib_stack_trace_stack()
        {
            static thread_specific_data<stack<stack_tracer_data,memory_manager<char>::kernel_2a>::kernel_1a> a;
            return a;
        }
    }

// ----------------------------------------------------------------------------------------

    stack_tracer::
    stack_tracer (
        const char* funct_name,
        const char* file_name,
        const int line_number
    )
    {
        stack_tracer_data data;
        data.funct_name = funct_name;
        data.file_name = file_name;
        data.line_number = line_number;

        // pop the info onto the function stack trace
        get_dlib_stack_trace_stack().data().push(data);
    }

// ----------------------------------------------------------------------------------------

    stack_tracer::
    ~stack_tracer()
    {
        stack_tracer_data temp;
        get_dlib_stack_trace_stack().data().pop(temp);
    }

// ----------------------------------------------------------------------------------------

    const std::string get_stack_trace()
    {
        std::ostringstream sout;
        get_dlib_stack_trace_stack().data().reset();
        while (get_dlib_stack_trace_stack().data().move_next())
        {
            stack_tracer_data data = get_dlib_stack_trace_stack().data().element();
            sout << data.file_name << ":" << data.line_number << "\n    " << data.funct_name << "\n";
        }
        return sout.str();
    }

// ----------------------------------------------------------------------------------------

}
#endif

#endif // DLIB_STACK_TRACE_CPp_