[cpp-sp] branch main updated: Syslog logging impl.

Scott Cantor cantor.2 at osu.edu
Wed Nov 27 15:21:22 UTC 2024


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository cpp-sp.

View the commit online:
http://git.shibboleth.net/view/?p=cpp-sp.git;a=commit;h=2aab347f363a2676caf3debffd97897aae0f6092

The following commit(s) were added to refs/heads/main by this push:
     new 2aab347f Syslog logging impl.
2aab347f is described below

commit 2aab347f363a2676caf3debffd97897aae0f6092
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Nov 27 10:21:14 2024 -0500

    Syslog logging impl.
---
 shibsp/Makefile.am                                 |   1 +
 shibsp/logging/impl/AbstractLoggingService.cpp     |   4 +-
 shibsp/logging/impl/SyslogLoggingService.cpp       | 132 +++++++++++++++++++++
 tests/AgentConfigTests.cpp                         |  10 +-
 .../{shibboleth.ini => console-shibboleth.ini}     |   0
 tests/data/syslog-shibboleth.ini                   |   8 ++
 6 files changed, 151 insertions(+), 4 deletions(-)

diff --git a/shibsp/Makefile.am b/shibsp/Makefile.am
index 53d2fac2..f5794045 100644
--- a/shibsp/Makefile.am
+++ b/shibsp/Makefile.am
@@ -124,6 +124,7 @@ libshibsp_la_SOURCES = \
 	logging/impl/ConsoleLoggingService.cpp \
 	logging/impl/Priority.cpp \
 	logging/impl/StringUtil.cpp \
+	logging/impl/SyslogLoggingService.cpp \
 	remoting/impl/ddf.cpp \
 	remoting/impl/ListenerService.cpp \
 	remoting/impl/SocketListener.cpp \
diff --git a/shibsp/logging/impl/AbstractLoggingService.cpp b/shibsp/logging/impl/AbstractLoggingService.cpp
index 2c486a11..43b0b875 100644
--- a/shibsp/logging/impl/AbstractLoggingService.cpp
+++ b/shibsp/logging/impl/AbstractLoggingService.cpp
@@ -43,7 +43,7 @@ namespace shibsp {
 #ifdef WIN32
     extern LoggingService* SHIBSP_DLLLOCAL WindowsLoggingServiceFactory(const ptree& pt, bool);
 #else
-    //extern LoggingService* SHIBSP_DLLLOCAL SyslogLoggingServiceFactory(const ptree& pt, bool);
+    extern LoggingService* SHIBSP_DLLLOCAL SyslogLoggingServiceFactory(const ptree& pt, bool);
 #endif
 }
 
@@ -54,7 +54,7 @@ void SHIBSP_API shibsp::registerLoggingServices()
 #ifdef WIN32
     conf.LoggingServiceManager.registerFactory(WINDOWS_LOGGING_SERVICE, WindowsLoggingServiceFactory);
 #else
-    //conf.LoggingServiceManager.registerFactory(SYSLOG_LOGGING_SERVICE, SyslogLoggingServiceFactory);
+    conf.LoggingServiceManager.registerFactory(SYSLOG_LOGGING_SERVICE, SyslogLoggingServiceFactory);
 #endif
 }
 
diff --git a/shibsp/logging/impl/SyslogLoggingService.cpp b/shibsp/logging/impl/SyslogLoggingService.cpp
new file mode 100644
index 00000000..e923b4bc
--- /dev/null
+++ b/shibsp/logging/impl/SyslogLoggingService.cpp
@@ -0,0 +1,132 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * logging/impl/SyslogLoggingService.cpp
+ *
+ * Logging service implementation using syslog.
+ */
+
+#include "internal.h"
+#include "logging/impl/AbstractLoggingService.h"
+
+#include <syslog.h>
+#include <boost/lexical_cast.hpp>
+#include <boost/property_tree/ptree.hpp>
+
+using namespace shibsp;
+using namespace boost::property_tree;
+using namespace boost;
+using namespace std;
+
+namespace shibsp {
+
+    class SyslogLoggingService : public virtual AbstractLoggingService {
+    public:
+        SyslogLoggingService(const ptree& pt);
+
+        static const char OPENSYSLOG_PROP_PATH[];
+        static const char FACILITY_PROP_PATH[];
+
+        bool init();
+        void term();
+
+        void outputMessage(const Category& category, Priority::Value prio, const string& message) {
+            outputMessage(category, prio, message.c_str());
+        }
+        void outputMessage(const Category& category, Priority::Value prio, const char* message);
+
+    private:
+        static int getSyslogPriority(Priority::Value prio);
+
+        bool m_open;
+        int m_facility;
+    };
+
+    LoggingService* SHIBSP_DLLLOCAL SyslogLoggingServiceFactory(const ptree& pt, bool) {
+        return new SyslogLoggingService(pt);
+    }
+
+}
+
+const char SyslogLoggingService::OPENSYSLOG_PROP_PATH[] = "logging.open-syslog";
+const char SyslogLoggingService::FACILITY_PROP_PATH[] = "logging.facility";
+
+SyslogLoggingService::SyslogLoggingService(const ptree& pt)
+    : AbstractLoggingService(pt), m_open(false), m_facility(LOG_USER)
+{
+    string opt = pt.get(OPENSYSLOG_PROP_PATH, "1");
+    m_open = (opt == "1" || opt == "true");
+
+    opt = pt.get(FACILITY_PROP_PATH, "0");
+    try {
+        m_facility = lexical_cast<int>(opt);
+        if (m_facility == 0) {
+            m_facility = LOG_USER;
+        }
+    } catch (const bad_lexical_cast& e) {
+        m_facility = LOG_USER;
+    }
+}
+
+bool SyslogLoggingService::init()
+{
+    if (!AbstractLoggingService::init()) {
+        return false;
+    }
+
+    if (m_open) {
+        openlog(PACKAGE, 0, m_facility);
+    }
+
+    return true;
+}
+
+void SyslogLoggingService::term()
+{
+    if (m_open) {
+        closelog();
+    }
+    AbstractLoggingService::term();
+}
+
+int SyslogLoggingService::getSyslogPriority(Priority::Value prio)
+{
+    switch (prio) {
+        case Priority::SHIB_DEBUG:
+            return LOG_DEBUG;
+        case Priority::SHIB_INFO:
+            return LOG_INFO;
+        case Priority::SHIB_WARN:
+            return LOG_WARNING;
+        case Priority::SHIB_ERROR:
+            return LOG_ERR;
+        case Priority::SHIB_CRIT:
+            return LOG_CRIT;
+        default:
+            return LOG_INFO;
+    }
+}
+
+void SyslogLoggingService::outputMessage(const Category& category, Priority::Value prio, const char* message)
+{
+    string s("[");
+    s += category.getName();
+    s += "] - ";
+    if (message) {
+        s += message;
+    }
+
+    syslog(getSyslogPriority(prio) | m_facility, "%s", s.c_str());
+}
diff --git a/tests/AgentConfigTests.cpp b/tests/AgentConfigTests.cpp
index 154a7b98..547b3b14 100644
--- a/tests/AgentConfigTests.cpp
+++ b/tests/AgentConfigTests.cpp
@@ -87,8 +87,14 @@ BOOST_AUTO_TEST_CASE(AgentConfig_term_without_init)
         runtime_error, checker_term.check_message);
 }
 
-BOOST_FIXTURE_TEST_CASE(AgentConfig_init_success, AC_Fixture)
+BOOST_FIXTURE_TEST_CASE(AgentConfig_init_console, AC_Fixture)
 {
-    BOOST_CHECK(AgentConfig::getConfig().init(nullptr, (data_path + "shibboleth.ini").c_str(), true));
+    BOOST_CHECK(AgentConfig::getConfig().init(nullptr, (data_path + "console-shibboleth.ini").c_str(), true));
+    AgentConfig::getConfig().term();
+}
+
+BOOST_FIXTURE_TEST_CASE(AgentConfig_init_syslog, AC_Fixture)
+{
+    BOOST_CHECK(AgentConfig::getConfig().init(nullptr, (data_path + "syslog-shibboleth.ini").c_str(), true));
     AgentConfig::getConfig().term();
 }
diff --git a/tests/data/shibboleth.ini b/tests/data/console-shibboleth.ini
similarity index 100%
rename from tests/data/shibboleth.ini
rename to tests/data/console-shibboleth.ini
diff --git a/tests/data/syslog-shibboleth.ini b/tests/data/syslog-shibboleth.ini
new file mode 100644
index 00000000..98d0e018
--- /dev/null
+++ b/tests/data/syslog-shibboleth.ini
@@ -0,0 +1,8 @@
+[logging]
+type = syslog
+default-level = WARN
+open-syslog = true
+facility = 32
+
+[logging-categories]
+Shibboleth.AgentConfig = WARN

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list