C ++启用/禁用std :: cout的调试消息 [英] C++ enable/disable debug messages of std::couts on the fly

查看:419
本文介绍了C ++启用/禁用std :: cout的调试消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当在程序中使用std :: cout时,是否有一种方法来定义/未定义调试消息?



我知道有这样的事情,如#define ,#ifndef,但我想是有一个更清洁的方法有一个变量说:

 #debug ON 

打印所有我的调试数据(使用std :: cout)。因此,我们将有这样的代码调试:

  #ifndef DEBUG 
//做有用的事情
#endif

我发现上面的代码繁琐,当你写100秒的调试代码。 >

谢谢!



Carlo

解决方案

一些日志库非常重,除非你有复杂的日志需求。这里是我只是敲在一起。需要一些测试,但可能符合您的要求:

  #include< cstdio& 
#include< cstdarg>

class CLog
{
public:
enum {All = 0,Debug,Info,Warning,Error,Fatal,None};
static void Write(int nLevel,const char * szFormat,...);
static void SetLevel(int nLevel);

protected:
static void CheckInit();
static void Init();

private:
CLog();
static bool m_bInitialised;
static int m_nLevel;
};

bool CLog :: m_bInitialised;
int CLog :: m_nLevel;

void CLog :: Write(int nLevel,const char * szFormat,...)
{
CheckInit();
if(nLevel> = m_nLevel)
{
va_list args;
va_start(args,szFormat);
vprintf(szFormat,args);
va_end(args);
}
}
void CLog :: SetLevel(int nLevel)
{
m_nLevel = nLevel;
m_bInitialised = true;
}
void CLog :: CheckInit()
{
if(!m_bInitialised)
{
Init
}
}
void CLog :: Init()
{
int nDfltLevel(CLog :: All);
//从环境变量中检索级别,
//注册表项或者wherecer
SetLevel(nDfltLevel);
}

int main()
{
CLog :: Write(CLog :: Debug,testing 1 2 3);
return 0;
}


Is there a way to define/undefine debug messages using std::cout whenever inside a program?

I am aware that there are such things such as #define, #ifndef, but I was thinking is there a cleaner way to having a variable say:

# debug ON

That prints all of my debug data (using std::cout). Consequently, we'll have code like this for debug:

#ifndef DEBUG
// do something useful
#endif

I find the above code cumbersome when you write 100s of debug code.

Thanks!

Carlo

解决方案

Some logging libraries are pretty heavy weight unless you have complex logging needs. Here's something I just knocked together. Needs a little testing but might meet your requirements:

#include <cstdio>
#include <cstdarg>

class CLog
{
public:
    enum { All=0, Debug, Info, Warning, Error, Fatal, None };
    static void Write(int nLevel, const char *szFormat, ...);
    static void SetLevel(int nLevel);

protected:
    static void CheckInit();
    static void Init();

private:
    CLog();
    static bool m_bInitialised;
    static int  m_nLevel;
};

bool CLog::m_bInitialised;
int  CLog::m_nLevel;

void CLog::Write(int nLevel, const char *szFormat, ...)
{
    CheckInit();
    if (nLevel >= m_nLevel)
    {
        va_list args;
        va_start(args, szFormat);
        vprintf(szFormat, args);
        va_end(args);
    }
}
void CLog::SetLevel(int nLevel)
{
    m_nLevel = nLevel;
    m_bInitialised = true;
}
void CLog::CheckInit()
{
    if (!m_bInitialised)
    {
        Init();
    }
}
void CLog::Init()
{
    int nDfltLevel(CLog::All);
    // Retrieve your level from an environment variable, 
    // registry entry or wherecer
    SetLevel(nDfltLevel);
}

int main()
{
    CLog::Write(CLog::Debug, "testing 1 2 3");
    return 0;
}

这篇关于C ++启用/禁用std :: cout的调试消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆