summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorsatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-12 16:13:40 +0000
committersatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-12 16:13:40 +0000
commitd4114baa6a01b346441eae309493c31149d8296a (patch)
tree8415f979dea2d0ca0a3c885ab3652845d794e906 /base
parent173fd09e335caa8fe487e10516f1709e7b1f8197 (diff)
downloadchromium_src-d4114baa6a01b346441eae309493c31149d8296a.zip
chromium_src-d4114baa6a01b346441eae309493c31149d8296a.tar.gz
chromium_src-d4114baa6a01b346441eae309493c31149d8296a.tar.bz2
Add StackTrace::ToString().
This can be used for debug logging like: LOG(ERROR) << base::debug::StackTrace().ToString(); As opposed to std::strstream stream; base::debug::StackTrace().OutputToStream(&stream); LOG(ERROR) << stream.str(); BUG=none TEST=add a test to stack_unitttest and confirm it passes Review URL: http://codereview.chromium.org/8206015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105085 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/debug/stack_trace.cc7
-rw-r--r--base/debug/stack_trace.h4
-rw-r--r--base/debug/stack_trace_unittest.cc5
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,