首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在项目中实现良好的调试/日志记录功能

如何在项目中实现良好的调试/日志记录功能
EN

Stack Overflow用户
提问于 2011-05-29 21:55:18
回答 3查看 35.3K关注 0票数 14

我正在做一个小项目,总共约3-4人。我想要有一个可靠的方式来调试应用程序,例如通过日志。有没有关于如何构建它之类的好资源?我从项目经理那里听到很多关于好的日志功能对每个项目都很重要的说法,但我不确定如何做到这一点。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-29 22:41:33

我发现Dobb博士的这篇文章,Logging In C++,对这个主题非常有用。

在Dobb博士的网站上还有:A Highly Configurable Logging Framework In C++

如果你想要的是一个非常简单的线程安全的日志类,它总是输出到stderr,那么你可以使用我写的这个类:

代码语言:javascript
复制
#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_

#include <iostream>
#include <sstream>

/* consider adding boost thread id since we'll want to know whose writting and
 * won't want to repeat it for every single call */

/* consider adding policy class to allow users to redirect logging to specific
 * files via the command line
 */

enum loglevel_e
    {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4};

class logIt
{
public:
    logIt(loglevel_e _loglevel = logERROR) {
        _buffer << _loglevel << " :" 
            << std::string(
                _loglevel > logDEBUG 
                ? (_loglevel - logDEBUG) * 4 
                : 1
                , ' ');
    }

    template <typename T>
    logIt & operator<<(T const & value)
    {
        _buffer << value;
        return *this;
    }

    ~logIt()
    {
        _buffer << std::endl;
        // This is atomic according to the POSIX standard
        // http://www.gnu.org/s/libc/manual/html_node/Streams-and-Threads.html
        std::cerr << _buffer.str();
    }

private:
    std::ostringstream _buffer;
};

extern loglevel_e loglevel;

#define log(level) \
if (level > loglevel) ; \
else logIt(level)

#endif

像这样使用它:

代码语言:javascript
复制
// define and turn off for the rest of the test suite
loglevel_e loglevel = logERROR;

void logTest(void) {
    loglevel_e loglevel_save = loglevel;

    loglevel = logDEBUG4;

    log(logINFO) << "foo " << "bar " << "baz";

    int count = 3;
    log(logDEBUG) << "A loop with "    << count << " iterations";
    for (int i = 0; i != count; ++i)
    {
        log(logDEBUG1) << "the counter i = " << i;
        log(logDEBUG2) << "the counter i = " << i;
    }

    loglevel = loglevel_save;
}
票数 16
EN

Stack Overflow用户

发布于 2011-05-30 16:35:10

如果您正在询问有关日志记录框架的信息,并且您在C++中工作,请查看Apache的log4cxx。理解这个架构需要一些时间,但一旦你理解了,你就会意识到它是灵活性、易用性和(正如他们所说的)性能之间的良好平衡。

log4cxx具有非常灵活的配置,您无需重新编译即可控制输出到何处(文件/旋转文件/控制台/等),子组件的调试级别(例如,您希望专注于特定的类/组件,以便将其设置为DEBUG级别,而其余的在INFO上),日志条目的格式等。

如果你问的是关于如何做日志的general guidelines,我还没有见过这样的东西(并不是我真正寻找的)。我认为这主要是经验性的-你决定每个日志级别需要什么信息,比如INFO,DEBUG等,然后根据你和你的客户的需求来改进它(不要忘记,你的客户也可以是日志的客户,这取决于你的项目)。

票数 4
EN

Stack Overflow用户

发布于 2011-05-29 22:27:02

这取决于你所说的“日志”是什么意思。一种形式是简单地提供一种将某些对象的内容打印到输出流的方法。对于ClassName类型的对象,这需要为类编写一个插入操作符:

代码语言:javascript
复制
std::ostream &operator<< (std::ostream &stream, const ClassName & obj) {
   // Implementation elided
}

有了它,您就可以将ClassName类型的对象打印到输出流。这可能非常有用,以至于一些组织要求每个类都实现这样的方法。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6168107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档