From 0e1891e5bb592762dbf0a1aedd57f488f77f4349 Mon Sep 17 00:00:00 2001 From: liuruilong Date: Thu, 17 May 2018 15:50:33 +0800 Subject: [PATCH] format files --- src/common/log.h | 215 ++++++++++++++++++------------------ test/unit-test/test_log.cpp | 18 +-- 2 files changed, 118 insertions(+), 115 deletions(-) diff --git a/src/common/log.h b/src/common/log.h index 4fae42893b..4ee17df1f4 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -27,125 +27,128 @@ SOFTWARE. namespace paddle_mobile { -enum LogLevel { - kNO_LOG, - kLOG_ERROR, - kLOG_WARNING, - kLOG_INFO, - kLOG_DEBUG, - kLOG_DEBUG1, - kLOG_DEBUG2, - kLOG_DEBUG3, - kLOG_DEBUG4 -}; - -// log level -static LogLevel log_level = kLOG_DEBUG4; - -static std::vector logs{"NO", "ERROR ", "WARNING", - "INFO ", "DEBUG ", "DEBUG1 ", - "DEBUG2 ", "DEBUG3 ", "DEBUG4 "}; - -struct ToLog; - -struct Print { - friend struct ToLog; - template Print &operator<<(T const &value) { - buffer_ << value; - return *this; - } - -private: - void print(LogLevel level) { - buffer_ << std::endl; - if (level == kLOG_ERROR) { - std::cerr << buffer_.str(); - } else { - std::cout << buffer_.str(); - } - } - std::ostringstream buffer_; -}; - -struct ToLog { - ToLog(LogLevel level = kLOG_DEBUG, const std::string &info = "") - : level_(level) { - unsigned blanks = - (unsigned)(level > kLOG_DEBUG ? (level - kLOG_DEBUG) * 4 : 1); - printer_ << logs[level] << " " << info << ":" << std::string(blanks, ' '); - } - - template ToLog &operator<<(T const &value) { - printer_ << value; - return *this; - } - - ~ToLog() { printer_.print(level_); } - -private: - LogLevel level_; - Print printer_; -}; + enum LogLevel { + kNO_LOG, + kLOG_ERROR, + kLOG_WARNING, + kLOG_INFO, + kLOG_DEBUG, + kLOG_DEBUG1, + kLOG_DEBUG2, + kLOG_DEBUG3, + kLOG_DEBUG4 + }; + + // log level + static LogLevel log_level = kLOG_DEBUG4; + + static std::vector logs{"NO", "ERROR ", "WARNING", + "INFO ", "DEBUG ", "DEBUG1 ", + "DEBUG2 ", "DEBUG3 ", "DEBUG4 "}; + + struct ToLog; + + struct Print { + friend struct ToLog; + template Print &operator<<(T const &value) { + buffer_ << value; + return *this; + } + + private: + void print(LogLevel level) { + buffer_ << std::endl; + if (level == kLOG_ERROR) { + std::cerr << buffer_.str(); + } else { + std::cout << buffer_.str(); + } + } + std::ostringstream buffer_; + }; + + struct ToLog { + ToLog(LogLevel level = kLOG_DEBUG, const std::string &info = "") + : level_(level) { + unsigned blanks = + (unsigned)(level > kLOG_DEBUG ? (level - kLOG_DEBUG) * 4 : 1); + printer_ << logs[level] << " " << info << ":" + << std::string(blanks, ' '); + } + + template ToLog &operator<<(T const &value) { + printer_ << value; + return *this; + } + + ~ToLog() { printer_.print(level_); } + + private: + LogLevel level_; + Print printer_; + }; #define LOG(level) \ - if (level > paddle_mobile::log_level) { \ - } else \ - paddle_mobile::ToLog(level, \ - (std::stringstream() \ - << "[file: " << (strrchr(__FILE__, '/') \ - ? (strrchr(__FILE__, '/') + 1) \ - : __FILE__) \ - << "] [line: " << __LINE__ << "] ") \ - .str()) + if (level > paddle_mobile::log_level) { \ + } else \ + paddle_mobile::ToLog( \ + level, \ + (std::stringstream() \ + << "[file: " \ + << (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__) \ + << "] [line: " << __LINE__ << "] ") \ + .str()) #define DLOG \ - paddle_mobile::ToLog(paddle_mobile::kLOG_DEBUG, \ - (std::stringstream() \ - << "[file: " << (strrchr(__FILE__, '/') \ - ? (strrchr(__FILE__, '/') + 1) \ - : __FILE__) \ - << "] [line: " << __LINE__ << "] ") \ - .str()) + paddle_mobile::ToLog( \ + paddle_mobile::kLOG_DEBUG, \ + (std::stringstream() \ + << "[file: " \ + << (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__) \ + << "] [line: " << __LINE__ << "] ") \ + .str()) } #else namespace paddle_mobile { -enum LogLevel { - kNO_LOG, - kLOG_ERROR, - kLOG_WARNING, - kLOG_INFO, - kLOG_DEBUG, - kLOG_DEBUG1, - kLOG_DEBUG2, - kLOG_DEBUG3, - kLOG_DEBUG4 -}; - -struct ToLog; -struct Print { - friend struct ToLog; - template Print &operator<<(T const &value) {} - -private: -}; - -struct ToLog { - ToLog(LogLevel level) {} - - template ToLog &operator<<(T const &value) { return *this; } -}; + enum LogLevel { + kNO_LOG, + kLOG_ERROR, + kLOG_WARNING, + kLOG_INFO, + kLOG_DEBUG, + kLOG_DEBUG1, + kLOG_DEBUG2, + kLOG_DEBUG3, + kLOG_DEBUG4 + }; + + struct ToLog; + struct Print { + friend struct ToLog; + template Print &operator<<(T const &value) {} + + private: + }; + + struct ToLog { + ToLog(LogLevel level) {} + + template ToLog &operator<<(T const &value) { + return *this; + } + }; #define LOG(level) \ - if (true) { \ - } else \ - paddle_mobile::ToLog(level) + if (true) { \ + } else \ + paddle_mobile::ToLog(level) #define DLOG \ - if (true) { \ - } else \ - paddle_mobile::ToLog(paddle_mobile::kLOG_DEBUG) + if (true) { \ + } else \ + paddle_mobile::ToLog(paddle_mobile::kLOG_DEBUG) } #endif diff --git a/test/unit-test/test_log.cpp b/test/unit-test/test_log.cpp index ea0b9f0a27..6e366363bc 100644 --- a/test/unit-test/test_log.cpp +++ b/test/unit-test/test_log.cpp @@ -19,13 +19,13 @@ SOFTWARE. #include "common/log.h" int main() { - LOG(paddle_mobile::kLOG_DEBUG) << "test debug" - << " next log"; - LOG(paddle_mobile::kLOG_DEBUG1) << "test debug1" - << " next log"; - LOG(paddle_mobile::kLOG_DEBUG2) << "test debug2" - << " next log"; - DLOG << "test DLOG"; - LOG(paddle_mobile::kLOG_ERROR) << " error occur !"; - return 0; + LOG(paddle_mobile::kLOG_DEBUG) << "test debug" + << " next log"; + LOG(paddle_mobile::kLOG_DEBUG1) << "test debug1" + << " next log"; + LOG(paddle_mobile::kLOG_DEBUG2) << "test debug2" + << " next log"; + DLOG << "test DLOG"; + LOG(paddle_mobile::kLOG_ERROR) << " error occur !"; + return 0; } -- GitLab