2

我正在为我的项目开发一个简单的记录器包装器,它可以让我轻松换出后端。
这是我理想的界面:

log::error << "some" << " log " << "message";

我实现它的方式是:

  1. log::error#operator<<返回一个临时Sink对象。

  2. Sink#operator<<返回*this并定义一个移动构造函数。

  3. 完整的消息可以在Sink调用链末尾调用的析构函数中使用。

人为的实现:

#include <iostream>
#include <string>

struct Sink {

  Sink (std::string const& msg) : m_message(msg) {}

  // no copying
  Sink (Sink const& orig) = delete;

  // move constructor
  Sink (Sink && orig) : m_message(std::move(orig.m_message)) {};

  // use the complete string in the destructor
  ~Sink() { std::cerr << m_message << std::endl;}

  Sink operator<< (std::string const& msg) {
    m_message.append(msg);
    return std::move(*this);
  }

  std::string m_message;
};

struct Level {
  Sink operator<< (std::string const& msg) { return Sink(msg); }
};

int main() {
  Level log;

  log << "this" << " is " << "a " << "test";
}

这很好用,除非我需要一种干净的方式来禁用日志记录。如果我没有使用链接,我的日志函数可以使用预处理器指令来删除函数的内容

void log (std::string) {
  #ifdef LOGGING_ENABLED
    // log message
  #endif
}

然后编译器将优化并删除空函数调用。但我不知道如何使用我试图实现的 api 来做到这一点。我知道这是可能的,因为glog以某种方式做到了。

使用这样的指令违背了拥有一个好的 api 的目的。

#ifdef LOGGING_ENABLED
  log << "this" << " is " << "a " << "test";
#endif

禁用这些类型的链接调用的干净方法是什么?任何帮助表示赞赏。

4

3 回答 3

4

你必须实现另一个Sink在记录时什么都不做的。Glog 称之为空流:

// A class for which we define operator<<, which does nothing.
class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {
 public:
  // Initialize the LogStream so the messages can be written somewhere
  // (they'll never be actually displayed). This will be needed if a
  // NullStream& is implicitly converted to LogStream&, in which case
  // the overloaded NullStream::operator<< will not be invoked.
  NullStream() : LogMessage::LogStream(message_buffer_, 1, 0) { }
  NullStream(const char* /*file*/, int /*line*/,
             const CheckOpString& /*result*/) :
      LogMessage::LogStream(message_buffer_, 1, 0) { }
  NullStream &stream() { return *this; }
 private:
  // A very short buffer for messages (which we discard anyway). This
  // will be needed if NullStream& converted to LogStream& (e.g. as a
  // result of a conditional expression).
  char message_buffer_[2];
};

// Do nothing. This operator is inline, allowing the message to be
// compiled away. The message will not be compiled away if we do
// something like (flag ? LOG(INFO) : LOG(ERROR)) << message; when
// SKIP_LOG=WARNING. In those cases, NullStream will be implicitly
// converted to LogStream and the message will be computed and then
// quietly discarded.
template<class T>
inline NullStream& operator<<(NullStream &str, const T &) { return str; }

在您的情况下,一个简单的实现看起来像

#ifdef LOGGING_ENABLED
  /* your sink */
#else
  struct Sink {
    Sink (std::string)  {}
    Sink (Sink const& orig) {};
  };
  template <typename T> Sink operator<<(Sink s, T) { return s; }
#endif

这非常简单,可以远离编译器进行优化。

于 2012-11-28T17:06:05.437 回答
1

流方法存在一个问题,即使是空流:C++ 中没有惰性计算。

也就是说,即使您的流对参数不做任何事情,参数仍然是完全创建的。

避免这种评估的唯一方法是使用宏:

#define LOG(Message_) \
    do (LogManager::activated()) {
        logger << Message_;
    } while(0);

当然,我会注意到,如果你使用宏,这是一个很好的机会来插入__func__,__FILE____LINE__.

于 2012-11-28T18:28:08.660 回答
1

这不是最漂亮的,但你可以这样做:

#ifdef LOGGING_ENABLED
#define LOG(message) message
#else
#define LOG(message)
#endif


LOG(log << "this" << "is" << "a" << "test");

你可以通过这样做稍微简化它

#ifdef LOGGING_ENABLED
#define LOG(message) log << message
#else
#define LOG(message)
#endif


LOG("this" << "is" << "a" << "test");
于 2012-11-28T16:56:54.203 回答