浏览代码

Drop level+message log writer.
Drop header from c_logger.

Sam Jaffe 6 年之前
父节点
当前提交
3308eacad1

+ 0 - 1
include/logger/c_logger.h

@@ -81,7 +81,6 @@ namespace logging {
   private:
     void vlognf(log_level, size_t, char const*, va_list);
     void log(log_level, std::string const&);
-    std::string get_header(log_level level);
     
   private:
     log_level min_level_;

+ 1 - 2
include/logger/logger_impl.h

@@ -7,13 +7,12 @@ namespace logging {
   public:
     virtual ~logger_impl() = default;
     virtual void write(logpacket const & pkt) = 0;
-    virtual void write(log_level level, std::string const& msg) = 0;
     virtual void flush() = 0;
     
     bool should_log(log_level ll) const {
       return ll >= min_log_level;
     }
-
+    
   protected:
     log_level min_log_level;
   };

+ 1 - 1
logger.xcodeproj/xcuserdata/samjaffe.xcuserdatad/xcschemes/xcschememanagement.plist

@@ -12,7 +12,7 @@
 		<key>logging.xcscheme</key>
 		<dict>
 			<key>isShown</key>
-			<false/>
+			<true/>
 			<key>orderHint</key>
 			<integer>3</integer>
 		</dict>

+ 5 - 12
src/c_logger.cxx

@@ -12,7 +12,8 @@
 
 using namespace logging;
 
-c_logger::c_logger(std::string const & name, std::shared_ptr<logger_impl> impl)
+c_logger::c_logger(std::string const & name,
+                   std::shared_ptr<logger_impl> impl)
 : min_level_(LDEBUG), logger_name_(name), impl_(impl)
 {
 }
@@ -21,28 +22,20 @@ c_logger::~c_logger() {
   if (impl_) impl_->flush();
 }
 
-std::string c_logger::get_header(log_level level) {
-  std::string ts = timestamp();
-  char data[64];
-  snprintf(data, sizeof(data), "[%-5s] %s %s - ",
-           level_header(level), logger_name_.c_str(), ts.c_str());
-  return data;
-}
-
 void c_logger::vlognf(log_level level, size_t num_bytes, char const* fmt,
-                    va_list args) {
+                      va_list args) {
   if (level < min_level_ || !impl_->should_log(level)) return;
   const std::unique_ptr<char[]> data(new char[num_bytes]);
   int n = vsnprintf(data.get(), num_bytes, fmt, args);
   if (n >= 0) {
     data[num_bytes-1] = '\0';
-    log(level, get_header(level) + data.get() + "\n");
+    impl_->write({ now(), level, {}, logger_name_.c_str(), data.get() });
   }
 }
 
 void c_logger::log(log_level level, std::string const& msg) {
   if (level < min_level_ || !impl_->should_log(level)) return;
-  impl_->write(level, msg);
+  impl_->write({ now(), level, {}, logger_name_.c_str(), msg });
 }
 
 void c_logger::flush() {

+ 0 - 3
src/log_manager.cxx

@@ -55,9 +55,6 @@ struct compound_logger : public logger_impl {
   void write(logpacket const & pkt) override {
     for (auto & log : values) { log->write(pkt); }
   }
-  void write(log_level l, std::string const & msg) override {
-    for (auto & log : values) { log->write(l, msg); }
-  }
   void flush() override {
     for (auto & log : values) { log->flush(); }
   }

+ 0 - 5
src/loggers/console_logger.cxx

@@ -32,7 +32,6 @@ public:
   explicit console_logger(std::ostream& os);
   
   void write(logpacket const & pkt) override;
-  void write(log_level level, std::string const& msg) override;
   
   void flush() override;
 private:
@@ -56,10 +55,6 @@ void console_logger::write(logpacket const & pkt) {
   out_ << pkt.message;
 }
 
-void console_logger::write(log_level level, std::string const& msg) {
-  out_ << msg;
-}
-
 void console_logger::flush() {
   out_.flush();
 }

+ 0 - 5
src/loggers/file_logger.cxx

@@ -22,7 +22,6 @@ public:
   
   explicit file_logger(const std::string& fname);
   
-  void write(log_level level, std::string const& msg) override;
   void flush() override;
 private:
   std::ofstream out_;
@@ -35,10 +34,6 @@ std::shared_ptr<logger_impl> file_logger::create(properties const&) {
 file_logger::file_logger(const std::string& fname)
 : out_(fname) {}
 
-void file_logger::write(log_level level, std::string const& msg) {
-  out_ << msg;
-}
-
 void file_logger::flush() {
   out_.flush();
 }

+ 8 - 8
test/c_logger_test.cxx

@@ -32,32 +32,32 @@ TEST_F(CLoggerTest, FlushesOnFlushCall) {
 
 TEST_F(CLoggerTest, LogsWithFmtCode) {
   using testing::_;
-  using testing::HasSubstr;
+  using testing::Field;
   // TODO: Eq
-  EXPECT_CALL(*pimpl, write(_, HasSubstr("5"))).Times(1);
+  EXPECT_CALL(*pimpl, write(Field(&logpacket::message, "5"))).Times(1);
   t_logger("", pimpl).errorf("%d", 5);
 }
 
 // TODO: This is wrong
 TEST_F(CLoggerTest, FmtLogHasNameInHeader) {
   using testing::_;
-  using testing::HasSubstr;
-  EXPECT_CALL(*pimpl, write(_, HasSubstr("TEST"))).Times(1);
+  using testing::Field;
+  EXPECT_CALL(*pimpl, write(Field(&logpacket::logger, "TEST"))).Times(1);
   t_logger("TEST", pimpl).errorf("%d", 5);
 }
 
 // TODO: This is wrong
 TEST_F(CLoggerTest, FmtLogHasLevelInHeader) {
   using testing::_;
-  using testing::HasSubstr;
-  EXPECT_CALL(*pimpl, write(_, HasSubstr("[ERROR]"))).Times(1);
+  using testing::Field;
+  EXPECT_CALL(*pimpl, write(Field(&logpacket::level, LERROR))).Times(1);
   t_logger("TEST", pimpl).errorf("%d", 5);
 }
 
 TEST_F(CLoggerTest, LogsRawData) {
   using testing::_;
-  using testing::HasSubstr;
-  EXPECT_CALL(*pimpl, write(_, "5")).Times(1);
+  using testing::Field;
+  EXPECT_CALL(*pimpl, write(Field(&logpacket::message, "5"))).Times(1);
   t_logger("", pimpl).error("5");
 }
 

+ 4 - 2
test/log_manager_test.cxx

@@ -54,8 +54,9 @@ TEST_F(LogManagerTest, CanFetchInjectedMock) {
   
   using ::testing::_;
   using ::testing::AnyNumber;
+  using ::testing::Field;
   EXPECT_CALL(*logger, flush()).Times(AnyNumber());
-  EXPECT_CALL(*logger, write(_, "TEST MESSAGE"));
+  EXPECT_CALL(*logger, write(Field(&logpacket::message, "TEST MESSAGE")));
 
   c_logger l = mgr.c_get();
   l.error("TEST MESSAGE");
@@ -67,8 +68,9 @@ TEST_F(LogManagerTest, MultiplexMockLogsToMultipleImpls) {
   
   using ::testing::_;
   using ::testing::AnyNumber;
+  using ::testing::Field;
   EXPECT_CALL(*logger, flush()).Times(AnyNumber());
-  EXPECT_CALL(*logger, write(_, "TEST MESSAGE")).Times(2);
+  EXPECT_CALL(*logger, write(Field(&logpacket::message, "TEST MESSAGE"))).Times(2);
   
   c_logger l = mgr.c_get();
   l.error("TEST MESSAGE");

+ 0 - 1
test/mock_logger.h

@@ -34,7 +34,6 @@ struct MockLoggerImpl : public logging::logger_impl {
   void SetLogLevel(logging::log_level ll) { min_log_level = ll; }
   MOCK_METHOD0(flush, void());
   MOCK_METHOD1(write, void(logging::logpacket const &));
-  MOCK_METHOD2(write, void(logging::log_level, std::string const &));
 };
 
 struct LoggerTest : public testing::Test {