Browse Source

[3186] Adding logging to user_chk hook libraryU

Replaced couts with isc::log mechanisms in user_chk DHCP hook library.
Thomas Markwalder 11 years ago
parent
commit
a27c2a6a18

+ 8 - 10
src/hooks/dhcp/user_chk/Makefile.am

@@ -11,36 +11,34 @@ AM_CXXFLAGS  = $(B10_CXXFLAGS)
 AM_CXXFLAGS += $(WARNING_NO_MISSING_FIELD_INITIALIZERS_CFLAG)
 
 # Define rule to build logging source files from message file
-# hooks_messages.h hooks_messages.cc: s-messages
+user_chk_messages.h user_chk_messages.cc: s-messages
 
-# s-messages: hooks_messages.mes
-# 	$(top_builddir)/src/lib/log/compiler/message $(top_srcdir)/src/lib/hooks/hooks_messages.mes
-# 	touch $@
+s-messages: user_chk_messages.mes
+	$(top_builddir)/src/lib/log/compiler/message $(top_srcdir)/src/hooks/dhcp/user_chk/user_chk_messages.mes
+	touch $@
 
 # Tell automake that the message files are built as part of the build process
 # (so that they are built before the main library is built).
-# BUILT_SOURCES = hooks_messages.h hooks_messages.cc
-BUILT_SOURCES =
+BUILT_SOURCES = user_chk_messages.h user_chk_messages.cc
 
 # Ensure that the message file is included in the distribution
 EXTRA_DIST =
 
 # Get rid of generated message files on a clean
-# CLEANFILES = *.gcno *.gcda hooks_messages.h hooks_messages.cc s-messages
-CLEANFILES = *.gcno *.gcda
+CLEANFILES = *.gcno *.gcda user_chk_messages.h user_chk_messages.cc s-messages
 
 lib_LTLIBRARIES = libdhcp_user_chk.la
 libdhcp_user_chk_la_SOURCES  =
 libdhcp_user_chk_la_SOURCES += load_unload.cc
 libdhcp_user_chk_la_SOURCES += subnet_select_co.cc
 libdhcp_user_chk_la_SOURCES += user.cc user.h
+libdhcp_user_chk_la_SOURCES += user_chk_log.cc user_chk_log.h
 libdhcp_user_chk_la_SOURCES += user_data_source.cc user_data_source.h
 libdhcp_user_chk_la_SOURCES += user_file.cc user_file.h
 libdhcp_user_chk_la_SOURCES += user_registry.cc user_registry.h
 libdhcp_user_chk_la_SOURCES += version.cc
 
-#nodist_libdhcp_user_chk_la_SOURCES = hooks_messages.cc hooks_messages.h
-nodist_libdhcp_user_chk_la_SOURCES =
+nodist_libdhcp_user_chk_la_SOURCES = user_chk_messages.cc user_chk_messages.h
 
 libdhcp_user_chk_la_CXXFLAGS = $(AM_CXXFLAGS)
 libdhcp_user_chk_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)

+ 18 - 9
src/hooks/dhcp/user_chk/load_unload.cc

@@ -14,11 +14,13 @@
 // load_unload.cc
 
 #include <hooks/hooks.h>
+#include <user_chk_log.h>
 #include <user_registry.h>
 #include <user_file.h>
 
 #include <iostream>
 #include <fstream>
+#include <errno.h>
 
 using namespace isc::hooks;
 
@@ -31,6 +33,8 @@ extern "C" {
 
 int load(LibraryHandle&) {
 
+    isc::log::MessageInitializer::loadDictionary();
+
     // non-zero indicates an error.
     int ret_val = 0;
     try {
@@ -49,16 +53,15 @@ int load(LibraryHandle&) {
         // Open up the output file for user_chk results.
         user_chk_output.open(user_chk_output_fname,
                      std::fstream::out | std::fstream::app);
-
+        int sav_errno = errno;
         if (!user_chk_output) {
-            std::cout << "UserCheckHook: cannot open user check output file: "
-                      << user_chk_output_fname << std::endl;
-            ret_val = 1;
+            isc_throw(isc::Unexpected, "Cannot open output file: "
+                                       << user_chk_output_fname
+                                       << " reason: " << strerror(sav_errno));
         }
     }
     catch (const std::exception& ex) {
-        std::cout << "UserCheckHook: loading user_chk hook lib failed:"
-                  << ex.what() << std::endl;
+        LOG_ERROR(user_chk_logger, USER_CHK_HOOK_LOAD_ERROR).arg(ex.what());
         ret_val = 1;
     }
 
@@ -66,9 +69,15 @@ int load(LibraryHandle&) {
 }
 
 int unload() {
-    user_registry.reset();
-    if (user_chk_output.is_open()) {
-        user_chk_output.close();
+    try {
+        user_registry.reset();
+        if (user_chk_output.is_open()) {
+            user_chk_output.close();
+        }
+    } catch (const std::exception& ex) {
+        // On the off chance something goes awry, catch it and log it.
+        // @todo Not sure if we should return a non-zero result or not.
+        LOG_ERROR(user_chk_logger, USER_CHK_HOOK_UNLOAD_ERROR).arg(ex.what());
     }
 
     return (0);

+ 7 - 6
src/hooks/dhcp/user_chk/subnet_select_co.cc

@@ -4,6 +4,7 @@
 #include <dhcp/pkt6.h>
 #include <dhcpsrv/subnet.h>
 #include <user_registry.h>
+#include <user_chk_log.h>
 
 #include <fstream>
 #include <string>
@@ -38,7 +39,7 @@ void generate_output_record(const std::string& id_type_str,
 // This callout is called at the "subnet4_select" hook.
 int subnet4_select(CalloutHandle& handle) {
     if (!user_registry) {
-        std::cout << "UserCheckHook: UserRegistry is null!" << std::endl;
+        LOG_ERROR(user_chk_logger, USER_CHK_SUBNET4_SELECT_REGISTRY_NULL);
         return (1);
     }
 
@@ -72,9 +73,8 @@ int subnet4_select(CalloutHandle& handle) {
                                    false);
         }
     } catch (const std::exception& ex) {
-        std::cout << "UserCheckHook: Exception in subnet4_select callout:"
-                  << ex.what() << std::endl;
-
+        LOG_ERROR(user_chk_logger, USER_CHK_SUBNET4_SELECT_ERROR)
+                 .arg(ex.what());
         return (1);
     }
 
@@ -83,7 +83,7 @@ int subnet4_select(CalloutHandle& handle) {
 // This callout is called at the "subnet6_select" hook.
 int subnet6_select(CalloutHandle& handle) {
     if (!user_registry) {
-        std::cout << "UserCheckHook: UserRegistry is null!" << std::endl;
+        LOG_ERROR(user_chk_logger, USER_CHK_SUBNET4_SELECT_REGISTRY_NULL);
         return (1);
     }
 
@@ -125,7 +125,8 @@ int subnet6_select(CalloutHandle& handle) {
                                    false);
         }
     } catch (const std::exception& ex) {
-        std::cout << "UserCheckHook: Exception in subnet6_select callout:"                        << ex.what() << std::endl;
+        LOG_ERROR(user_chk_logger, USER_CHK_SUBNET6_SELECT_ERROR)
+                                   .arg(ex.what());
         return (1);
     }
 

+ 3 - 0
src/hooks/dhcp/user_chk/tests/Makefile.am

@@ -34,6 +34,8 @@ libdhcp_user_chk_unittests_SOURCES += ../load_unload.cc
 libdhcp_user_chk_unittests_SOURCES += ../subnet_select_co.cc
 libdhcp_user_chk_unittests_SOURCES += ../version.cc
 libdhcp_user_chk_unittests_SOURCES += ../user.cc ../user.h
+libdhcp_user_chk_unittests_SOURCES += ../user_chk_log.cc ../user_chk_log.h
+libdhcp_user_chk_unittests_SOURCES += ../user_chk_messages.cc ../user_chk_messages.h
 libdhcp_user_chk_unittests_SOURCES += ../user_data_source.cc ../user_data_source.h
 libdhcp_user_chk_unittests_SOURCES += ../user_file.cc ../user_file.h
 libdhcp_user_chk_unittests_SOURCES += ../user_registry.cc ../user_registry.h
@@ -57,6 +59,7 @@ endif
 
 libdhcp_user_chk_unittests_LDADD = $(top_builddir)/src/lib/log/libb10-log.la
 libdhcp_user_chk_unittests_LDADD += $(top_builddir)/src/lib/util/libb10-util.la
+libdhcp_user_chk_unittests_LDADD += $(top_builddir)/src/lib/hooks/libb10-hooks.la
 libdhcp_user_chk_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libb10-dhcp++.la
 libdhcp_user_chk_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libb10-exceptions.la
 libdhcp_user_chk_unittests_LDADD += $(top_builddir)/src/lib/cc/libb10-cc.la

+ 19 - 0
src/hooks/dhcp/user_chk/user_chk_log.cc

@@ -0,0 +1,19 @@
+// 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 <user_chk_log.h>
+
+isc::log::Logger user_chk_logger("user_chk");

+ 29 - 0
src/hooks/dhcp/user_chk/user_chk_log.h

@@ -0,0 +1,29 @@
+// Copyright (C) 2013  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 USER_CHK_LOG_H
+#define USER_CHK_LOG_H
+
+#include <log/message_initializer.h>
+#include <log/macros.h>
+#include <user_chk_messages.h>
+
+/// @brief User Check Logger
+///
+/// Define the logger used to log messages.  We could define it in multiple
+/// modules, but defining in a single module and linking to it saves time and
+/// space.
+extern isc::log::Logger user_chk_logger;
+
+#endif // USER_CHK_LOG_H

+ 42 - 0
src/hooks/dhcp/user_chk/user_chk_messages.mes

@@ -0,0 +1,42 @@
+# Copyright (C) 2013  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.
+
+% USER_CHK_HOOK_LOAD_ERROR DHCP UserCheckHook could not be loaded: %1
+This is an error message issued when the DHCP UserCheckHook could not be loaded.
+The exact cause should be explained in the log message.  User subnet selection will revert to default processing.
+
+% USER_CHK_HOOK_UNLOAD_ERROR DHCP UserCheckHook an error occurred unloading the library: %1
+This is an error message issued when an error occurs while unloading the
+UserCheckHook library.  This is unlikely to occur and normal operations of the
+library will likely resume when it is next loaded.
+
+% USER_CHK_SUBNET4_SELECT_ERROR DHCP UserCheckHook an unexpected error occured in subnet4_select callout: %1
+This is an error message issued when the DHCP UserCheckHook subnet4_select hook
+encounters an unexpected error.  The message should contain a more detailed
+explanation.
+
+% USER_CHK_SUBNET4_SELECT_REGISTRY_NULL DHCP UserCheckHook UserRegistry has not been created.
+This is an error message issued when the DHCP UserCheckHook subnet4_select hook
+has been invoked but the UserRegistry has not been created.  This is a
+programmatic error and should not occur.
+
+% USER_CHK_SUBNET6_SELECT_ERROR DHCP UserCheckHook an unexpected error occured in subnet6_select callout: %1
+This is an error message issued when the DHCP UserCheckHook subnet6_select hook
+encounters an unexpected error.  The message should contain a more detailed
+explanation.
+
+% USER_CHK_SUBNET6_SELECT_REGISTRY_NULL DHCP UserCheckHook UserRegistry has not been created.
+This is an error message issued when the DHCP UserCheckHook subnet6_select hook
+has been invoked but the UserRegistry has not been created.  This is a
+programmatic error and should not occur.

+ 4 - 1
src/hooks/dhcp/user_chk/user_file.cc

@@ -17,6 +17,7 @@
 #include <user_file.h>
 
 #include <boost/foreach.hpp>
+#include <errno.h>
 
 UserFile::UserFile(const std::string& fname) : fname_(fname) {
     if (fname_.empty()) {
@@ -35,8 +36,10 @@ UserFile::open() {
     }
 
     ifs_.open(fname_.c_str(), std::ifstream::in);
+    int sav_error = errno;
     if (!ifs_.is_open()) {
-        isc_throw(UserFileError, "cannot open file:" << fname_);
+        isc_throw(UserFileError, "cannot open file:" << fname_
+                                 << " reason: " << strerror(sav_error));
     }
 
     setOpenFlag(true);