1cfca06d7SDimitry Andric //===-- Log.cpp -----------------------------------------------------------===//
274a628f7SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
674a628f7SDimitry Andric //
774a628f7SDimitry Andric //===----------------------------------------------------------------------===//
874a628f7SDimitry Andric
974a628f7SDimitry Andric #include "lldb/Utility/Log.h"
1074a628f7SDimitry Andric #include "lldb/Utility/VASPrintf.h"
1174a628f7SDimitry Andric
1274a628f7SDimitry Andric #include "llvm/ADT/SmallString.h"
1394994d37SDimitry Andric #include "llvm/ADT/Twine.h"
1494994d37SDimitry Andric #include "llvm/ADT/iterator.h"
1574a628f7SDimitry Andric
16145449b1SDimitry Andric #include "llvm/Support/Casting.h"
1774a628f7SDimitry Andric #include "llvm/Support/Chrono.h"
1894994d37SDimitry Andric #include "llvm/Support/ManagedStatic.h"
1974a628f7SDimitry Andric #include "llvm/Support/Path.h"
2074a628f7SDimitry Andric #include "llvm/Support/Signals.h"
2174a628f7SDimitry Andric #include "llvm/Support/Threading.h"
2274a628f7SDimitry Andric #include "llvm/Support/raw_ostream.h"
2374a628f7SDimitry Andric
2494994d37SDimitry Andric #include <chrono>
2574a628f7SDimitry Andric #include <cstdarg>
2674a628f7SDimitry Andric #include <mutex>
2794994d37SDimitry Andric #include <utility>
2874a628f7SDimitry Andric
29344a3780SDimitry Andric #include <cassert>
30f73363f1SDimitry Andric #if defined(_WIN32)
3194994d37SDimitry Andric #include <process.h>
3274a628f7SDimitry Andric #else
3374a628f7SDimitry Andric #include <unistd.h>
3474a628f7SDimitry Andric #endif
3574a628f7SDimitry Andric
3674a628f7SDimitry Andric using namespace lldb_private;
3774a628f7SDimitry Andric
38145449b1SDimitry Andric char LogHandler::ID;
39145449b1SDimitry Andric char StreamLogHandler::ID;
40145449b1SDimitry Andric char CallbackLogHandler::ID;
41145449b1SDimitry Andric char RotatingLogHandler::ID;
42ac9a064cSDimitry Andric char TeeLogHandler::ID;
43145449b1SDimitry Andric
4474a628f7SDimitry Andric llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
4574a628f7SDimitry Andric
ForEachCategory(const Log::ChannelMap::value_type & entry,llvm::function_ref<void (llvm::StringRef,llvm::StringRef)> lambda)46ead24645SDimitry Andric void Log::ForEachCategory(
47ead24645SDimitry Andric const Log::ChannelMap::value_type &entry,
48ead24645SDimitry Andric llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
49ead24645SDimitry Andric lambda("all", "all available logging categories");
50ead24645SDimitry Andric lambda("default", "default set of logging categories");
5174a628f7SDimitry Andric for (const auto &category : entry.second.m_channel.categories)
52ead24645SDimitry Andric lambda(category.name, category.description);
53ead24645SDimitry Andric }
54ead24645SDimitry Andric
ListCategories(llvm::raw_ostream & stream,const ChannelMap::value_type & entry)55ead24645SDimitry Andric void Log::ListCategories(llvm::raw_ostream &stream,
56ead24645SDimitry Andric const ChannelMap::value_type &entry) {
57ead24645SDimitry Andric stream << llvm::formatv("Logging categories for '{0}':\n", entry.first());
58ead24645SDimitry Andric ForEachCategory(entry,
59ead24645SDimitry Andric [&stream](llvm::StringRef name, llvm::StringRef description) {
60ead24645SDimitry Andric stream << llvm::formatv(" {0} - {1}\n", name, description);
61ead24645SDimitry Andric });
6274a628f7SDimitry Andric }
6374a628f7SDimitry Andric
GetFlags(llvm::raw_ostream & stream,const ChannelMap::value_type & entry,llvm::ArrayRef<const char * > categories)64e3b55780SDimitry Andric Log::MaskType Log::GetFlags(llvm::raw_ostream &stream,
65e3b55780SDimitry Andric const ChannelMap::value_type &entry,
6674a628f7SDimitry Andric llvm::ArrayRef<const char *> categories) {
6774a628f7SDimitry Andric bool list_categories = false;
68e3b55780SDimitry Andric Log::MaskType flags = 0;
6974a628f7SDimitry Andric for (const char *category : categories) {
70344a3780SDimitry Andric if (llvm::StringRef("all").equals_insensitive(category)) {
71e3b55780SDimitry Andric flags |= std::numeric_limits<Log::MaskType>::max();
7274a628f7SDimitry Andric continue;
7374a628f7SDimitry Andric }
74344a3780SDimitry Andric if (llvm::StringRef("default").equals_insensitive(category)) {
7574a628f7SDimitry Andric flags |= entry.second.m_channel.default_flags;
7674a628f7SDimitry Andric continue;
7774a628f7SDimitry Andric }
78344a3780SDimitry Andric auto cat = llvm::find_if(entry.second.m_channel.categories,
79344a3780SDimitry Andric [&](const Log::Category &c) {
80344a3780SDimitry Andric return c.name.equals_insensitive(category);
81344a3780SDimitry Andric });
8274a628f7SDimitry Andric if (cat != entry.second.m_channel.categories.end()) {
8374a628f7SDimitry Andric flags |= cat->flag;
8474a628f7SDimitry Andric continue;
8574a628f7SDimitry Andric }
8674a628f7SDimitry Andric stream << llvm::formatv("error: unrecognized log category '{0}'\n",
8774a628f7SDimitry Andric category);
8874a628f7SDimitry Andric list_categories = true;
8974a628f7SDimitry Andric }
9074a628f7SDimitry Andric if (list_categories)
9174a628f7SDimitry Andric ListCategories(stream, entry);
9274a628f7SDimitry Andric return flags;
9374a628f7SDimitry Andric }
9474a628f7SDimitry Andric
Enable(const std::shared_ptr<LogHandler> & handler_sp,uint32_t options,Log::MaskType flags)95145449b1SDimitry Andric void Log::Enable(const std::shared_ptr<LogHandler> &handler_sp,
96e3b55780SDimitry Andric uint32_t options, Log::MaskType flags) {
9774a628f7SDimitry Andric llvm::sys::ScopedWriter lock(m_mutex);
9874a628f7SDimitry Andric
996f8fc217SDimitry Andric MaskType mask = m_mask.fetch_or(flags, std::memory_order_relaxed);
10074a628f7SDimitry Andric if (mask | flags) {
10174a628f7SDimitry Andric m_options.store(options, std::memory_order_relaxed);
102145449b1SDimitry Andric m_handler = handler_sp;
10374a628f7SDimitry Andric m_channel.log_ptr.store(this, std::memory_order_relaxed);
10474a628f7SDimitry Andric }
10574a628f7SDimitry Andric }
10674a628f7SDimitry Andric
Disable(Log::MaskType flags)107e3b55780SDimitry Andric void Log::Disable(Log::MaskType flags) {
10874a628f7SDimitry Andric llvm::sys::ScopedWriter lock(m_mutex);
10974a628f7SDimitry Andric
1106f8fc217SDimitry Andric MaskType mask = m_mask.fetch_and(~flags, std::memory_order_relaxed);
11174a628f7SDimitry Andric if (!(mask & ~flags)) {
112145449b1SDimitry Andric m_handler.reset();
11374a628f7SDimitry Andric m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
11474a628f7SDimitry Andric }
11574a628f7SDimitry Andric }
11674a628f7SDimitry Andric
Dump(llvm::raw_ostream & output_stream)117145449b1SDimitry Andric bool Log::Dump(llvm::raw_ostream &output_stream) {
118145449b1SDimitry Andric llvm::sys::ScopedReader lock(m_mutex);
119145449b1SDimitry Andric if (RotatingLogHandler *handler =
120145449b1SDimitry Andric llvm::dyn_cast_or_null<RotatingLogHandler>(m_handler.get())) {
121145449b1SDimitry Andric handler->Dump(output_stream);
122145449b1SDimitry Andric return true;
123145449b1SDimitry Andric }
124145449b1SDimitry Andric return false;
125145449b1SDimitry Andric }
126145449b1SDimitry Andric
GetOptions() const12774a628f7SDimitry Andric const Flags Log::GetOptions() const {
12874a628f7SDimitry Andric return m_options.load(std::memory_order_relaxed);
12974a628f7SDimitry Andric }
13074a628f7SDimitry Andric
GetMask() const131e3b55780SDimitry Andric Log::MaskType Log::GetMask() const {
13274a628f7SDimitry Andric return m_mask.load(std::memory_order_relaxed);
13374a628f7SDimitry Andric }
13474a628f7SDimitry Andric
PutCString(const char * cstr)1357fa27ce4SDimitry Andric void Log::PutCString(const char *cstr) { PutString(cstr); }
1367fa27ce4SDimitry Andric
PutString(llvm::StringRef str)1377fa27ce4SDimitry Andric void Log::PutString(llvm::StringRef str) {
1387fa27ce4SDimitry Andric std::string FinalMessage;
1397fa27ce4SDimitry Andric llvm::raw_string_ostream Stream(FinalMessage);
1407fa27ce4SDimitry Andric WriteHeader(Stream, "", "");
1417fa27ce4SDimitry Andric Stream << str << "\n";
1427fa27ce4SDimitry Andric WriteMessage(FinalMessage);
1437fa27ce4SDimitry Andric }
14474a628f7SDimitry Andric
14574a628f7SDimitry Andric // Simple variable argument logging with flags.
Printf(const char * format,...)14674a628f7SDimitry Andric void Log::Printf(const char *format, ...) {
14774a628f7SDimitry Andric va_list args;
14874a628f7SDimitry Andric va_start(args, format);
14974a628f7SDimitry Andric VAPrintf(format, args);
15074a628f7SDimitry Andric va_end(args);
15174a628f7SDimitry Andric }
15274a628f7SDimitry Andric
VAPrintf(const char * format,va_list args)15374a628f7SDimitry Andric void Log::VAPrintf(const char *format, va_list args) {
15474a628f7SDimitry Andric llvm::SmallString<64> Content;
15574a628f7SDimitry Andric lldb_private::VASprintf(Content, format, args);
1567fa27ce4SDimitry Andric PutString(Content);
1577fa27ce4SDimitry Andric }
15874a628f7SDimitry Andric
Formatf(llvm::StringRef file,llvm::StringRef function,const char * format,...)1597fa27ce4SDimitry Andric void Log::Formatf(llvm::StringRef file, llvm::StringRef function,
1607fa27ce4SDimitry Andric const char *format, ...) {
1617fa27ce4SDimitry Andric va_list args;
1627fa27ce4SDimitry Andric va_start(args, format);
1637fa27ce4SDimitry Andric VAFormatf(file, function, format, args);
1647fa27ce4SDimitry Andric va_end(args);
1657fa27ce4SDimitry Andric }
16674a628f7SDimitry Andric
VAFormatf(llvm::StringRef file,llvm::StringRef function,const char * format,va_list args)1677fa27ce4SDimitry Andric void Log::VAFormatf(llvm::StringRef file, llvm::StringRef function,
1687fa27ce4SDimitry Andric const char *format, va_list args) {
1697fa27ce4SDimitry Andric llvm::SmallString<64> Content;
1707fa27ce4SDimitry Andric lldb_private::VASprintf(Content, format, args);
1717fa27ce4SDimitry Andric Format(file, function, llvm::formatv("{0}", Content));
17274a628f7SDimitry Andric }
17374a628f7SDimitry Andric
17474a628f7SDimitry Andric // Printing of errors that are not fatal.
Error(const char * format,...)17574a628f7SDimitry Andric void Log::Error(const char *format, ...) {
17674a628f7SDimitry Andric va_list args;
17774a628f7SDimitry Andric va_start(args, format);
17874a628f7SDimitry Andric VAError(format, args);
17974a628f7SDimitry Andric va_end(args);
18074a628f7SDimitry Andric }
18174a628f7SDimitry Andric
VAError(const char * format,va_list args)18274a628f7SDimitry Andric void Log::VAError(const char *format, va_list args) {
18374a628f7SDimitry Andric llvm::SmallString<64> Content;
18474a628f7SDimitry Andric VASprintf(Content, format, args);
18574a628f7SDimitry Andric
18674a628f7SDimitry Andric Printf("error: %s", Content.c_str());
18774a628f7SDimitry Andric }
18874a628f7SDimitry Andric
189f73363f1SDimitry Andric // Printing of warnings that are not fatal only if verbose mode is enabled.
Verbose(const char * format,...)19074a628f7SDimitry Andric void Log::Verbose(const char *format, ...) {
19174a628f7SDimitry Andric if (!GetVerbose())
19274a628f7SDimitry Andric return;
19374a628f7SDimitry Andric
19474a628f7SDimitry Andric va_list args;
19574a628f7SDimitry Andric va_start(args, format);
19674a628f7SDimitry Andric VAPrintf(format, args);
19774a628f7SDimitry Andric va_end(args);
19874a628f7SDimitry Andric }
19974a628f7SDimitry Andric
20074a628f7SDimitry Andric // Printing of warnings that are not fatal.
Warning(const char * format,...)20174a628f7SDimitry Andric void Log::Warning(const char *format, ...) {
20274a628f7SDimitry Andric llvm::SmallString<64> Content;
20374a628f7SDimitry Andric va_list args;
20474a628f7SDimitry Andric va_start(args, format);
20574a628f7SDimitry Andric VASprintf(Content, format, args);
20674a628f7SDimitry Andric va_end(args);
20774a628f7SDimitry Andric
20874a628f7SDimitry Andric Printf("warning: %s", Content.c_str());
20974a628f7SDimitry Andric }
21074a628f7SDimitry Andric
Register(llvm::StringRef name,Channel & channel)21174a628f7SDimitry Andric void Log::Register(llvm::StringRef name, Channel &channel) {
21274a628f7SDimitry Andric auto iter = g_channel_map->try_emplace(name, channel);
21374a628f7SDimitry Andric assert(iter.second == true);
214b1c73532SDimitry Andric UNUSED_IF_ASSERT_DISABLED(iter);
21574a628f7SDimitry Andric }
21674a628f7SDimitry Andric
Unregister(llvm::StringRef name)21774a628f7SDimitry Andric void Log::Unregister(llvm::StringRef name) {
21874a628f7SDimitry Andric auto iter = g_channel_map->find(name);
21974a628f7SDimitry Andric assert(iter != g_channel_map->end());
220e3b55780SDimitry Andric iter->second.Disable(std::numeric_limits<MaskType>::max());
22174a628f7SDimitry Andric g_channel_map->erase(iter);
22274a628f7SDimitry Andric }
22374a628f7SDimitry Andric
EnableLogChannel(const std::shared_ptr<LogHandler> & log_handler_sp,uint32_t log_options,llvm::StringRef channel,llvm::ArrayRef<const char * > categories,llvm::raw_ostream & error_stream)224145449b1SDimitry Andric bool Log::EnableLogChannel(const std::shared_ptr<LogHandler> &log_handler_sp,
22574a628f7SDimitry Andric uint32_t log_options, llvm::StringRef channel,
226145449b1SDimitry Andric llvm::ArrayRef<const char *> categories,
227145449b1SDimitry Andric llvm::raw_ostream &error_stream) {
22874a628f7SDimitry Andric auto iter = g_channel_map->find(channel);
22974a628f7SDimitry Andric if (iter == g_channel_map->end()) {
23074a628f7SDimitry Andric error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
23174a628f7SDimitry Andric return false;
23274a628f7SDimitry Andric }
233e3b55780SDimitry Andric MaskType flags = categories.empty()
23474a628f7SDimitry Andric ? iter->second.m_channel.default_flags
23574a628f7SDimitry Andric : GetFlags(error_stream, *iter, categories);
236145449b1SDimitry Andric iter->second.Enable(log_handler_sp, log_options, flags);
23774a628f7SDimitry Andric return true;
23874a628f7SDimitry Andric }
23974a628f7SDimitry Andric
DisableLogChannel(llvm::StringRef channel,llvm::ArrayRef<const char * > categories,llvm::raw_ostream & error_stream)24074a628f7SDimitry Andric bool Log::DisableLogChannel(llvm::StringRef channel,
24174a628f7SDimitry Andric llvm::ArrayRef<const char *> categories,
24274a628f7SDimitry Andric llvm::raw_ostream &error_stream) {
24374a628f7SDimitry Andric auto iter = g_channel_map->find(channel);
24474a628f7SDimitry Andric if (iter == g_channel_map->end()) {
24574a628f7SDimitry Andric error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
24674a628f7SDimitry Andric return false;
24774a628f7SDimitry Andric }
248e3b55780SDimitry Andric MaskType flags = categories.empty()
249e3b55780SDimitry Andric ? std::numeric_limits<MaskType>::max()
25074a628f7SDimitry Andric : GetFlags(error_stream, *iter, categories);
25174a628f7SDimitry Andric iter->second.Disable(flags);
25274a628f7SDimitry Andric return true;
25374a628f7SDimitry Andric }
25474a628f7SDimitry Andric
DumpLogChannel(llvm::StringRef channel,llvm::raw_ostream & output_stream,llvm::raw_ostream & error_stream)255145449b1SDimitry Andric bool Log::DumpLogChannel(llvm::StringRef channel,
256145449b1SDimitry Andric llvm::raw_ostream &output_stream,
257145449b1SDimitry Andric llvm::raw_ostream &error_stream) {
258145449b1SDimitry Andric auto iter = g_channel_map->find(channel);
259145449b1SDimitry Andric if (iter == g_channel_map->end()) {
260145449b1SDimitry Andric error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
261145449b1SDimitry Andric return false;
262145449b1SDimitry Andric }
263145449b1SDimitry Andric if (!iter->second.Dump(output_stream)) {
264145449b1SDimitry Andric error_stream << llvm::formatv(
265145449b1SDimitry Andric "log channel '{0}' does not support dumping.\n", channel);
266145449b1SDimitry Andric return false;
267145449b1SDimitry Andric }
268145449b1SDimitry Andric return true;
269145449b1SDimitry Andric }
270145449b1SDimitry Andric
ListChannelCategories(llvm::StringRef channel,llvm::raw_ostream & stream)27174a628f7SDimitry Andric bool Log::ListChannelCategories(llvm::StringRef channel,
27274a628f7SDimitry Andric llvm::raw_ostream &stream) {
27374a628f7SDimitry Andric auto ch = g_channel_map->find(channel);
27474a628f7SDimitry Andric if (ch == g_channel_map->end()) {
27574a628f7SDimitry Andric stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
27674a628f7SDimitry Andric return false;
27774a628f7SDimitry Andric }
27874a628f7SDimitry Andric ListCategories(stream, *ch);
27974a628f7SDimitry Andric return true;
28074a628f7SDimitry Andric }
28174a628f7SDimitry Andric
DisableAllLogChannels()28274a628f7SDimitry Andric void Log::DisableAllLogChannels() {
28374a628f7SDimitry Andric for (auto &entry : *g_channel_map)
284e3b55780SDimitry Andric entry.second.Disable(std::numeric_limits<MaskType>::max());
28574a628f7SDimitry Andric }
28674a628f7SDimitry Andric
ForEachChannelCategory(llvm::StringRef channel,llvm::function_ref<void (llvm::StringRef,llvm::StringRef)> lambda)287ead24645SDimitry Andric void Log::ForEachChannelCategory(
288ead24645SDimitry Andric llvm::StringRef channel,
289ead24645SDimitry Andric llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
290ead24645SDimitry Andric auto ch = g_channel_map->find(channel);
291ead24645SDimitry Andric if (ch == g_channel_map->end())
292ead24645SDimitry Andric return;
293ead24645SDimitry Andric
294ead24645SDimitry Andric ForEachCategory(*ch, lambda);
295ead24645SDimitry Andric }
296ead24645SDimitry Andric
ListChannels()297ead24645SDimitry Andric std::vector<llvm::StringRef> Log::ListChannels() {
298ead24645SDimitry Andric std::vector<llvm::StringRef> result;
299ead24645SDimitry Andric for (const auto &channel : *g_channel_map)
300ead24645SDimitry Andric result.push_back(channel.first());
301ead24645SDimitry Andric return result;
302ead24645SDimitry Andric }
303ead24645SDimitry Andric
ListAllLogChannels(llvm::raw_ostream & stream)30474a628f7SDimitry Andric void Log::ListAllLogChannels(llvm::raw_ostream &stream) {
30574a628f7SDimitry Andric if (g_channel_map->empty()) {
30674a628f7SDimitry Andric stream << "No logging channels are currently registered.\n";
30774a628f7SDimitry Andric return;
30874a628f7SDimitry Andric }
30974a628f7SDimitry Andric
31074a628f7SDimitry Andric for (const auto &channel : *g_channel_map)
31174a628f7SDimitry Andric ListCategories(stream, channel);
31274a628f7SDimitry Andric }
31374a628f7SDimitry Andric
GetVerbose() const31474a628f7SDimitry Andric bool Log::GetVerbose() const {
31574a628f7SDimitry Andric return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
31674a628f7SDimitry Andric }
31774a628f7SDimitry Andric
WriteHeader(llvm::raw_ostream & OS,llvm::StringRef file,llvm::StringRef function)31874a628f7SDimitry Andric void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
31974a628f7SDimitry Andric llvm::StringRef function) {
32074a628f7SDimitry Andric Flags options = GetOptions();
32174a628f7SDimitry Andric static uint32_t g_sequence_id = 0;
32274a628f7SDimitry Andric // Add a sequence ID if requested
32374a628f7SDimitry Andric if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
32474a628f7SDimitry Andric OS << ++g_sequence_id << " ";
32574a628f7SDimitry Andric
32674a628f7SDimitry Andric // Timestamp if requested
32774a628f7SDimitry Andric if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
32874a628f7SDimitry Andric auto now = std::chrono::duration<double>(
32974a628f7SDimitry Andric std::chrono::system_clock::now().time_since_epoch());
33074a628f7SDimitry Andric OS << llvm::formatv("{0:f9} ", now.count());
33174a628f7SDimitry Andric }
33274a628f7SDimitry Andric
33374a628f7SDimitry Andric // Add the process and thread if requested
33474a628f7SDimitry Andric if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
33574a628f7SDimitry Andric OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
33674a628f7SDimitry Andric llvm::get_threadid());
33774a628f7SDimitry Andric
33874a628f7SDimitry Andric // Add the thread name if requested
33974a628f7SDimitry Andric if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
34074a628f7SDimitry Andric llvm::SmallString<32> thread_name;
34174a628f7SDimitry Andric llvm::get_thread_name(thread_name);
342f73363f1SDimitry Andric
343f73363f1SDimitry Andric llvm::SmallString<12> format_str;
344f73363f1SDimitry Andric llvm::raw_svector_ostream format_os(format_str);
345f73363f1SDimitry Andric format_os << "{0,-" << llvm::alignTo<16>(thread_name.size()) << "} ";
346f73363f1SDimitry Andric OS << llvm::formatv(format_str.c_str(), thread_name);
34774a628f7SDimitry Andric }
34874a628f7SDimitry Andric
34974a628f7SDimitry Andric if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
35074a628f7SDimitry Andric llvm::sys::PrintStackTrace(OS);
35174a628f7SDimitry Andric
35274a628f7SDimitry Andric if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
35374a628f7SDimitry Andric (!file.empty() || !function.empty())) {
35474a628f7SDimitry Andric file = llvm::sys::path::filename(file).take_front(40);
35574a628f7SDimitry Andric function = function.take_front(40);
35674a628f7SDimitry Andric OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
35774a628f7SDimitry Andric }
35874a628f7SDimitry Andric }
35974a628f7SDimitry Andric
3607fa27ce4SDimitry Andric // If we have a callback registered, then we call the logging callback. If we
3617fa27ce4SDimitry Andric // have a valid file handle, we also log to the file.
WriteMessage(llvm::StringRef message)3627fa27ce4SDimitry Andric void Log::WriteMessage(llvm::StringRef message) {
363f73363f1SDimitry Andric // Make a copy of our stream shared pointer in case someone disables our log
364f73363f1SDimitry Andric // while we are logging and releases the stream
365145449b1SDimitry Andric auto handler_sp = GetHandler();
366145449b1SDimitry Andric if (!handler_sp)
36774a628f7SDimitry Andric return;
368145449b1SDimitry Andric handler_sp->Emit(message);
36974a628f7SDimitry Andric }
37074a628f7SDimitry Andric
Format(llvm::StringRef file,llvm::StringRef function,const llvm::formatv_object_base & payload)37174a628f7SDimitry Andric void Log::Format(llvm::StringRef file, llvm::StringRef function,
37274a628f7SDimitry Andric const llvm::formatv_object_base &payload) {
37374a628f7SDimitry Andric std::string message_string;
37474a628f7SDimitry Andric llvm::raw_string_ostream message(message_string);
37574a628f7SDimitry Andric WriteHeader(message, file, function);
37674a628f7SDimitry Andric message << payload << "\n";
37774a628f7SDimitry Andric WriteMessage(message.str());
37874a628f7SDimitry Andric }
379145449b1SDimitry Andric
StreamLogHandler(int fd,bool should_close,size_t buffer_size)380145449b1SDimitry Andric StreamLogHandler::StreamLogHandler(int fd, bool should_close,
381145449b1SDimitry Andric size_t buffer_size)
382145449b1SDimitry Andric : m_stream(fd, should_close, buffer_size == 0) {
383145449b1SDimitry Andric if (buffer_size > 0)
384145449b1SDimitry Andric m_stream.SetBufferSize(buffer_size);
385145449b1SDimitry Andric }
386145449b1SDimitry Andric
~StreamLogHandler()387145449b1SDimitry Andric StreamLogHandler::~StreamLogHandler() { Flush(); }
388145449b1SDimitry Andric
Flush()389145449b1SDimitry Andric void StreamLogHandler::Flush() {
390145449b1SDimitry Andric std::lock_guard<std::mutex> guard(m_mutex);
391145449b1SDimitry Andric m_stream.flush();
392145449b1SDimitry Andric }
393145449b1SDimitry Andric
Emit(llvm::StringRef message)394145449b1SDimitry Andric void StreamLogHandler::Emit(llvm::StringRef message) {
395145449b1SDimitry Andric if (m_stream.GetBufferSize() > 0) {
396145449b1SDimitry Andric std::lock_guard<std::mutex> guard(m_mutex);
397145449b1SDimitry Andric m_stream << message;
398145449b1SDimitry Andric } else {
399145449b1SDimitry Andric m_stream << message;
400145449b1SDimitry Andric }
401145449b1SDimitry Andric }
402145449b1SDimitry Andric
CallbackLogHandler(lldb::LogOutputCallback callback,void * baton)403145449b1SDimitry Andric CallbackLogHandler::CallbackLogHandler(lldb::LogOutputCallback callback,
404145449b1SDimitry Andric void *baton)
405145449b1SDimitry Andric : m_callback(callback), m_baton(baton) {}
406145449b1SDimitry Andric
Emit(llvm::StringRef message)407145449b1SDimitry Andric void CallbackLogHandler::Emit(llvm::StringRef message) {
408145449b1SDimitry Andric m_callback(message.data(), m_baton);
409145449b1SDimitry Andric }
410145449b1SDimitry Andric
RotatingLogHandler(size_t size)411145449b1SDimitry Andric RotatingLogHandler::RotatingLogHandler(size_t size)
412145449b1SDimitry Andric : m_messages(std::make_unique<std::string[]>(size)), m_size(size) {}
413145449b1SDimitry Andric
Emit(llvm::StringRef message)414145449b1SDimitry Andric void RotatingLogHandler::Emit(llvm::StringRef message) {
415145449b1SDimitry Andric std::lock_guard<std::mutex> guard(m_mutex);
416145449b1SDimitry Andric ++m_total_count;
417145449b1SDimitry Andric const size_t index = m_next_index;
418145449b1SDimitry Andric m_next_index = NormalizeIndex(index + 1);
419145449b1SDimitry Andric m_messages[index] = message.str();
420145449b1SDimitry Andric }
421145449b1SDimitry Andric
NormalizeIndex(size_t i) const422145449b1SDimitry Andric size_t RotatingLogHandler::NormalizeIndex(size_t i) const { return i % m_size; }
423145449b1SDimitry Andric
GetNumMessages() const424145449b1SDimitry Andric size_t RotatingLogHandler::GetNumMessages() const {
425145449b1SDimitry Andric return m_total_count < m_size ? m_total_count : m_size;
426145449b1SDimitry Andric }
427145449b1SDimitry Andric
GetFirstMessageIndex() const428145449b1SDimitry Andric size_t RotatingLogHandler::GetFirstMessageIndex() const {
429145449b1SDimitry Andric return m_total_count < m_size ? 0 : m_next_index;
430145449b1SDimitry Andric }
431145449b1SDimitry Andric
Dump(llvm::raw_ostream & stream) const432145449b1SDimitry Andric void RotatingLogHandler::Dump(llvm::raw_ostream &stream) const {
433145449b1SDimitry Andric std::lock_guard<std::mutex> guard(m_mutex);
434145449b1SDimitry Andric const size_t start_idx = GetFirstMessageIndex();
435145449b1SDimitry Andric const size_t stop_idx = start_idx + GetNumMessages();
436145449b1SDimitry Andric for (size_t i = start_idx; i < stop_idx; ++i) {
437145449b1SDimitry Andric const size_t idx = NormalizeIndex(i);
438145449b1SDimitry Andric stream << m_messages[idx];
439145449b1SDimitry Andric }
440145449b1SDimitry Andric stream.flush();
441145449b1SDimitry Andric }
442ac9a064cSDimitry Andric
TeeLogHandler(std::shared_ptr<LogHandler> first_log_handler,std::shared_ptr<LogHandler> second_log_handler)443ac9a064cSDimitry Andric TeeLogHandler::TeeLogHandler(std::shared_ptr<LogHandler> first_log_handler,
444ac9a064cSDimitry Andric std::shared_ptr<LogHandler> second_log_handler)
445ac9a064cSDimitry Andric : m_first_log_handler(first_log_handler),
446ac9a064cSDimitry Andric m_second_log_handler(second_log_handler) {
447ac9a064cSDimitry Andric assert(m_first_log_handler && "first log handler must be valid");
448ac9a064cSDimitry Andric assert(m_second_log_handler && "second log handler must be valid");
449ac9a064cSDimitry Andric }
450ac9a064cSDimitry Andric
Emit(llvm::StringRef message)451ac9a064cSDimitry Andric void TeeLogHandler::Emit(llvm::StringRef message) {
452ac9a064cSDimitry Andric m_first_log_handler->Emit(message);
453ac9a064cSDimitry Andric m_second_log_handler->Emit(message);
454ac9a064cSDimitry Andric }
455