Browse Source

[trac3667] Use basic logging facility for output and error messages.

Shawn Routhier 10 years ago
parent
commit
c781d47b07

+ 2 - 2
src/bin/lfc/Makefile.am

@@ -47,8 +47,8 @@ BUILT_SOURCES = lfc_messages.h lfc_messages.cc
 noinst_LTLIBRARIES = liblfc.la
 
 liblfc_la_SOURCES  =
-liblfc_la_SOURCES += lfc_controller.h
-liblfc_la_SOURCES += lfc_controller.cc
+liblfc_la_SOURCES += lfc_controller.h lfc_controller.cc
+liblfc_la_SOURCES += lfc_log.h lfc_log.cc
 
 nodist_liblfc_la_SOURCES = lfc_messages.h lfc_messages.cc
 EXTRA_DIST += lfc_messages.mes

+ 22 - 17
src/bin/lfc/lfc_controller.cc

@@ -13,6 +13,7 @@
 // PERFORMANCE OF THIS SOFTWARE.
 
 #include <lfc/lfc_controller.h>
+#include <lfc/lfc_log.h>
 #include <util/pid_file.h>
 #include <exceptions/exceptions.h>
 #include <dhcpsrv/csv_lease_file4.h>
@@ -31,6 +32,7 @@
 using namespace std;
 using namespace isc::util;
 using namespace isc::dhcp;
+using namespace isc::log;
 
 namespace {
 /// @brief Maximum number of errors to allow when reading leases from the file.
@@ -56,9 +58,14 @@ LFCController::~LFCController() {
 }
 
 void
-LFCController::launch(int argc, char* argv[]) {
+LFCController::launch(int argc, char* argv[], const bool test_mode) {
     bool do_rotate = true;
 
+    // If we aren't running in test mode initialize the logging
+    // system with the defaults.
+    if (!test_mode)
+        initLogger(lfc_app_name_, WARN, 0, NULL, false);
+
     try {
         parseArgs(argc, argv);
     } catch (const InvalidUsage& ex) {
@@ -66,24 +73,28 @@ LFCController::launch(int argc, char* argv[]) {
         throw;  // rethrow it
     }
 
+    // Now that we have parsed the arguments we can
+    // update the logging level.
     if (verbose_) {
-        std::cerr << "Starting lease file cleanup" << std::endl;
+        setDefaultLoggingOutput(verbose_);
     }
 
+    LOG_WARN(lfc_logger, LFC_START_MESSAGE);
+
     // verify we are the only instance
     PIDFile pid_file(pid_file_);
 
     try {
         if (pid_file.check()) {
             // Already running instance, bail out
-            std::cerr << "LFC instance already running" <<  std::endl;
+            LOG_FATAL(lfc_logger, LFC_RUNNING_MESSAGE);
             return;
         }
 
         // create the pid file for this instance
         pid_file.write();
     } catch (const PIDFileError& pid_ex) {
-        std::cerr << pid_ex.what() << std::endl;
+        LOG_FATAL(lfc_logger, LFC_FAILURE_MESSAGE).arg(pid_ex.what());
         return;
     }
 
@@ -92,9 +103,7 @@ LFCController::launch(int argc, char* argv[]) {
     // all we care about is if it exists so that's okay
     CSVFile lf_finish(getFinishFile());
     if (!lf_finish.exists()) {
-        if (verbose_) {
-            std::cerr << "LFC Processing files" << std::endl;
-        }
+        LOG_INFO(lfc_logger, LFC_PROCESSING_MESSAGE);
 
         try {
             if (getProtocolVersion() == 4) {
@@ -105,7 +114,7 @@ LFCController::launch(int argc, char* argv[]) {
         } catch (const isc::Exception& proc_ex) {
             // We don't want to do the cleanup but do want to get rid of the pid
             do_rotate = false;
-            std::cerr << "Processing failed: " << proc_ex.what() << std::endl;
+            LOG_FATAL(lfc_logger, LFC_FAILURE_MESSAGE).arg(proc_ex.what());
         }
     }
 
@@ -114,14 +123,12 @@ LFCController::launch(int argc, char* argv[]) {
     // we don't want to return after the catch as we
     // still need to cleanup the pid file
     if (do_rotate) {
-        if (verbose_) {
-            std::cerr << "LFC cleaning files" << std::endl;
-        }
+        LOG_INFO(lfc_logger, LFC_ROTATING_MESSAGE);
 
         try {
             fileRotate();
         } catch (const RunTimeFail& run_ex) {
-            std::cerr << run_ex.what() << std::endl;
+          LOG_FATAL(lfc_logger, LFC_FAILURE_MESSAGE).arg(run_ex.what());
         }
     }
 
@@ -129,12 +136,10 @@ LFCController::launch(int argc, char* argv[]) {
     try {
         pid_file.deleteFile();
     } catch (const PIDFileError& pid_ex) {
-        std::cerr << pid_ex.what() << std::endl;
+          LOG_FATAL(lfc_logger, LFC_FAILURE_MESSAGE).arg(pid_ex.what());
     }
 
-    if (verbose_) {
-        std::cerr << "LFC complete" << std::endl;
-    }
+    LOG_WARN(lfc_logger, LFC_COMPLETE_MESSAGE);
 }
 
 void
@@ -272,7 +277,7 @@ LFCController::parseArgs(int argc, char* argv[]) {
 
     // If verbose is set echo the input information
     if (verbose_) {
-        std::cerr << "Protocol version:    DHCPv" << protocol_version_ << std::endl
+        std::cout << "Protocol version:    DHCPv" << protocol_version_ << std::endl
                   << "Previous or ex lease file: " << previous_file_ << std::endl
                   << "Copy lease file:           " << copy_file_ << std::endl
                   << "Output lease file:         " << output_file_ << std::endl

+ 5 - 1
src/bin/lfc/lfc_controller.h

@@ -77,9 +77,13 @@ public:
     ///
     /// @param argc Number of strings in the @c argv array.
     /// @param argv Array of arguments passed in via the program's main function.
+    /// @param test_mode is a bool value which indicates if @c launch
+    /// should be run in the test mode (if true).  This parameter doesn't
+    /// have a default value to force test implementers to enable test
+    /// mode explicitly.
     ///
     /// @throw InvalidUsage if the command line parameters are invalid.
-    void launch(int argc, char* argv[]);
+    void launch(int argc, char* argv[], const bool test_mode);
 
     /// @brief Process the command line arguments.
     ///

+ 26 - 0
src/bin/lfc/lfc_log.cc

@@ -0,0 +1,26 @@
+// Copyright (C) 2015  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 top-level component of kea-lfc.
+
+#include <lfc/lfc_log.h>
+
+namespace isc {
+namespace lfc {
+
+/// @brief Defines the logger used within LFC.
+isc::log::Logger lfc_logger("DhcpLFC");
+
+} // namespace lfc
+} // namespace isc

+ 32 - 0
src/bin/lfc/lfc_log.h

@@ -0,0 +1,32 @@
+// Copyright (C) 2015  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.
+
+#ifndef LFC_LOG_H
+#define LFC_LOG_H
+
+#include <log/logger_support.h>
+#include <log/macros.h>
+#include <lfc/lfc_messages.h>
+
+namespace isc {
+namespace lfc {
+
+/// Define the logger for the "lfc" logging.
+extern isc::log::Logger lfc_logger;
+
+
+} // namespace lfc
+} // namespace isc
+
+#endif // LFC_LOG_H

+ 23 - 2
src/bin/lfc/lfc_messages.mes

@@ -13,5 +13,26 @@
 # PERFORMANCE OF THIS SOFTWARE.
 
 $NAMESPACE isc::lfc
-% LFC_TEST_MESSAGE test messages
-This is a test and placeholder debug message
+% LFC_START_MESSAGE Starting lease file cleanup
+This message is issued as the LFC process starts.
+
+% LFC_COMPLETE_MESSAGE LFC complete
+This message is issued when the LFC process completes.  It does not
+indicate that the process was successful only that it has finished.
+
+% LFC_RUNNING_MESSAGE LFC instance already running
+This message is issued if LFC detects that a previous copy of LFC
+may still be running via the PID check.
+
+% LFC_PROCESSING_MESSAGE LFC Processing files
+This message is issued just before LFC starts processing the 
+lease files.
+
+% LFC_ROTATING_MESSAGE LFC rotating files
+This message is issued just before LFC starts rotating the
+lease files - removing the old and replacing them with the new.
+
+% LFC_FAILURE_MESSAGE LFC failed: %1
+This message is issued if LFC detected a failure when trying
+to manipulate the files.  It includes a more specifc error string.
+

+ 2 - 2
src/bin/lfc/main.cc

@@ -19,8 +19,8 @@
 #include <config.h>
 #include <iostream>
 
-using namespace isc::lfc;
 using namespace std;
+using namespace isc::lfc;
 
 /// This file contains the entry point (main() function) for the
 /// standard LFC process, kea-lfc, component of the Kea software suite.
@@ -36,7 +36,7 @@ int main(int argc, char* argv[]) {
     // Exit program with the controller's return code.
     try  {
         // 'false' value disables test mode.
-        lfc_controller.launch(argc, argv);
+        lfc_controller.launch(argc, argv, false);
     } catch (const isc::Exception& ex) {
         std::cerr << "Service failed: " << ex.what() << std::endl;
         ret = EXIT_FAILURE;

+ 19 - 12
src/bin/lfc/tests/lfc_controller_unittests.cc

@@ -75,7 +75,7 @@ public:
     }
 
 protected:
-    /// @brief Sets up the file names and header string and removes 
+    /// @brief Sets up the file names and header string and removes
     /// any old test files before the test
     virtual void SetUp() {
         // set up the test files we need
@@ -105,6 +105,13 @@ protected:
         removeTestFile();
     }
 
+    /// @Wrapper to invoke the controller's launch method  Please refer to
+    /// lfcController::launch for details.  This is wrapped to provide
+    /// a single place to update the test_mode throughout the file.
+    void launch(LFCController lfc_controller, int argc, char* argv[]) {
+        lfc_controller.launch(argc, argv, true);
+    }
+
 private:
 };
 
@@ -348,7 +355,7 @@ TEST_F(LFCControllerTest, fileRotate) {
     writeFile(istr_, "11");
     writeFile(fstr_, "12");
 
-    lfc_controller_launch.launch(argc, argv);
+    launch(lfc_controller_launch, argc, argv);
 
     // verify finish is now previous and no temp files or pid remain.
     EXPECT_EQ(readFile(xstr_), "12");
@@ -426,7 +433,7 @@ TEST_F(LFCControllerTest, launch4) {
     writeFile(istr_, test_str);
 
     // Run the cleanup
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // Compare the results, we expect the last lease for each ip
     // except for C which was invalid and D which has expired.
@@ -445,7 +452,7 @@ TEST_F(LFCControllerTest, launch4) {
     // No copy file
 
     // Run the cleanup
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // Compare the results, we expect the last lease for each ip
     // except for C which was invalid and D which has expired.
@@ -464,7 +471,7 @@ TEST_F(LFCControllerTest, launch4) {
     writeFile(istr_, test_str);
 
     // Run the cleanup
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // Compare the results, we expect the last lease for each ip
     // except for C which was invalid and D which has expired.
@@ -481,7 +488,7 @@ TEST_F(LFCControllerTest, launch4) {
     // No copy file
 
     // Run the cleanup
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // Compare the results, we expect a header and no leaes.
     // We also verify none of the temp or pid files remain.
@@ -502,7 +509,7 @@ TEST_F(LFCControllerTest, launch4) {
 
     // Run the cleanup, the file should fail but we should
     // catch the error and properly cleanup.
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // And we shouldn't have deleted the previous file.
     // We also verify none of the temp or pid files remain.
@@ -581,7 +588,7 @@ TEST_F(LFCControllerTest, launch6) {
     writeFile(istr_, test_str);
 
     // Run the cleanup
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // Compare the results, we expect the last lease for each ip
     // except for A which has expired.
@@ -600,7 +607,7 @@ TEST_F(LFCControllerTest, launch6) {
     // No copy file
 
     // Run the cleanup
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // Compare the results, we expect the last lease for each ip.
     // We also verify none of the temp or pid files remain.
@@ -618,7 +625,7 @@ TEST_F(LFCControllerTest, launch6) {
     writeFile(istr_, test_str);
 
     // Run the cleanup
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // Compare the results, we expect the last lease for each ip.
     // We also verify none of the temp or pid files remain.
@@ -634,7 +641,7 @@ TEST_F(LFCControllerTest, launch6) {
     // No copy file
 
     // Run the cleanup
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // Compare the results, we expect a header and no leases.
     // We also verify none of the temp or pid files remain.
@@ -655,7 +662,7 @@ TEST_F(LFCControllerTest, launch6) {
 
     // Run the cleanup, the file should fail but we should
     // catch the error and properly cleanup.
-    lfc_controller.launch(argc, argv);
+    launch(lfc_controller, argc, argv);
 
     // And we shouldn't have deleted the previous file.
     // We also verify none of the temp or pid files remain.