diff options
| author | Pavel Labath <labath@google.com> | 2017-01-18 11:00:26 +0000 |
|---|---|---|
| committer | Pavel Labath <labath@google.com> | 2017-01-18 11:00:26 +0000 |
| commit | 107d9bbd6c7280959556da864c0c67dc9d951e14 (patch) | |
| tree | 43ca304e71c4d51f4e243861af30a7f36a16a68f /lldb/unittests/Core/LogTest.cpp | |
| parent | ee6d7186c3c7b154cce0c3cfb5e234db8de5c277 (diff) | |
| download | bcm5719-llvm-107d9bbd6c7280959556da864c0c67dc9d951e14.tar.gz bcm5719-llvm-107d9bbd6c7280959556da864c0c67dc9d951e14.zip | |
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
Diffstat (limited to 'lldb/unittests/Core/LogTest.cpp')
| -rw-r--r-- | lldb/unittests/Core/LogTest.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lldb/unittests/Core/LogTest.cpp b/lldb/unittests/Core/LogTest.cpp new file mode 100644 index 00000000000..46640edfb15 --- /dev/null +++ b/lldb/unittests/Core/LogTest.cpp @@ -0,0 +1,56 @@ +//===-- LogTest.cpp ---------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Core/Log.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Host/Host.h" +#include "gtest/gtest.h" + +using namespace lldb; +using namespace lldb_private; + +static std::string GetLogString(uint32_t log_options, const char *format, + int arg) { + std::shared_ptr<StreamString> stream_sp(new StreamString()); + Log log_(stream_sp); + log_.GetOptions().Reset(log_options); + Log *log = &log_; + LLDB_LOG(log, format, arg); + return stream_sp->GetString(); +} + +TEST(LogTest, LLDB_LOG_nullptr) { + Log *log = nullptr; + LLDB_LOG(log, "{0}", 0); // Shouldn't crash +} + +TEST(LogTest, log_options) { + EXPECT_EQ("Hello World 47\n", GetLogString(0, "Hello World {0}", 47)); + EXPECT_EQ("Hello World 47\n", + GetLogString(LLDB_LOG_OPTION_THREADSAFE, "Hello World {0}", 47)); + + { + std::string msg = + GetLogString(LLDB_LOG_OPTION_PREPEND_SEQUENCE, "Hello World {0}", 47); + int seq_no; + EXPECT_EQ(1, sscanf(msg.c_str(), "%d Hello World 47", &seq_no)); + } + + EXPECT_EQ( + "LogTest.cpp:GetLogString Hello " + "World 47\n", + GetLogString(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION, "Hello World {0}", 47)); + + EXPECT_EQ(llvm::formatv("[{0}/{1}] Hello World 47\n", + Host::GetCurrentProcessID(), + Host::GetCurrentThreadID()) + .str(), + GetLogString(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD, + "Hello World {0}", 47)); +} |

