[cpp-sp] branch main updated: Port in some initial logging machinery.
Scott Cantor
cantor.2 at osu.edu
Thu Nov 21 15:40:16 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=e6f3e42f777d67618096ec81c4f0bdcb066d2681
The following commit(s) were added to refs/heads/main by this push:
new e6f3e42f Port in some initial logging machinery.
e6f3e42f is described below
commit e6f3e42f777d67618096ec81c4f0bdcb066d2681
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Nov 21 10:40:12 2024 -0500
Port in some initial logging machinery.
---
shibsp/Makefile.am | 10 ++
shibsp/logging/Category.h | 269 +++++++++++++++++++++++++++++++++++++
shibsp/logging/Priority.h | 76 +++++++++++
shibsp/logging/impl/Category.cpp | 154 +++++++++++++++++++++
shibsp/logging/impl/Priority.cpp | 53 ++++++++
shibsp/logging/impl/StringUtil.cpp | 55 ++++++++
shibsp/logging/impl/StringUtil.h | 48 +++++++
7 files changed, 665 insertions(+)
diff --git a/shibsp/Makefile.am b/shibsp/Makefile.am
index 440f3e12..2274195c 100644
--- a/shibsp/Makefile.am
+++ b/shibsp/Makefile.am
@@ -8,6 +8,8 @@ attrincludedir = $(includedir)/shibsp/attribute
handincludedir = $(includedir)/shibsp/handler
+logincludedir = $(includedir)/shibsp/logging
+
remincludedir = $(includedir)/shibsp/remoting
utilincludedir = $(includedir)/shibsp/util
@@ -45,6 +47,10 @@ handinclude_HEADERS = \
handler/SecuredHandler.h \
handler/SessionInitiator.h
+loginclude_HEADERS = \
+ logging/Category.h \
+ logging/Priority.h
+
reminclude_HEADERS = \
remoting/ddf.h \
remoting/ListenerService.h
@@ -64,6 +70,7 @@ noinst_HEADERS = \
impl/StorageServiceSessionCache.h \
impl/XMLApplication.h \
impl/XMLServiceProvider.h \
+ logging/impl/StringUtil.h \
remoting/impl/SocketListener.h
libshibsp_la_SOURCES = \
@@ -105,6 +112,9 @@ libshibsp_la_SOURCES = \
impl/XMLApplication.cpp \
impl/XMLRequestMapper.cpp \
impl/XMLServiceProvider.cpp \
+ logging/impl/Category.cpp \
+ logging/impl/Priority.cpp \
+ logging/impl/StringUtil.cpp \
remoting/impl/ddf.cpp \
remoting/impl/ListenerService.cpp \
remoting/impl/SocketListener.cpp \
diff --git a/shibsp/logging/Category.h b/shibsp/logging/Category.h
new file mode 100644
index 00000000..e98718ec
--- /dev/null
+++ b/shibsp/logging/Category.h
@@ -0,0 +1,269 @@
+/**
+ * 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.
+ */
+
+#ifndef __shibsp_logging_category_h__
+#define __shibsp_logging_category_h__
+
+#include <shibsp/logging/Priority.h>
+
+#include <map>
+#include <vector>
+#include <cstdarg>
+#include <stdexcept>
+
+namespace shibsp {
+
+ /**
+ * This is the central class in the logging API, but is not a fully caoable
+ * port of a hierarchical logging system that allows differentiation of logging
+ * levels by category. This is merely a porting convenience and to potentially
+ * allow for that in the future.
+ */
+ class SHIBSP_API Category {
+ MAKE_NONCOPYABLE(Category);
+ public:
+ /**
+ * Instantiate a Category with name <code>name</code>. This
+ * method does not set priority of the category which is by
+ * default <code>Priority::NOTSET</code>.
+ *
+ * @param name The name of the category to retrieve.
+ */
+ static Category& getInstance(const std::string& name);
+
+ /**
+ * Destructor for Category.
+ */
+ virtual ~Category();
+
+ /**
+ * Return the category name.
+ *
+ * @returns The category name.
+ */
+ virtual const std::string& getName() const;
+
+ /**
+ * Set the priority of this Category.
+ *
+ * @param priority The priority to set. Use Priority::NOTSET to let
+ * the category use its parents priority as effective priority.
+ *
+ * @exception std::invalid_argument if the caller tries to set
+ * Priority::NOTSET on the Root Category.
+ */
+ virtual void setPriority(Priority::Value priority);
+
+ /**
+ * Returns the assigned Priority, if any, for this Category.
+ *
+ * @return Priority - the assigned Priority, can be Priority::NOTSET
+ */
+ virtual Priority::Value getPriority() const;
+
+ /**
+ * Returns true if the priority of the Category is equal to
+ * or higher than given priority.
+ *
+ * @param priority The priority to compare with.
+ * @returns whether logging is enable for this priority.
+ */
+ virtual bool isPriorityEnabled(Priority::Value priority) const;
+
+ /**
+ * Log a message with the specified priority.
+ *
+ * @param priority The priority of this log message.
+ * @param stringFormat Format specifier for the string to write
+ * in the log file.
+ * @param ... The arguments for stringFormat
+ */
+ virtual void log(Priority::Value priority, const char* stringFormat, ...) throw();
+
+ /**
+ * Log a message with the specified priority.
+ * @param priority The priority of this log message.
+ * @param message string to write in the log file
+ */
+ virtual void log(Priority::Value priority, const std::string& message) throw();
+
+ /**
+ * Log a message with the specified priority.
+ *
+ * @param priority The priority of this log message.
+ * @param stringFormat Format specifier for the string to write
+ * in the log file.
+ * @param va The arguments for stringFormat.
+ */
+ virtual void logva(Priority::Value priority, const char* stringFormat, va_list va) throw();
+
+ /**
+ * Log a message with debug priority.
+ * @param stringFormat Format specifier for the string to write
+ * in the log file.
+ * @param ... The arguments for stringFormat
+ */
+ void debug(const char* stringFormat, ...) throw();
+
+ /**
+ * Log a message with debug priority.
+ * @param message string to write in the log file
+ */
+ void debug(const std::string& message) throw();
+
+ /**
+ * Return true if the Category will log messages with priority SHIB_DEBUG.
+ *
+ * @returns Whether the Category will log.
+ */
+ inline bool isDebugEnabled() const throw() {
+ return isPriorityEnabled(Priority::SHIB_DEBUG);
+ };
+
+ /**
+ * Log a message with info priority.
+ *
+ * @param stringFormat Format specifier for the string to write
+ * in the log file.
+ * @param ... The arguments for stringFormat
+ */
+ void info(const char* stringFormat, ...) throw();
+
+ /**
+ * Log a message with info priority.
+ *
+ * @param message string to write in the log file
+ */
+ void info(const std::string& message) throw();
+
+ /**
+ * Return true if the Category will log messages with priority SHIB_INFO.
+ *
+ * @returns Whether the Category will log.
+ */
+ inline bool isInfoEnabled() const throw() {
+ return isPriorityEnabled(Priority::SHIB_INFO);
+ };
+
+ /**
+ * Log a message with warn priority.
+ *
+ * @param stringFormat Format specifier for the string to write
+ * in the log file.
+ * @param ... The arguments for stringFormat
+ */
+ void warn(const char* stringFormat, ...) throw();
+
+ /**
+ * Log a message with warn priority.
+ *
+ * @param message string to write in the log file
+ */
+ void warn(const std::string& message) throw();
+
+ /**
+ * Return true if the Category will log messages with priority SHIB_WARN.
+ *
+ * @returns Whether the Category will log.
+ */
+ inline bool isWarnEnabled() const throw() {
+ return isPriorityEnabled(Priority::SHIB_WARN);
+ };
+
+ /**
+ * Log a message with error priority.
+ *
+ * @param stringFormat Format specifier for the string to write
+ * in the log file.
+ * @param ... The arguments for stringFormat
+ */
+ void error(const char* stringFormat, ...) throw();
+
+ /**
+ * Log a message with error priority.
+ *
+ * @param message string to write in the log file
+ */
+ void error(const std::string& message) throw();
+
+ /**
+ * Return true if the Category will log messages with priority SHIB_ERROR.
+ *
+ * @returns Whether the Category will log.
+ */
+ inline bool isErrorEnabled() const throw() {
+ return isPriorityEnabled(Priority::SHIB_ERROR);
+ };
+
+ /**
+ * Log a message with crit priority.
+ *
+ * @param stringFormat Format specifier for the string to write
+ * in the log file.
+ * @param ... The arguments for stringFormat
+ */
+ void crit(const char* stringFormat, ...) throw();
+
+ /**
+ * Log a message with crit priority.
+ *
+ * @param message string to write in the log file
+ */
+ void crit(const std::string& message) throw();
+
+ /**
+ * Return true if the Category will log messages with priority CRIT.
+ *
+ * @returns Whether the Category will log.
+ */
+ inline bool isCritEnabled() const throw() {
+ return isPriorityEnabled(Priority::SHIB_CRIT);
+ };
+
+ protected:
+ /**
+ * Constructor.
+ *
+ * @param name the fully qualified name of this Category
+ * @param parent the parent of this parent, or NULL for the root
+ * Category
+ * @param priority the priority for this Category. Defaults to
+ * Priority::SHIB_NOTSET
+ */
+ Category(const std::string& name, Priority::Value priority = Priority::SHIB_NOTSET);
+
+ /**
+ * Unconditionally log a message with the specified priority.
+ *
+ * @param priority The priority of this log message
+ * @param format formatting string for message
+ * @param arguments variable arguments
+ */
+ virtual void _logUnconditionally(Priority::Value priority, const char* format, va_list arguments) throw();
+
+ /**
+ * Unconditionally log a message with the specified priority.
+ *
+ * @param priority The priority of this log message
+ * @param message string to write to the log
+ */
+ virtual void _logUnconditionally2(Priority::Value priority, const std::string& message) throw();
+
+ private:
+ const std::string m_name;
+ Priority::Value m_priority;
+ };
+
+}
+#endif // __shibsp_logging_category_h__
diff --git a/shibsp/logging/Priority.h b/shibsp/logging/Priority.h
new file mode 100644
index 00000000..3a383845
--- /dev/null
+++ b/shibsp/logging/Priority.h
@@ -0,0 +1,76 @@
+/**
+ * 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.
+ */
+
+#ifndef __shibsp_logging_priority_h__
+#define __shibsp_logging_priority_h__
+
+#include <shibsp/base.h>
+
+#include <string>
+#include <stdexcept>
+
+namespace shibsp {
+
+ /**
+ * Enumerates levels of logging we support.
+ */
+ class SHIBSP_API Priority {
+ public:
+
+ /**
+ * Predefined Levels of Priorities.
+ */
+ enum {
+ SHIB_CRIT = 0,
+ SHIB_ERROR = 100,
+ SHIB_WARN = 200,
+ SHIB_INFO = 300,
+ SHIB_DEBUG = 400,
+ SHIB_NOTSET = 500
+ } PriorityLevel;
+
+ /**
+ * The type of Priority Values.
+ */
+ typedef int Value;
+
+ /**
+ * Returns the name of the given priority value.
+ *
+ * Currently, if the value is not one of the PriorityLevel values,
+ * the method returns the name of the largest priority smaller
+ * the given value.
+ *
+ * @param priority the numeric value of the priority.
+ * @returns a string representing the name of the priority.
+ */
+ static const std::string& getPriorityName(int priority) throw();
+
+ /**
+ * Returns the value of the given priority name.
+ *
+ * This can be either one of SHIB_CRIT ... SHIB_NOTSET or a
+ * decimal string representation of the value, e.g. '700' for SHIB_DEBUG.
+ *
+ * @param priorityName the string containing the the of the priority
+ * @return the value corresponding with the priority name
+ *
+ * @throw std::invalid_argument if the priorityName does not
+ * correspond with a known Priority name or a number
+ */
+ static Value getPriorityValue(const std::string& priorityName);
+ };
+}
+
+#endif // __shibsp_logging_priority_h__
diff --git a/shibsp/logging/impl/Category.cpp b/shibsp/logging/impl/Category.cpp
new file mode 100644
index 00000000..09057205
--- /dev/null
+++ b/shibsp/logging/impl/Category.cpp
@@ -0,0 +1,154 @@
+/**
+ * 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.
+ */
+
+#include "logging/Category.h"
+#include "StringUtil.h"
+
+using namespace shibsp;
+
+Category& Category::getInstance(const std::string& name) {
+ //return HierarchyMaintainer::getDefaultMaintainer().getInstance(name);
+}
+
+Category::Category(const std::string& name, Priority::Value priority) : m_name(name), m_priority(priority) {
+}
+
+Category::~Category() {
+}
+
+const std::string& Category::getName() const {
+ return m_name;
+}
+
+Priority::Value Category::getPriority() const {
+ return m_priority;
+}
+
+void Category::setPriority(Priority::Value priority) {
+ if ((priority < Priority::SHIB_NOTSET)) {
+ m_priority = priority;
+ } else {
+ /* caller tried to set NOTSET priority to root Category.
+ Bad caller!
+ */
+ throw std::invalid_argument("cannot set priority SHIB_NOTSET on Root Category");
+ }
+}
+
+void Category::_logUnconditionally(Priority::Value priority, const char* format, va_list arguments) throw() {
+ _logUnconditionally2(priority, StringUtil::vform(format, arguments));
+}
+
+void Category::_logUnconditionally2(Priority::Value priority, const std::string& message) throw() {
+ // TODO: do the actual logging
+}
+
+bool Category::isPriorityEnabled(Priority::Value priority) const {
+ return m_priority >= priority;
+}
+
+void Category::log(Priority::Value priority, const char* stringFormat, ...) throw() {
+ if (isPriorityEnabled(priority)) {
+ va_list va;
+ va_start(va, stringFormat);
+ _logUnconditionally(priority, stringFormat, va);
+ va_end(va);
+ }
+}
+
+void Category::log(Priority::Value priority, const std::string& message) throw() {
+ if (isPriorityEnabled(priority)) {
+ _logUnconditionally2(priority, message);
+ }
+}
+
+void Category::logva(Priority::Value priority, const char* stringFormat, va_list va) throw() {
+ if (isPriorityEnabled(priority)) {
+ _logUnconditionally(priority, stringFormat, va);
+ }
+}
+
+void Category::debug(const char* stringFormat, ...) throw() {
+ if (isPriorityEnabled(Priority::SHIB_DEBUG)) {
+ va_list va;
+ va_start(va,stringFormat);
+ _logUnconditionally(Priority::SHIB_DEBUG, stringFormat, va);
+ va_end(va);
+ }
+}
+
+void Category::debug(const std::string& message) throw() {
+ if (isPriorityEnabled(Priority::SHIB_DEBUG)) {
+ _logUnconditionally2(Priority::SHIB_DEBUG, message);
+ }
+}
+
+void Category::info(const char* stringFormat, ...) throw() {
+ if (isPriorityEnabled(Priority::SHIB_INFO)) {
+ va_list va;
+ va_start(va,stringFormat);
+ _logUnconditionally(Priority::SHIB_INFO, stringFormat, va);
+ va_end(va);
+ }
+}
+
+void Category::info(const std::string& message) throw() {
+ if (isPriorityEnabled(Priority::SHIB_INFO)) {
+ _logUnconditionally2(Priority::SHIB_INFO, message);
+ }
+}
+
+void Category::warn(const char* stringFormat, ...) throw() {
+ if (isPriorityEnabled(Priority::SHIB_WARN)) {
+ va_list va;
+ va_start(va,stringFormat);
+ _logUnconditionally(Priority::SHIB_WARN, stringFormat, va);
+ va_end(va);
+ }
+}
+
+void Category::warn(const std::string& message) throw() {
+ if (isPriorityEnabled(Priority::SHIB_WARN))
+ _logUnconditionally2(Priority::SHIB_WARN, message);
+}
+
+void Category::error(const char* stringFormat, ...) throw() {
+ if (isPriorityEnabled(Priority::SHIB_ERROR)) {
+ va_list va;
+ va_start(va,stringFormat);
+ _logUnconditionally(Priority::SHIB_ERROR, stringFormat, va);
+ va_end(va);
+ }
+}
+
+void Category::error(const std::string& message) throw() {
+ if (isPriorityEnabled(Priority::SHIB_ERROR)) {
+ _logUnconditionally2(Priority::SHIB_ERROR, message);
+ }
+}
+
+void Category::crit(const char* stringFormat, ...) throw() {
+ if (isPriorityEnabled(Priority::SHIB_CRIT)) {
+ va_list va;
+ va_start(va,stringFormat);
+ _logUnconditionally(Priority::SHIB_CRIT, stringFormat, va);
+ va_end(va);
+ }
+}
+
+void Category::crit(const std::string& message) throw() {
+ if (isPriorityEnabled(Priority::SHIB_CRIT)) {
+ _logUnconditionally2(Priority::SHIB_CRIT, message);
+ }
+}
diff --git a/shibsp/logging/impl/Priority.cpp b/shibsp/logging/impl/Priority.cpp
new file mode 100644
index 00000000..6a658e2c
--- /dev/null
+++ b/shibsp/logging/impl/Priority.cpp
@@ -0,0 +1,53 @@
+/**
+ * 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.
+ */
+
+#include <shibsp/logging/Priority.h>
+
+#include <cstdlib>
+
+using namespace shibsp;
+
+namespace {
+ const std::string names[7] = {
+ "SHIB_CRIT", "SHIB_ERROR", "SHIB_WARN", "SHIB_INFO", "SHIB_DEBUG", "SHIB_NOTSET", "UNKNOWN"
+ };
+}
+
+const std::string& Priority::getPriorityName(int priority) throw() {
+
+ priority++;
+ priority /= 100;
+ return names[((priority < 0) || (priority > 5)) ? 5 : priority];
+}
+
+Priority::Value Priority::getPriorityValue(const std::string& priorityName) {
+ Priority::Value value = -1;
+
+ for (unsigned int i = 0; i < 7; i++) {
+ if (priorityName == names[i]) {
+ value = i * 100;
+ break;
+ }
+ }
+
+ if (value == -1) {
+ char* endPointer;
+ value = std::strtoul(priorityName.c_str(), &endPointer, 10);
+ if (*endPointer != 0) {
+ throw std::invalid_argument(std::string("unknown priority name: '") + priorityName + "'");
+ }
+ }
+
+ return value;
+}
diff --git a/shibsp/logging/impl/StringUtil.cpp b/shibsp/logging/impl/StringUtil.cpp
new file mode 100644
index 00000000..a5f50bae
--- /dev/null
+++ b/shibsp/logging/impl/StringUtil.cpp
@@ -0,0 +1,55 @@
+/**
+ * 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.
+ */
+
+#include "StringUtil.h"
+
+#include <cstdio>
+
+using namespace shibsp;
+
+#if defined(_MSC_VER)
+ #define VSNPRINTF _vsnprintf
+#else
+ #define VSNPRINTF vsnprintf
+#endif // _MSC_VER
+
+std::string StringUtil::vform(const char* format, va_list args) {
+ size_t size = 256;
+ char* buffer = new char[size];
+
+ while (true) {
+ va_list args_copy;
+
+ va_copy(args_copy, args);
+ int n = VSNPRINTF(buffer, size, format, args_copy);
+
+ va_end(args_copy);
+
+ // If that worked, return a string.
+ if ((n > -1) && (static_cast<size_t>(n) < size)) {
+ std::string s(buffer);
+ delete [] buffer;
+ return s;
+ }
+
+ // Else try again with more space.
+ size = (n > -1) ?
+ n + 1 : // ISO/IEC 9899:1999
+ size * 2; // twice the old size
+
+ delete [] buffer;
+ buffer = new char[size];
+ }
+
+}
diff --git a/shibsp/logging/impl/StringUtil.h b/shibsp/logging/impl/StringUtil.h
new file mode 100644
index 00000000..f4b42a7a
--- /dev/null
+++ b/shibsp/logging/impl/StringUtil.h
@@ -0,0 +1,48 @@
+/**
+ * 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.
+ */
+
+#ifndef __shibsp_logging_stringutil_h__
+#define __shibsp_logging_stringutil_h__
+
+#include <shibsp/base.h>
+
+#include <string>
+#include <vector>
+#include <climits>
+#include <cstdarg>
+
+namespace shibsp {
+
+ /**
+ * Utility class ported from log4shib.
+ */
+ class StringUtil {
+ private:
+ StringUtil() {}
+ MAKE_NONCOPYABLE(StringUtil);
+
+ public:
+ /**
+ * Returns a string contructed from the a format specifier
+ * and a va_list of arguments, analogously to vprintf(3).
+ *
+ * @param format the format specifier.
+ * @param args the va_list of arguments.
+ */
+ static std::string vform(const char* format, va_list args);
+ };
+
+}
+
+#endif // __shibsp_logging_stringutil_h__
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list