mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
cb8a987122
`Logger` is the new infrastructrure for debug messages. They are supposed to be used by just creating a global variable in a translation unit and then writing there directly as if they were a stream. Key features: * Self-registration: a `Logger` by default register itself automatically in a register. This means that at run-time, unlike with `DBG`, we have a list of all the possible `Logger`s. One benefit of this approach is being able to list all the available `Logger`s in `--help`. * Indentation: `Logger`s can be indented. In particular the `LoggerIndent` helper class can automatically indent a certain `Logger` during the lifetime of its instance. * Atomic messages: a message is no longer simply delimited by a "\n": to mark the end of a message, an instance of `LogTerminator` has to be streamed to the `Logger`. This can also be associated directly to the lifetime of an object by using the `LogOnReturn` class. * Custom class formatting: it is possible to decide how a certain object should be formatted when sent to a `Logger` by implementing the `writeToLog` function. For example, `llvm::Value`-derived objects are passed through the `getName` function. If no custom formatter is specified, the `operator<<` is employed.
79 lines
1.7 KiB
C++
79 lines
1.7 KiB
C++
/// \file debug.cpp
|
|
/// \brief Implementation of the debug framework
|
|
|
|
//
|
|
// This file is distributed under the MIT License. See LICENSE.md for details.
|
|
//
|
|
|
|
// Standard includes
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// LLVM includes
|
|
#include "llvm/IR/Value.h"
|
|
|
|
// Local includes
|
|
#include "debug.h"
|
|
|
|
#ifndef NDEBUG
|
|
namespace llvm {
|
|
void Value::assertModuleIsMaterialized() const { }
|
|
}
|
|
#endif
|
|
|
|
static size_t MaxLoggerNameLength = 0;
|
|
LogTerminator DoLog;
|
|
llvm::ManagedStatic<LoggersRegistry> Loggers;
|
|
|
|
bool DebuggingEnabled = false;
|
|
std::ostream &dbg(std::cerr);
|
|
static std::vector<std::string> DebugFeatures;
|
|
|
|
bool isDebugFeatureEnabled(std::string Name) {
|
|
return std::find(DebugFeatures.begin(),
|
|
DebugFeatures.end(),
|
|
Name) != DebugFeatures.end();
|
|
}
|
|
|
|
void enableDebugFeature(std::string Name) {
|
|
if (!isDebugFeatureEnabled(Name))
|
|
DebugFeatures.push_back(Name);
|
|
|
|
Loggers->enable(Name);
|
|
|
|
if (Name.size() > MaxLoggerNameLength)
|
|
MaxLoggerNameLength = Name.size();
|
|
}
|
|
|
|
void disableDebugFeature(std::string Name) {
|
|
auto It = std::find(DebugFeatures.begin(),
|
|
DebugFeatures.end(),
|
|
Name);
|
|
if (It != DebugFeatures.end())
|
|
DebugFeatures.erase(It);
|
|
|
|
Loggers->disable(Name);
|
|
}
|
|
|
|
template<bool X>
|
|
void Logger<X>::emit() {
|
|
if (X && Enabled) {
|
|
std::string Pad = std::string(MaxLoggerNameLength - Name.size(), ' ');
|
|
dbg << "[" << Name.data() << Pad << "] ";
|
|
for (unsigned I = 0; I < IndentLevel; I++)
|
|
dbg << " ";
|
|
dbg << Buffer.str() << "\n";
|
|
Buffer.str("");
|
|
Buffer.clear();
|
|
}
|
|
}
|
|
|
|
template<bool X>
|
|
unsigned Logger<X>::IndentLevel;
|
|
|
|
// Force instantiation
|
|
template class Logger<true>;
|
|
template class Logger<false>;
|