Pavel Labath 5fae71c51c Convert Log class to llvm streams
Summary:
This converts LLDB's logging to use llvm streams instead of
lldb_private::Stream and friends. The changes are mostly
straight-forward and amount to s/lldb_private::Stream/llvm::raw_ostream.

The part worth calling out is the rewrite of the StreamCallback class.
Previously this class contained a per-thread buffer of data written. I
assume this had something to do with it trying to make sure each log
line is delivered as a single event, instead of multiple (possibly
interleaved) events. However, this is no longer relevant as the Log
class already writes things to a temporary buffer and then delivers the
message as a single "write", so I have just removed the code in
question.

Reviewers: zturner, clayborg

Subscribers: emaste, lldb-commits, mgorny

Differential Revision: https://reviews.llvm.org/D29615

llvm-svn: 294736
2017-02-10 11:49:21 +00:00

60 lines
1.9 KiB
C++

//===-- 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 "gtest/gtest.h"
#include "lldb/Core/Log.h"
#include "lldb/Host/Host.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb;
using namespace lldb_private;
static std::string GetLogString(uint32_t log_options, const char *format,
int arg) {
std::string stream_string;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(stream_string));
Log log_(stream_sp);
log_.GetOptions().Reset(log_options);
Log *log = &log_;
LLDB_LOG(log, format, arg);
return stream_sp->str();
}
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,0+4}/{1,0+4}] Hello World 47\n",
Host::GetCurrentProcessID(),
Host::GetCurrentThreadID())
.str(),
GetLogString(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD,
"Hello World {0}", 47));
}