From 818bc6adf6b57fd71880a14ffacf5b9773bf9867 Mon Sep 17 00:00:00 2001 From: Davis King Date: Thu, 15 May 2008 20:56:04 +0000 Subject: [PATCH] Changed the stack trace stuff so that it doesn't perform memory allocations or copy strings. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402242 --- dlib/stack_trace.cpp | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/dlib/stack_trace.cpp b/dlib/stack_trace.cpp index 6cecf4f43..91c70ec51 100644 --- a/dlib/stack_trace.cpp +++ b/dlib/stack_trace.cpp @@ -10,16 +10,27 @@ #include "stack_trace.h" #include "threads.h" #include "stack.h" +#include "memory_manager.h" namespace dlib { // ---------------------------------------------------------------------------------------- - thread_specific_data::kernel_1a>& get_dlib_stack_trace_stack() + namespace { - static thread_specific_data::kernel_1a> a; - return a; + struct stack_tracer_data + { + const char* funct_name; + const char* file_name; + int line_number; + }; + + thread_specific_data::kernel_2a>::kernel_1a>& get_dlib_stack_trace_stack() + { + static thread_specific_data::kernel_2a>::kernel_1a> a; + return a; + } } // ---------------------------------------------------------------------------------------- @@ -31,16 +42,13 @@ namespace dlib 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; + stack_tracer_data data; + data.funct_name = funct_name; + data.file_name = file_name; + data.line_number = line_number; - // pop the string onto the function stack trace - std::string temp(sout.str()); - get_dlib_stack_trace_stack().data().push(temp); + // pop the info onto the function stack trace + get_dlib_stack_trace_stack().data().push(data); } // ---------------------------------------------------------------------------------------- @@ -48,7 +56,7 @@ namespace dlib stack_tracer:: ~stack_tracer() { - std::string temp; + stack_tracer_data temp; get_dlib_stack_trace_stack().data().pop(temp); } @@ -60,7 +68,8 @@ namespace dlib 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"; + stack_tracer_data data = get_dlib_stack_trace_stack().data().element(); + sout << data.file_name << ":" << data.line_number << "\n " << data.funct_name; } return sout.str(); }