Skip to content

Commit

Permalink
Provide unified logging interface (#816)
Browse files Browse the repository at this point in the history
* retdec::io: new logging interface

Provides new logging interface that is ment to unify output that
is produced in each part of RetDec decompiler.

The interface is designed to provide eazy management
of logging into files/tty.

Currently each module od decompiler manages logging on its own.
This is not sustainable state as change to logging interface
is rather difficult.

* Use retdec::io interface for logging

* Get rid of llvm-support module

This module is now obsolete as all logging mechanisms
have been transferred to retdec::io module.

* Provide option [-s|--silent]

Provides new option [-s|--silent] for retdec-decompiler executable.
This option will force retdec not to output anything on stdout.

* retdec/config: provide option to specify Log/Error files

Provides way to specify Log/Error files to RetDec.
This is useful for RetDec plugins that might this way
control decompilation output.

* Create loggers from user configuration

* Fix validators error messages

Fixes bug provided by fbbff34.

* Change Log functions to return new object

Provides change to log interface. Before this commit
logging was done on global objects. Now the logging
should be made on temporary objects that are returned
from special functions.

The reason for this is for usage of colors on output.
When temporary object is destructed default color is
printed on required interface.

* Change namespace Log to class Log

This way we can avoid not intentional usage of the
Log interface.

All functions of Log are ment to be used like:

Log::info()
Log::debug()
Log::error()

* Provide missing doxygen

* Fix missing cassert header

Fixes dc57917.

* Enable ANSI colors on Windows

Starting Windows aniversary update Windows terminals provide
supprot for ANSI colors. This, however, needs to be enabled
manually.

More about this can be found here:
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences?redirectedfrom=MSDN

* utils/io/logger: don't print colors to not supported terminals

* Use macro OS_WINDOWS instead of __has_include

Provides substitution of __has_include macro to OS_WINDOWS defined
in utils/os.h. The reason for this is to maintain a level of
consistency in RetDec source code base.

* Add a CHANGELOG entry for #791
  • Loading branch information
Peter Kubov authored Jul 23, 2020
1 parent d394a99 commit b59f811
Show file tree
Hide file tree
Showing 101 changed files with 1,079 additions and 846 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# dev

* Enhancement: Unified logging on stdout/stderr. Added option `--silent`. Printed text is colored only when output is a terminal ([#791](https://github.com/avast/retdec/issues/791).
* Enhancement: Support all the CMake build types (i.e. `Debug`, `Release`, `RelWithDebInfo` and `MinSizeRel`) on all systems ([#774](https://github.com/avast/retdec/issues/774)).
* Enhancement: YARA updated to version 4.0.1 ([#758](https://github.com/avast/retdec/issues/758)), fixed Mach-O parsing issue ([#283](https://github.com/avast/retdec/issues/283)).
* Enhancement: Improved detection of many packers/installers/compilers in `retdec-fileinfo`, including Armadillo ([#733](https://github.com/avast/retdec/pull/733)), VMProtect ([#734](https://github.com/avast/retdec/pull/734), [#778](https://github.com/avast/retdec/pull/778)), Petite ([#735](https://github.com/avast/retdec/pull/735)), Enigma ([#741](https://github.com/avast/retdec/pull/741)), ASPack ([#743](https://github.com/avast/retdec/pull/743)), Eziriz ([#746](https://github.com/avast/retdec/pull/746)), PyInstaller ([#748](https://github.com/avast/retdec/pull/748)), Astrum InstallWizard ([#753](https://github.com/avast/retdec/pull/753)), AutoHotKey ([#756](https://github.com/avast/retdec/pull/756)), AutoIt ([#757](https://github.com/avast/retdec/pull/757)), BAT to PE-EXE script compilers ([#761](https://github.com/avast/retdec/pull/761)), Bero ([#764](https://github.com/avast/retdec/pull/764)), CExe ([#781](https://github.com/avast/retdec/pull/781)), MoleBox ([#815](https://github.com/avast/retdec/pull/815)), and other improvements ([#804](https://github.com/avast/retdec/pull/804)).
Expand Down
1 change: 0 additions & 1 deletion cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ foreach(t ${RETDEC_ENABLE})
set_if_equal(${t} "fileinfo" RETDEC_ENABLE_FILEINFO)
set_if_equal(${t} "getsig" RETDEC_ENABLE_GETSIG)
set_if_equal(${t} "idr2pat" RETDEC_ENABLE_IDR2PAT)
set_if_equal(${t} "llvm-support" RETDEC_ENABLE_LLVM_SUPPORT)
set_if_equal(${t} "llvmir-emul" RETDEC_ENABLE_LLVMIR_EMUL)
set_if_equal(${t} "llvmir2hll" RETDEC_ENABLE_LLVMIR2HLL)
set_if_equal(${t} "loader" RETDEC_ENABLE_LOADER)
Expand Down
8 changes: 7 additions & 1 deletion include/retdec/config/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Parameters
void setOutputConfigFile(const std::string& file);
void setOutputUnpackedFile(const std::string& file);
void setOutputFormat(const std::string& format);
void setLogFile(const std::string& file);
void setErrFile(const std::string& file);
void setMaxMemoryLimit(uint64_t limit);
void setIsMaxMemoryLimitHalfRam(bool f);
void setTimeout(uint64_t seconds);
Expand Down Expand Up @@ -95,6 +97,8 @@ class Parameters
const std::string& getOutputConfigFile() const;
const std::string& getOutputUnpackedFile() const;
const std::string& getOutputFormat() const;
const std::string& getLogFile() const;
const std::string& getErrFile() const;
uint64_t getMaxMemoryLimit() const;
uint64_t getTimeout() const;
retdec::common::Address getEntryPoint() const;
Expand Down Expand Up @@ -136,7 +140,7 @@ class Parameters
private:
/// Decompilation will verbosely inform about the
/// decompilation process.
bool _verboseOutput = false;
bool _verboseOutput = true;

/// Keep all functions in the decompiler's output.
/// Otherwise, only functions reachable from main are kept.
Expand All @@ -158,6 +162,8 @@ class Parameters
std::string _outputConfigFile;
std::string _outputUnpackedFile;
std::string _outputFormat;
std::string _logFile;
std::string _errFile;
uint64_t _maxMemoryLimit = 0;
bool _maxMemoryLimitHalfRam = true;
uint64_t _timeout = 0;
Expand Down
132 changes: 0 additions & 132 deletions include/retdec/llvm-support/diagnostics.h

This file was deleted.

1 change: 0 additions & 1 deletion include/retdec/llvmir2hll/llvmir2hll.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
#include "retdec/llvmir2hll/var_name_gen/var_name_gens/num_var_name_gen.h"
#include "retdec/llvmir2hll/var_renamer/var_renamer.h"
#include "retdec/llvmir2hll/var_renamer/var_renamer_factory.h"
#include "retdec/llvm-support/diagnostics.h"
#include "retdec/utils/container.h"
#include "retdec/utils/conversion.h"
#include "retdec/utils/memory.h"
Expand Down
1 change: 0 additions & 1 deletion include/retdec/llvmir2hll/optimizer/optimizer_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "retdec/llvmir2hll/optimizer/optimizer.h"
#include "retdec/llvmir2hll/support/smart_ptr.h"
#include "retdec/llvmir2hll/support/types.h"
#include "retdec/llvm-support/diagnostics.h"
#include "retdec/utils/non_copyable.h"

namespace retdec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>

#include "retdec/llvmir2hll/pattern/pattern_finder_runner.h"
#include "retdec/utils/io/log.h"

namespace retdec {
namespace llvmir2hll {
Expand All @@ -23,7 +24,7 @@ namespace llvmir2hll {
*/
class CLIPatternFinderRunner: public PatternFinderRunner {
public:
CLIPatternFinderRunner(llvm::raw_ostream &os);
CLIPatternFinderRunner(utils::io::Logger &os);

private:
virtual void doActionsBeforePatternFinderRuns(ShPtr<PatternFinder> pf) override;
Expand All @@ -34,7 +35,7 @@ class CLIPatternFinderRunner: public PatternFinderRunner {

private:
/// Output stream, into which the patterns will be emitted.
llvm::raw_ostream &os;
utils::io::Logger &os;
};

} // namespace llvmir2hll
Expand Down
10 changes: 5 additions & 5 deletions include/retdec/llvmir2hll/validator/validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

#include "retdec/llvmir2hll/support/smart_ptr.h"
#include "retdec/llvmir2hll/support/visitors/ordered_all_visitor.h"
#include "retdec/llvm-support/diagnostics.h"
#include "retdec/utils/io/log.h"

using namespace retdec::utils::io;

namespace retdec {
namespace llvmir2hll {
Expand Down Expand Up @@ -45,12 +47,10 @@ class Validator: protected OrderedAllVisitor {
/**
* @brief Function to be called when there is a validation error.
*/
template<typename... Args>
void validationError(const std::string &warningMessage, Args &&... args) {
void validationError(const std::string &warningMessage) {
moduleIsCorrect = false;
if (printMessageOnError) {
retdec::llvm_support::printWarningMessage(warningMessage,
std::forward<Args>(args)...);
Log::error() << Log::Warning << warningMessage << std::endl;
}
}

Expand Down
12 changes: 7 additions & 5 deletions include/retdec/unpacker/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
#ifndef RETDEC_UNPACKER_PLUGIN_H
#define RETDEC_UNPACKER_PLUGIN_H

#include <iostream>
#include <memory>
#include <sstream>
#include <string>

#include "retdec/utils/io/log.h"
#include "retdec/unpacker/unpacker_exception.h"

#define plugin(T) retdec::unpackertool::Plugin::instance<T>()

using namespace retdec::utils::io;

namespace retdec {
namespace unpackertool {

Expand Down Expand Up @@ -171,7 +173,7 @@ class Plugin
*/
template <typename... Args> void log(const Args&... args)
{
Plugin::logImpl(std::cout, "[", getInfo()->name, "] ", args...);
Plugin::logImpl(Log::get(Log::Type::Info), "[", getInfo()->name, "] ", args...);
}

/**
Expand All @@ -184,7 +186,7 @@ class Plugin
*/
template <typename... Args> void error(const Args&... args)
{
Plugin::logImpl(std::cerr, "[ERROR] [", getInfo()->name, "] ", args...);
Plugin::logImpl(Log::get(Log::Type::Error), "[ERROR] [", getInfo()->name, "] ", args...);
}

/**
Expand All @@ -211,13 +213,13 @@ class Plugin
private:
PluginExitCode _cachedExitCode; ///< Cached exit code of the plugin for the unpacked file.

template <typename T, typename... Args> static void logImpl(std::ostream& out, const T& data, const Args&... args)
template <typename T, typename... Args> static void logImpl(Logger& out, const T& data, const Args&... args)
{
out << data;
logImpl(out, args...);
}

static void logImpl(std::ostream& out)
static void logImpl(Logger& out)
{
out << std::endl;
}
Expand Down
Loading

0 comments on commit b59f811

Please sign in to comment.