Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lldb/include/lldb/Utility/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ template <typename Cat> Log *GetLog(Cat mask) {
return LogChannelFor<Cat>().GetLog(Log::MaskType(mask));
}

/// Getter and setter for the error log (see g_error_log).
/// The error log is set to the system log in SystemInitializerFull. We can't
/// use the system log directly because that would violate the layering between
/// Utility and Host.
/// @{
void SetLLDBErrorLog(Log *log);
Log *GetLLDBErrorLog();
/// @}

} // namespace lldb_private

/// The LLDB_LOG* macros defined below are the way to emit log messages.
Expand Down Expand Up @@ -387,6 +396,9 @@ template <typename Cat> Log *GetLog(Cat mask) {
if (log_private && error_private) { \
log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
__VA_ARGS__); \
} else if (::lldb_private::Log *log_error = GetLLDBErrorLog()) { \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend structuring this in a way that avoids evaluating __VA_ARGS__ more than once. You could e.g. insert a if(!log_private) log_private = GetLLDBErrorLog() statement before this if.

(I know that the macro gets dynamically evaluated only once, but I was recently reading about how this innocent-looking macro in the linux kernel exploded to several dozen kilobytes of goo. We're not there yet, but this should help anyone looking at the preprocessed output, and also help the compiler generate less code)

log_error->FormatError(::std::move(error_private), __FILE__, __func__, \
__VA_ARGS__); \
} else \
::llvm::consumeError(::std::move(error_private)); \
} while (0)
Expand All @@ -401,6 +413,9 @@ template <typename Cat> Log *GetLog(Cat mask) {
if (log_private && log_private->GetVerbose() && error_private) { \
log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
__VA_ARGS__); \
} else if (::lldb_private::Log *log_error = GetLLDBErrorLog()) { \
log_error->FormatError(::std::move(error_private), __FILE__, __func__, \
__VA_ARGS__); \
} else \
::llvm::consumeError(::std::move(error_private)); \
} while (0)
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/API/SystemInitializerFull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ llvm::Error SystemInitializerFull::Initialize() {
// Use the Debugger's LLDBAssert callback.
SetLLDBAssertCallback(Debugger::AssertCallback);

// Use the system log to report errors that would otherwise get dropped.
SetLLDBErrorLog(GetLog(SystemLog::System));

LLDB_LOG(GetLog(SystemLog::System), "{0}", GetVersion());

return llvm::Error::success();
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/Utility/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ char TeeLogHandler::ID;

llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;

// The error log is used by LLDB_LOG_ERROR and LLDB_LOG_ERRORV. If the given
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The error log is used by LLDB_LOG_ERROR and LLDB_LOG_ERRORV. If the given
// The error log is used by LLDB_LOG_ERROR. If the given

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already fixed in the latest version of this PR :-)

// log channel is not enabled, error messages are logged to the error log.
static std::atomic<Log *> g_error_log = nullptr;

void Log::ForEachCategory(
const Log::ChannelMap::value_type &entry,
llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
Expand Down Expand Up @@ -460,3 +464,7 @@ void TeeLogHandler::Emit(llvm::StringRef message) {
m_first_log_handler->Emit(message);
m_second_log_handler->Emit(message);
}

void lldb_private::SetLLDBErrorLog(Log *log) { g_error_log.exchange(log); }

Log *lldb_private::GetLLDBErrorLog() { return g_error_log; }
Loading