]> granicus.if.org Git - pdns/commitdiff
logger: Allow logging with the severity prefix
authorPieter Lexis <pieter.lexis@powerdns.com>
Thu, 12 Apr 2018 15:06:42 +0000 (17:06 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Fri, 13 Apr 2018 08:51:02 +0000 (10:51 +0200)
pdns/logger.cc
pdns/logger.hh

index 8bbf03307ca9135d32081debec8ad79e7b48d87f..1766d35ae9a74aa52218bdbd2ead3a2511271f19 100644 (file)
@@ -51,9 +51,42 @@ void Logger::log(const string &msg, Urgency u)
       strftime(buffer,sizeof(buffer),"%b %d %H:%M:%S ", &tm);
     }
 
+    string prefix;
+    if (d_prefixed) {
+      switch(u) {
+        case All:
+          prefix = "[all] ";
+          break;
+        case Alert:
+          prefix = "[ALERT] ";
+          break;
+        case Critical:
+          prefix = "[CRITICAL] ";
+          break;
+        case Error:
+          prefix = "[ERROR] ";
+          break;
+        case Warning:
+          prefix = "[WARNING] ";
+          break;
+        case Notice:
+          prefix = "[NOTICE] ";
+          break;
+        case Info:
+          prefix = "[INFO] ";
+          break;
+        case Debug:
+          prefix = "[DEBUG] ";
+          break;
+        case None:
+          prefix = "[none] ";
+          break;
+      }
+    }
+
     static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
     Lock l(&m); // the C++-2011 spec says we need this, and OSX actually does
-    clog << string(buffer) + msg <<endl;
+    clog << string(buffer) + prefix + msg <<endl;
 #ifndef RECURSOR
     mustAccount=true;
 #endif
index e08fa0d548140138eb30ba1058da906d6bb094e9..3fb97ee0fcf9ec8f079b3e6cd5d5a5929100efce 100644 (file)
@@ -64,6 +64,10 @@ public:
     d_timestamps = t;
   }
 
+  void setPrefixed(bool p) {
+    d_prefixed = p;
+  }
+
   //! Log to a file.
   void toFile( const string & filename );
   
@@ -110,6 +114,7 @@ private:
   bool opened;
   bool d_disableSyslog;
   bool d_timestamps{true};
+  bool d_prefixed{false};
   static pthread_once_t s_once;
   static pthread_key_t g_loggerKey;
 };