C++-根据级别记录语句

C++ - Logging statements according to the level

本文关键字:记录 语句 C++-      更新时间:2023-10-16

我有以下语句:

static Logging::Logger* common_logger = new Logging::Logger(Logging::Logger::LEVEL);

在Logger.h中,我有

  class Logger {
public:
    enum LEVEL {
        Debug,
        Warning,
        Notification,
        Error
    };
  }

我已经将Logger.h文件包含在我的另一个类中,作为:

    Logging::log(CustomDialog::logger, Logging::Entry, CustomDialog::CLASSNAME, "CustomDialog");

我需要知道这是否是正确的方法。我这样做的原因是根据级别获取日志。

问候,

看看Log4cxx——它很容易使用,几乎包含了C++日志框架中需要的所有功能。它是可扩展的,可以通过配置文件进行配置,甚至支持开箱即用的远程登录。

你可以使用ACE_DEBUG,它看起来很老派(ala printf),但线程安全、可靠且完全可配置(使用日志文件、stdout等)。当然,你必须链接到libACE(自适应通信框架),但它的开发包现在在许多linux发行版中默认都很容易获得。我一直在查看Als提到的C++日志库帖子中的列表,但似乎大多数人都遇到了许多框架的内存泄漏,boost::Log还没有发布。

另一点是,大多数日志库都使用流,例如:

// from thread 1
mlog(mlog::DEBUG) << "Debug message goes here" << mlog::endl;
// from thread 2
mlog(mlog::INFO) << "Info message goes here" << mlog::endl;

将无法在多线程环境中按预期工作,而ACE将在那里正确执行。

上面的输出看起来像这样:

[thread1 | 12:04.23] Debug me[thread2 | 12:04.24] Info message goesssage goes herehere