Parcourir la source

[trac745] Move logging initialization code to logger directory

... the logical place for it.
Stephen Morris il y a 14 ans
Parent
commit
5b4feea19e

+ 72 - 0
src/lib/log/logger_support.cc

@@ -25,6 +25,7 @@
 /// the logging parameters from the configuration database.
 
 #include <algorithm>
+#include <iostream>
 #include <string>
 #include <vector>
 #include <boost/lexical_cast.hpp>
@@ -128,5 +129,76 @@ initLogger(const string& root, isc::log::Severity severity, int dbglevel,
     }
 }
 
+/// Logger Run-Time Initialization via Environment Variables
+void initLogger() {
+
+    // Root logger name is defined by the environment variable B10_LOGGER_ROOT.
+    // If not present, the name is "b10root".
+    const char* DEFAULT_ROOT = "b10root";
+    const char* root = getenv("B10_LOGGER_ROOT");
+    if (! root) {
+        root = DEFAULT_ROOT;
+    }
+
+    // Set the logging severity.  The environment variable is
+    // B10_LOGGER_SEVERITY, and can be one of "DEBUG", "INFO", "WARN", "ERROR"
+    // of "FATAL".  Note that the string must be in upper case with no leading
+    // of trailing blanks.
+    isc::log::Severity severity = isc::log::DEFAULT;
+    const char* sev_char = getenv("B10_LOGGER_SEVERITY");
+    if (sev_char) {
+        string sev_string(sev_char);
+        if (sev_string == "DEBUG") {
+            severity = isc::log::DEBUG;
+        } else if (sev_string == "INFO") {
+            severity = isc::log::INFO;
+        } else if (sev_string == "WARN") {
+            severity = isc::log::WARN;
+        } else if (sev_string == "ERROR") {
+            severity = isc::log::ERROR;
+        } else if (sev_string == "FATAL") {
+            severity = isc::log::FATAL;
+        } else {
+            std::cerr << "**ERROR** unrecognised logger severity of '"
+                      << sev_string << "' - default severity will be used\n";
+        }
+    }
+
+    // If the severity is debug, get the debug level (environment variable
+    // B10_LOGGER_DBGLEVEL), which should be in the range 0 to 99.
+    int dbglevel = 0;
+    if (severity == isc::log::DEBUG) {
+        const char* dbg_char = getenv("B10_LOGGER_DBGLEVEL");
+        if (dbg_char) {
+            int level = 0;
+            try {
+                level = boost::lexical_cast<int>(dbg_char);
+                if (level < MIN_DEBUG_LEVEL) {
+                    std::cerr << "**ERROR** debug level of " << level
+                              << " is invalid - a value of " << MIN_DEBUG_LEVEL
+                              << " will be used\n";
+                    level = MIN_DEBUG_LEVEL;
+                } else if (level > MAX_DEBUG_LEVEL) {
+                    std::cerr << "**ERROR** debug level of " << level
+                              << " is invalid - a value of " << MAX_DEBUG_LEVEL
+                              << " will be used\n";
+                    level = MAX_DEBUG_LEVEL;
+                }
+            } catch (...) {
+                // Error, but not fatal to the test
+                std::cerr << "**ERROR** Unable to translate "
+                             "B10_LOGGER_DBGLEVEL - a value of 0 will be used\n";
+            }
+            dbglevel = level;
+        }
+    }
+
+    /// Set the local message file
+    const char* localfile = getenv("B10_LOGGER_LOCALMSG");
+
+    // Initialize logging
+    initLogger(root, severity, dbglevel, localfile);
+}
+
 } // namespace log
 } // namespace isc

+ 31 - 0
src/lib/log/logger_support.h

@@ -39,6 +39,37 @@ namespace log {
 void initLogger(const std::string& root, isc::log::Severity severity,
     int dbglevel, const char* file);
 
+
+/// \brief Run-Time Initialization from Environment
+///
+/// Performs run-time initialization of the logger via the setting of
+/// environment variables.  These are:
+///
+/// B10_LOGGER_ROOT
+/// Name of the root logger.  If not given, the string "b10root" will be used.
+///
+/// B10_LOGGER_SEVERITY
+/// Severity of messages that will be logged.  This must be one of the strings
+/// "DEBUG", "INFO", "WARN", "ERROR", "FATAL". (Must be upper case and must
+/// not contain leading or trailing spaces.)  If not specified (or if
+/// specified but incorrect), the default for the logging system will be used
+/// (currently INFO).
+///
+/// B10_LOGGER_DBGLEVEL
+/// Ignored if the level is not DEBUG, this should be a number between 0 and
+/// 99 indicating the logging severity.  The default is 0.  If outside these
+/// limits or if not a number, a value of 0 is used.
+///
+/// B10_LOGGER_LOCALMSG
+/// If defined, the path specification of a file that contains message
+/// definitions replacing ones in the default dictionary.
+///
+/// Any errors in the settings cause messages to be output to stderr.
+///
+/// This function is most likely to be called from unit test programs.
+
+void initLogger();
+
 } // namespace log
 } // namespace isc
 

+ 26 - 0
src/lib/nsas/nsas_log.cc

@@ -0,0 +1,26 @@
+// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+/// Defines the logger used by the NSAS
+
+#include "nsas/nsas_log.h"
+
+namespace isc {
+namespace nsas {
+
+isc::log::Logger nsas_logger("nsas");
+
+} // namespace nsas
+} // namespace isc
+

src/lib/nsas/nsas_levels.h → src/lib/nsas/nsas_log.h


+ 1 - 56
src/lib/nsas/tests/run_unittests.cc

@@ -25,66 +25,11 @@
 
 using namespace std;
 
-// Initialize the logging.
-void init_logging() {
-
-    const char* DEFAULT_ROOT = "b10root";
-
-    // Root logger name is defined by the environment variable B10_LOGGER_ROOT.
-    // If not present, the name is "b10root".
-    const char* root = getenv("B10_LOGGER_ROOT");
-    if (! root) {
-        root = DEFAULT_ROOT;
-    }
-
-    // Set the logging severity.  The environment variable is
-    // B10_LOGGER_SEVERITY, and can be one of "DEBUG", "INFO", "WARN", "ERROR"
-    // of "FATAL".  Note that the string must be in upper case with no leading
-    // of trailing blanks.
-    isc::log::Severity severity = isc::log::DEFAULT;
-    const char* sev_char = getenv("B10_LOGGER_SEVERITY");
-    if (sev_char) {
-        string sev_string(sev_char);
-        if (sev_string == "DEBUG") {
-            severity = isc::log::DEBUG;
-        } else if (sev_string == "INFO") {
-            severity = isc::log::INFO;
-        } else if (sev_string == "WARN") {
-            severity = isc::log::WARN;
-        } else if (sev_string == "ERROR") {
-            severity = isc::log::ERROR;
-        } else if (sev_string == "FATAL") {
-            severity = isc::log::FATAL;
-        }
-    }
-
-    // If the severity is debug, get the debug level (environment variable
-    // B10_LOGGER_DBGLEVEL), which should be in the range 0 to 99.
-    int dbglevel = 0;
-    if (severity == isc::log::DEBUG) {
-        const char* dbg_char = getenv("B10_LOGGER_DBGLEVEL");
-        if (dbg_char) {
-            int level = 0;
-            try {
-                level = boost::lexical_cast<int>(dbg_char);
-            } catch (...) {
-                // Error, but not fatal to the test
-                std::cerr << "***ERROR*** Unable to translate "
-                             "B10_LOGGER_DBGLEVEL \n";
-            }
-            dbglevel = level;
-        }
-    }
-
-    // Initialize logging
-    isc::log::initLogger(root, severity, dbglevel, NULL);
-}
-
 int
 main(int argc, char* argv[]) {
     ::testing::InitGoogleTest(&argc, argv);
 
-    init_logging();
+    isc::log::initLogger();
 
     return (RUN_ALL_TESTS());
 }