【问题标题】:How to make C++ logging more concise如何使 C++ 日志记录更简洁
【发布时间】:2012-11-27 14:43:44
【问题描述】:

我正在使用 log4cxx 登录 C++ 项目。目前,我的日志调用看起来像

LOG4CXX_INFO(log4cxx::Logger::getRootLogger(), "parsed " << lc << " lines");

这仍然太冗长且难以记住。理想情况下,日志语句看起来类似于

log.debug("parsed %d lines", lc)

a) 我可以做些什么来获得更简洁的日志记录语句? b) 是否可以使用类似printf 的语法?

【问题讨论】:

  • 添加宏调试(x) LOGC4xx....., x)
  • printf like is c not c++...,使用此语法的 C++ 日志记录比使用 printf 语法的 c 日志记录更快。
  • @neagoegab 我应该在哪里添加这个宏,以便在我的项目中随处可用?
  • @neagoegab 对我来说,printf-like 读写也很方便,不像流。
  • 在 commonincludes 头文件中...或 loggerinclude.h

标签: c++ logging log4cxx


【解决方案1】:

您可以使用 Boost.Format 库http://www.boost.org/doc/libs/1_52_0/libs/format/doc/format.html

PS。 C++11 便利函数示例:

#include <boost/format.hpp>
#include <iostream>

void
log (const boost::format &fmt)
{
  std::cout << fmt;
}

template<typename T, typename... Args>
void
log (boost::format &fmt, const T &v, Args... args)
{
  log (boost::format (fmt) % v, args ...);
}

template<typename... Args>
void
log (const char *fmt, Args... args)
{
  boost::format f (fmt);
  log (f, args ...);
}

int
main ()
{
  log ("some number %1% and some other %2%", 1, 3.5f);
}

【讨论】:

    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多