c++如何实现类似qDebug()的功能,即追加【换行】到末尾

#ifndef LOGHELPER_H
#define LOGHELPER_H

#include 
#include 

struct debug
{
    debug()
    {
    }

    ~debug()
    {
        std::cerr << m_SS.str() << std::endl;
    }

public:
    // accepts just about anything
    template
    debug &operator<<(const T &x)
    {
        m_SS << x;
        return *this;
    }
private:
    std::ostringstream m_SS;
};

#define LOG_INFO debug()
#define LOG_ERROR debug()

#endif // LOGHELPER_H

借鉴来自网络,

使用方法:

#include "LogHelper.h"

LOG_INFO << "hello loginfo";

等价于std::cout << "hello loginfo" <

 

你可能感兴趣的:(编程经验,qDebug,cout)