c++ - #define 预处理器指令可以包含 if 和 else 吗?

标签 c++ visual-studio c-preprocessor preprocessor-directive

我正在尝试来自此链接的记录器代码,但它给了我错误。 How to implement a good debug/logging feature in a project

#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

更准确地说,这个#define 给出了错误:

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

错误是Syntax error: ifSyntax error: else

但后来,我注意到如果我将 #include "logger.hpp"main.h 移动到 main.cpp,问题消失了。尽管“main.h”在不同的地方被多次包含,但它确实包含“#pragma once”。

有什么想法吗?

最佳答案

如果 loglevel 在编译时已知,您可以执行以下操作:

template <bool>
struct LogSystem
{
    template <class T>
    LogSystem& operator << (const T &)
    {
        //ignore the input
        return (*this);
    }
};

template <>
struct LogSystem <true>
{
    template <class T>
    LogSystem& operator << (const T & v)
    {
        cout << v;
        return (*this);
    }
};

template <bool B>
LogSystem<B>    getLog()
{
    return LogSystem<B>();
}

#define log(level) getLog< (level <= loglevel) >()

如果 loglevel 在编译时未知:

class iLogSystem
{
public:
    virtual iLogSystem& operator << (const int &)
    {
        //empty
        return (*this);
    }
    virtual iLogSystem& operator << (const custom_type &);
    {
        return (*this);
    }
    //make functions for logging all the types you want
};

class LogSystem : public iLogSystem
{
public:
    virtual iLogSystem& operator << (const int & v)
    {
        cout << v;
        return (*this);
    }
    virtual iLogSystem& operator << (const custom_type &  q);
    {
        cout << q.toString();
        return (*this);
    }
    //make functions for logging all the types you want
};
iLogSystem& getLog(const bool l)
{
    static LogSystem actual_log;
    static iLogSystem empty_log;
    if(l)
        return &actual_log;
    return &empty_log;
}

#define log(level) getLog( level <= loglevel )

关于c++ - #define 预处理器指令可以包含 if 和 else 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19795038/

相关文章:

c++ - cin.get() 不工作

c++ - 通过命令行使用 R 时出错

visual-studio - 我想要标准化的行尾吗?

c# - 从当前项目中获取文件路径

visual-studio - 恢复 Windows 应用程序崩溃调试按钮

c++ - C 预处理器和 if-else 条件语句

c++ - 通过 FlasCC 在 AS3 中使用解压库

c++ - 多态对象的外部化渲染

c-preprocessor - 是否有任何 C 预处理器作为独立程序?

objective-c - Xcode:在另一个项目使用的一个项目中定义预处理器宏