mirror of
https://github.com/davisking/dlib.git
synced 2024-11-01 10:14:53 +08:00
Made the stack trace stuff more robust
--HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402236
This commit is contained in:
parent
44776dbbd4
commit
23b3a722df
@ -41,6 +41,7 @@
|
||||
#include "../threads/threads_kernel_2.cpp"
|
||||
#include "../threads/threads_kernel_shared.cpp"
|
||||
#include "../timer/timer_kernel_2.cpp"
|
||||
#include "../stack_trace.cpp"
|
||||
|
||||
#ifdef DLIB_PNG_SUPPORT
|
||||
#include "../image_loader/png_loader.cpp"
|
||||
|
@ -106,5 +106,7 @@ extern "C"
|
||||
|
||||
// -----------------------------
|
||||
|
||||
#include "stack_trace.h"
|
||||
|
||||
#endif // DLIB_ASSERt_
|
||||
|
||||
|
75
dlib/stack_trace.cpp
Normal file
75
dlib/stack_trace.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright (C) 2008 Davis E. King (davisking@users.sourceforge.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"
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
thread_specific_data<stack<std::string>::kernel_1a>& get_dlib_stack_trace_stack()
|
||||
{
|
||||
static thread_specific_data<stack<std::string>::kernel_1a> a;
|
||||
return a;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
stack_tracer::
|
||||
stack_tracer (
|
||||
const char* funct_name,
|
||||
const char* file_name,
|
||||
const int line_number
|
||||
)
|
||||
{
|
||||
std::ostringstream sout;
|
||||
// if the function name isn't really long then put this all on a single line
|
||||
if (std::strlen(funct_name) < 40)
|
||||
sout << file_name << ":" << line_number << ": " << funct_name;
|
||||
else
|
||||
sout << file_name << ":" << line_number << "\n" << funct_name;
|
||||
|
||||
// pop the string onto the function stack trace
|
||||
std::string temp(sout.str());
|
||||
get_dlib_stack_trace_stack().data().push(temp);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
stack_tracer::
|
||||
~stack_tracer()
|
||||
{
|
||||
std::string 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())
|
||||
{
|
||||
sout << get_dlib_stack_trace_stack().data().element() << "\n";
|
||||
}
|
||||
return sout.str();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // DLIB_STACK_TRACE_CPp_
|
||||
|
||||
|
@ -49,18 +49,16 @@
|
||||
!*/
|
||||
|
||||
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include "assert.h"
|
||||
|
||||
// only setup the stack trace stuff if the asserts are enabled (which happens in debug mode
|
||||
// basically)
|
||||
#ifdef DLIB_ENABLE_STACK_TRACE
|
||||
// basically). Also, this stuff doesn't work if you use NO_MAKEFILE
|
||||
#if defined(DLIB_ENABLE_STACK_TRACE) && !defined(NO_MAKEFILE)
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
inline const std::string get_stack_trace();
|
||||
const std::string get_stack_trace();
|
||||
}
|
||||
|
||||
// redefine the DLIB_CASSERT macro to include the stack trace
|
||||
@ -80,8 +78,6 @@ namespace dlib
|
||||
}}
|
||||
|
||||
|
||||
#include "threads.h"
|
||||
#include "stack.h"
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
@ -93,52 +89,11 @@ namespace dlib
|
||||
const char* funct_name,
|
||||
const char* file_name,
|
||||
const int line_number
|
||||
)
|
||||
{
|
||||
std::ostringstream sout;
|
||||
// if the function name isn't really long then put this all on a single line
|
||||
if (std::strlen(funct_name) < 40)
|
||||
sout << file_name << ":" << line_number << ": " << funct_name;
|
||||
else
|
||||
sout << file_name << ":" << line_number << "\n" << funct_name;
|
||||
);
|
||||
|
||||
// pop the string onto the function stack trace
|
||||
std::string temp(sout.str());
|
||||
trace().data().push(temp);
|
||||
}
|
||||
~stack_tracer();
|
||||
|
||||
~stack_tracer()
|
||||
{
|
||||
std::string temp;
|
||||
trace().data().pop(temp);
|
||||
}
|
||||
|
||||
static const std::string get_stack_trace()
|
||||
{
|
||||
std::ostringstream sout;
|
||||
trace().data().reset();
|
||||
while (trace().data().move_next())
|
||||
{
|
||||
sout << trace().data().element() << "\n";
|
||||
}
|
||||
return sout.str();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
typedef stack<std::string>::kernel_1a_c stack_of_string;
|
||||
static thread_specific_data<stack_of_string>& trace()
|
||||
{
|
||||
static thread_specific_data<stack_of_string> a;
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
inline const std::string get_stack_trace()
|
||||
{
|
||||
return stack_tracer::get_stack_trace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#define DLIB_STACK_TRACE_NAMED(x) stack_tracer dlib_stack_tracer_object(x,__FILE__,__LINE__)
|
||||
|
Loading…
Reference in New Issue
Block a user