C++如何禁用字符串流输出?
我有一个日志,使用以下方式:
LOG(INFO) << "string";
这样定义:
#define LOG(status) LOG_##status.stream()
#define LOG_ERROR LOG_INFO
#define LOG_INFO LogMessage(__FILE__, __FUNCTION__, __LINE__, "I")
#define LOG_DEBUG LogMessage(__FILE__, __FUNCTION__, __LINE__, "D")
#define LOG_WARNING LogMessage(__FILE__, __FUNCTION__, __LINE__, "W")
#define LOG_FATAL LogMessageFatal(__FILE__, __FUNCTION__, __LINE__)
#define VLOG(level) \
VLogMessage(__FILE__, __FUNCTION__, __LINE__, level).stream()
#define CHECK(x) \
if (!(x)) \
LogMessageFatal(__FILE__, __FUNCTION__, __LINE__).stream() \
<< "Check failed: " #x << ": " // NOLINT(*)
#define CHECK_EQ(x, y) _CHECK_BINARY(x, ==, y)
#define CHECK_NE(x, y) _CHECK_BINARY(x, !=, y)
#define CHECK_LT(x, y) _CHECK_BINARY(x, <, y)
#define CHECK_LE(x, y) _CHECK_BINARY(x, <=, y)
#define CHECK_GT(x, y) _CHECK_BINARY(x, >, y)
#define CHECK_GE(x, y) _CHECK_BINARY(x, >=, y)
#define _CHECK_BINARY(x, cmp, y) CHECK(x cmp y) << x << "!" #cmp << y << " "
using namespace std;
inline void gen_log(std::ostream &log_stream_, const char *file,
const char *func, int lineno, const char *level,
const int kMaxLen = 20) {
const int len = strlen(file);
std::string time_str;
struct tm tm_time; // Time of creation of LogMessage
time_t timestamp = time(NULL);
localtime_r(×tamp, &tm_time);
struct timeval tv;
gettimeofday(&tv, NULL);
log_stream_ << level << ' ' << 1 + tm_time.tm_mon << '/' << tm_time.tm_mday
<< ' ' << tm_time.tm_hour << ':' << tm_time.tm_min << ':'
<< std::setw(2) << tm_time.tm_sec << '.' << std::setw(3)
<< tv.tv_usec / 1000 << " ";
if (len > kMaxLen) {
log_stream_ << "..." << file + len - kMaxLen << " " << func << ":" << lineno
<< "] ";
} else {
log_stream_ << file << " " << func << ":" << lineno << "] ";
}
}
class LogMessage {
public:
LogMessage(const char *file, const char *func, int lineno,
const char *level = "I") {
if (*level == 'I') {
#if LOG_LEVEL <= 1
gen_log(log_stream_, file, func, lineno, level);
#else
log_stream_ = std::stringstream(nullptr);
#endif
} else if (*level == 'D') {
#if LOG_LEVEL <= 2
gen_log(log_stream_, file, func, lineno, level);
#else
log_stream_ = std::stringstream(nullptr);
#endif
} else if (*level == 'W') {
#if LOG_LEVEL <= 3
gen_log(log_stream_, file, func, lineno, level);
#else
log_stream_ = std::stringstream(nullptr);
#endif
}
}
~LogMessage() {
log_stream_ << '\n';
fprintf(stderr, "%s", log_stream_.str().c_str());
}
std::ostream &stream() { return log_stream_; }
protected:
std::stringstream log_stream_;
LogMessage(const LogMessage &) = delete;
void operator=(const LogMessage &) = delete;
};
现在,我想控制所有日志,而不是打印出(全部)。
我应该怎么做?
IMO,我需要发送一个空的流,但是&lt;&lt;&lt;&lt;如何使其被忽略?
I have a log, which using like:
LOG(INFO) << "string";
defined like this:
#define LOG(status) LOG_##status.stream()
#define LOG_ERROR LOG_INFO
#define LOG_INFO LogMessage(__FILE__, __FUNCTION__, __LINE__, "I")
#define LOG_DEBUG LogMessage(__FILE__, __FUNCTION__, __LINE__, "D")
#define LOG_WARNING LogMessage(__FILE__, __FUNCTION__, __LINE__, "W")
#define LOG_FATAL LogMessageFatal(__FILE__, __FUNCTION__, __LINE__)
#define VLOG(level) \
VLogMessage(__FILE__, __FUNCTION__, __LINE__, level).stream()
#define CHECK(x) \
if (!(x)) \
LogMessageFatal(__FILE__, __FUNCTION__, __LINE__).stream() \
<< "Check failed: " #x << ": " // NOLINT(*)
#define CHECK_EQ(x, y) _CHECK_BINARY(x, ==, y)
#define CHECK_NE(x, y) _CHECK_BINARY(x, !=, y)
#define CHECK_LT(x, y) _CHECK_BINARY(x, <, y)
#define CHECK_LE(x, y) _CHECK_BINARY(x, <=, y)
#define CHECK_GT(x, y) _CHECK_BINARY(x, >, y)
#define CHECK_GE(x, y) _CHECK_BINARY(x, >=, y)
#define _CHECK_BINARY(x, cmp, y) CHECK(x cmp y) << x << "!" #cmp << y << " "
using namespace std;
inline void gen_log(std::ostream &log_stream_, const char *file,
const char *func, int lineno, const char *level,
const int kMaxLen = 20) {
const int len = strlen(file);
std::string time_str;
struct tm tm_time; // Time of creation of LogMessage
time_t timestamp = time(NULL);
localtime_r(×tamp, &tm_time);
struct timeval tv;
gettimeofday(&tv, NULL);
log_stream_ << level << ' ' << 1 + tm_time.tm_mon << '/' << tm_time.tm_mday
<< ' ' << tm_time.tm_hour << ':' << tm_time.tm_min << ':'
<< std::setw(2) << tm_time.tm_sec << '.' << std::setw(3)
<< tv.tv_usec / 1000 << " ";
if (len > kMaxLen) {
log_stream_ << "..." << file + len - kMaxLen << " " << func << ":" << lineno
<< "] ";
} else {
log_stream_ << file << " " << func << ":" << lineno << "] ";
}
}
class LogMessage {
public:
LogMessage(const char *file, const char *func, int lineno,
const char *level = "I") {
if (*level == 'I') {
#if LOG_LEVEL <= 1
gen_log(log_stream_, file, func, lineno, level);
#else
log_stream_ = std::stringstream(nullptr);
#endif
} else if (*level == 'D') {
#if LOG_LEVEL <= 2
gen_log(log_stream_, file, func, lineno, level);
#else
log_stream_ = std::stringstream(nullptr);
#endif
} else if (*level == 'W') {
#if LOG_LEVEL <= 3
gen_log(log_stream_, file, func, lineno, level);
#else
log_stream_ = std::stringstream(nullptr);
#endif
}
}
~LogMessage() {
log_stream_ << '\n';
fprintf(stderr, "%s", log_stream_.str().c_str());
}
std::ostream &stream() { return log_stream_; }
protected:
std::stringstream log_stream_;
LogMessage(const LogMessage &) = delete;
void operator=(const LogMessage &) = delete;
};
Now, I want control all LOG not print out (all).
how should I do it?
IMO, I need send an empty stream, but the content after << how to make it being ignored?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论