Browse Source

[trac747] Support for logging exceptions

Just that we don't need to write .arg(e.what()), .arg(e) is enough.
Michal 'vorner' Vaner 14 years ago
parent
commit
421d19914a
2 changed files with 23 additions and 0 deletions
  1. 8 0
      src/lib/log/log_formatter.h
  2. 15 0
      src/lib/log/tests/log_formatter_unittest.cc

+ 8 - 0
src/lib/log/log_formatter.h

@@ -180,6 +180,14 @@ public:
         return (*this);
     }
 
+    Formatter& arg(const std::exception& e) {
+        if (logger_) {
+            return (arg(e.what()));
+        } else {
+            return (*this);
+        }
+    }
+
 };
 
 }

+ 15 - 0
src/lib/log/tests/log_formatter_unittest.cc

@@ -123,4 +123,19 @@ TEST_F(FormatterTest, noRecurse) {
     EXPECT_EQ("%1 %1", outputs[0].second);
 }
 
+// Test it can accept exceptions (which don't have a default conversion
+// to string by themself)
+TEST_F(FormatterTest, exception) {
+    class Ex : public std::exception {
+    public:
+        virtual const char* what() const throw() {
+            return "Exception test";
+        }
+    };
+    Formatter(isc::log::INFO, s("%1"), this).arg(Ex());
+    ASSERT_EQ(1, outputs.size());
+    EXPECT_EQ(isc::log::INFO, outputs[0].first);
+    EXPECT_EQ("Exception test", outputs[0].second);
+}
+
 }