llvm-project/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp
Pavel Labath 0f08db66db [lldb] Make logging machinery type-safe
This patch makes use of c++ type checking and scoped enums to make
logging statements shorter and harder to misuse.

Defines like LIBLLDB_LOG_PROCESS are replaces with LLDBLog::Process.
Because it now carries type information we do not need to worry about
matching a specific enum value with the right getter function -- the
compiler will now do that for us.

The main entry point for the logging machinery becomes the GetLog
(template) function, which will obtain the correct Log object based on
the enum type. It achieves this through another template function
(LogChannelFor<T>), which must be specialized for each type, and should
return the appropriate channel object.

This patch also removes the ability to log a message if multiple
categories are enabled simultaneously as it was unused and confusing.

This patch does not actually remove any of the existing interfaces. The
defines and log retrieval functions are left around as wrappers around
the new interfaces. They will be removed in follow-up patch.

Differential Revision: https://reviews.llvm.org/D117490
2022-01-25 12:13:49 +01:00

48 lines
1.8 KiB
C++

//===-- ProcessGDBRemoteLog.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ProcessGDBRemoteLog.h"
#include "ProcessGDBRemote.h"
#include "llvm/Support/Threading.h"
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::process_gdb_remote;
static constexpr Log::Category g_categories[] = {
{{"async"}, {"log asynchronous activity"}, GDBRLog::Async},
{{"break"}, {"log breakpoints"}, GDBRLog::Breakpoints},
{{"comm"}, {"log communication activity"}, GDBRLog::Comm},
{{"packets"}, {"log gdb remote packets"}, GDBRLog::Packets},
{{"memory"}, {"log memory reads and writes"}, GDBRLog::Memory},
{{"data-short"},
{"log memory bytes for memory reads and writes for short transactions "
"only"},
GDBRLog::MemoryDataShort},
{{"data-long"},
{"log memory bytes for memory reads and writes for all transactions"},
GDBRLog::MemoryDataLong},
{{"process"}, {"log process events and activities"}, GDBRLog::Process},
{{"step"}, {"log step related activities"}, GDBRLog::Step},
{{"thread"}, {"log thread events and activities"}, GDBRLog::Thread},
{{"watch"}, {"log watchpoint related activities"}, GDBRLog::Watchpoints},
};
static Log::Channel g_channel(g_categories, GDBRLog::Packets);
template <> Log::Channel &lldb_private::LogChannelFor<GDBRLog>() {
return g_channel;
}
void ProcessGDBRemoteLog::Initialize() {
static llvm::once_flag g_once_flag;
llvm::call_once(g_once_flag, []() {
Log::Register("gdb-remote", g_channel);
});
}