diff options
-rw-r--r-- | base/debug/stack_trace.cc | 7 | ||||
-rw-r--r-- | base/debug/stack_trace.h | 4 | ||||
-rw-r--r-- | base/debug/stack_trace_unittest.cc | 5 |
3 files changed, 15 insertions, 1 deletions
diff --git a/base/debug/stack_trace.cc b/base/debug/stack_trace.cc index 94ce145..1919abc 100644 --- a/base/debug/stack_trace.cc +++ b/base/debug/stack_trace.cc @@ -9,6 +9,7 @@ #include <string.h> #include <algorithm> +#include <sstream> namespace base { namespace debug { @@ -30,5 +31,11 @@ const void *const *StackTrace::Addresses(size_t* count) const { return NULL; } +std::string StackTrace::ToString() const { + std::stringstream stream; + OutputToStream(&stream); + return stream.str(); +} + } // namespace debug } // namespace base diff --git a/base/debug/stack_trace.h b/base/debug/stack_trace.h index a524616..65421b23 100644 --- a/base/debug/stack_trace.h +++ b/base/debug/stack_trace.h @@ -7,6 +7,7 @@ #pragma once #include <iosfwd> +#include <string> #include "base/base_export.h" #include "build/build_config.h" @@ -52,6 +53,9 @@ class BASE_EXPORT StackTrace { // Resolves backtrace to symbols and write to stream. void OutputToStream(std::ostream* os) const; + // Resolves backtrace to symbols and returns as string. + std::string ToString() const; + private: // From http://msdn.microsoft.com/en-us/library/bb204633.aspx, // the sum of FramesToSkip and FramesToCapture must be less than 63, diff --git a/base/debug/stack_trace_unittest.cc b/base/debug/stack_trace_unittest.cc index ce5f313..925f619 100644 --- a/base/debug/stack_trace_unittest.cc +++ b/base/debug/stack_trace_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -28,6 +28,9 @@ TEST(StackTrace, MAYBE_OutputToStream) { trace.OutputToStream(&os); std::string backtrace_message = os.str(); + // ToString() should produce the same output. + EXPECT_EQ(backtrace_message, trace.ToString()); + #if defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG // Stack traces require an extra data table that bloats our binaries, // so they're turned off for release builds. We stop the test here, |