You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sw...@apache.org on 2023/11/16 22:56:37 UTC
(logging-log4cxx) branch support_std_format created (now 024ea65e)
This is an automated email from the ASF dual-hosted git repository.
swebb2066 pushed a change to branch support_std_format
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
at 024ea65e Optionally use std::format in LOG4CXX_XXXXX_FMT macros instead of fmt::format
This branch includes the following new commits:
new 024ea65e Optionally use std::format in LOG4CXX_XXXXX_FMT macros instead of fmt::format
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(logging-log4cxx) 01/01: Optionally use std::format in LOG4CXX_XXXXX_FMT macros instead of fmt::format
Posted by sw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
swebb2066 pushed a commit to branch support_std_format
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit 024ea65efe41bdca24cd0bc2c370f538eb45f69a
Author: Stephen Webb <sw...@gmail.com>
AuthorDate: Fri Nov 17 09:51:34 2023 +1100
Optionally use std::format in LOG4CXX_XXXXX_FMT macros instead of fmt::format
---
src/examples/cpp/format-string.cpp | 25 +++++++++++++++++++++++--
src/main/include/CMakeLists.txt | 7 +++++++
src/main/include/log4cxx/log4cxx.h.in | 7 +++++++
src/main/include/log4cxx/logger.h | 16 ++++++++--------
src/test/cpp/benchmark/benchmark.cpp | 4 ++++
src/test/cpp/throughput/log4cxxbenchmarker.cpp | 5 ++++-
src/test/cpp/throughput/throughput-main.cpp | 4 ++++
7 files changed, 57 insertions(+), 11 deletions(-)
diff --git a/src/examples/cpp/format-string.cpp b/src/examples/cpp/format-string.cpp
index 19dafa37..10053ffe 100644
--- a/src/examples/cpp/format-string.cpp
+++ b/src/examples/cpp/format-string.cpp
@@ -18,9 +18,13 @@
#include <stdlib.h>
#include <log4cxx/basicconfigurator.h>
#include <locale.h>
+#if LOG4CXX_USING_STD_FORMAT
+#include <format>
+#else
#include <fmt/core.h>
#include <fmt/color.h>
#include <fmt/ostream.h>
+#endif
#include <iomanip>
using namespace log4cxx;
@@ -34,7 +38,22 @@ std::ostream& operator<<( std::ostream& stream, const MyStruct& mystruct ){
stream << "[MyStruct x:" << mystruct.x << "]";
return stream;
}
-#if FMT_VERSION >= (9 * 10000)
+
+#if LOG4CXX_USING_STD_FORMAT
+template <typename Char>
+struct basic_ostream_formatter
+ : std::formatter<std::basic_string_view<Char>, Char>
+{
+ template <typename T, typename OutputIt>
+ auto format(const T& value, std::basic_format_context<OutputIt, Char>& ctx) const -> OutputIt
+ {
+ std::basic_stringstream<Char> ss;
+ ss << value;
+ return std::formatter<std::basic_string_view<Char>, Char>::format(ss.view(), ctx);
+ }
+};
+template <> struct std::formatter<MyStruct> : basic_ostream_formatter<char> {};
+#elif FMT_VERSION >= (9 * 10000)
template <> struct fmt::formatter<MyStruct> : ostream_formatter {};
#endif
@@ -46,10 +65,12 @@ int main()
LoggerPtr rootLogger = Logger::getRootLogger();
LOG4CXX_INFO_FMT( rootLogger, "This is a {} mesage", "test" );
+#if !LOG4CXX_USING_STD_FORMAT
LOG4CXX_INFO_FMT( rootLogger, fmt::fg(fmt::color::red), "Messages can be colored" );
+#endif
LOG4CXX_INFO_FMT( rootLogger, "We can also align text to the {:<10} or {:>10}", "left", "right" );
- MyStruct mine;
+ MyStruct mine{ 42 };
LOG4CXX_INFO_FMT( rootLogger, "This custom type {} can also be logged, since it implements operator<<", mine );
LOG4CXX_INFO( rootLogger, "Numbers can be formatted with excessive operator<<: "
diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt
index 3127a213..4e0603db 100644
--- a/src/main/include/CMakeLists.txt
+++ b/src/main/include/CMakeLists.txt
@@ -96,6 +96,13 @@ if(NOT "log4cxx" STREQUAL "${LOG4CXX_NS}")
set(LOG4CXX_DECLARE_DEFAULT_NS_ALIAS 1)
endif()
+set(LOG4CXX_FORMAT_NAMESPACE "fmt" CACHE STRING "format() function namespace, choice of fmt (default), std")
+set_property(CACHE LOG4CXX_FORMAT_NAMESPACE PROPERTY STRINGS "fmt" "std")
+set(LOG4CXX_USE_STANDARD_FORMAT 0)
+if("std" STREQUAL "${LOG4CXX_FORMAT_NAMESPACE}")
+ set(LOG4CXX_USE_STANDARD_FORMAT 1)
+endif()
+
# Configure log4cxx_private.h
set(LOG4CXX_CHARSET "utf-8" CACHE STRING "LogString characters, choice of utf-8 (default), ISO-8859-1, US-ASCII, EBCDIC, locale")
set_property(CACHE LOG4CXX_CHARSET PROPERTY STRINGS "utf-8" "ISO-8859-1" "US-ASCII" "EBCDIC" "locale")
diff --git a/src/main/include/log4cxx/log4cxx.h.in b/src/main/include/log4cxx/log4cxx.h.in
index b16d3de1..763d3907 100644
--- a/src/main/include/log4cxx/log4cxx.h.in
+++ b/src/main/include/log4cxx/log4cxx.h.in
@@ -123,4 +123,11 @@ LOG4CXX_EXPORT uint32_t libraryVersion();
namespace log4cxx = LOG4CXX_NS;
#endif
+#define LOG4CXX_USING_STD_FORMAT @LOG4CXX_USE_STANDARD_FORMAT@
+#if LOG4CXX_USING_STD_FORMAT
+#define LOG4CXX_FORMAT_NS std
+#elif !defined(LOG4CXX_FORMAT_NS)
+#define LOG4CXX_FORMAT_NS fmt
+#endif
+
#endif
diff --git a/src/main/include/log4cxx/logger.h b/src/main/include/log4cxx/logger.h
index b0f573d6..37ac8c2d 100644
--- a/src/main/include/log4cxx/logger.h
+++ b/src/main/include/log4cxx/logger.h
@@ -2037,7 +2037,7 @@ Add a new logging event containing libfmt formatted <code>...</code> to attached
*/
#define LOG4CXX_LOG_FMT(logger, level, ...) do { \
if (logger->isEnabledFor(level)) {\
- logger->addEvent(level, fmt::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
+ logger->addEvent(level, ::LOG4CXX_FORMAT_NS::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
/**
Add a new logging event containing \c message to attached appender(s) if this logger is enabled for \c events.
@@ -2083,7 +2083,7 @@ Add a new logging event containing libfmt formatted <code>...</code> to attached
*/
#define LOG4CXX_DEBUG_FMT(logger, ...) do { \
if (LOG4CXX_UNLIKELY(::LOG4CXX_NS::Logger::isDebugEnabledFor(logger))) {\
- logger->addEvent(::LOG4CXX_NS::Level::getDebug(), fmt::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
+ logger->addEvent(::LOG4CXX_NS::Level::getDebug(), ::LOG4CXX_FORMAT_NS::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
#else
#define LOG4CXX_DEBUG(logger, message)
#define LOG4CXX_DEBUG_FMT(logger, ...)
@@ -2115,7 +2115,7 @@ Add a new logging event containing libfmt formatted <code>...</code> to attached
*/
#define LOG4CXX_TRACE_FMT(logger, ...) do { \
if (LOG4CXX_UNLIKELY(::LOG4CXX_NS::Logger::isTraceEnabledFor(logger))) {\
- logger->addEvent(::LOG4CXX_NS::Level::getTrace(), fmt::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
+ logger->addEvent(::LOG4CXX_NS::Level::getTrace(), ::LOG4CXX_FORMAT_NS::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
#else
#define LOG4CXX_TRACE(logger, message)
#define LOG4CXX_TRACE_FMT(logger, ...)
@@ -2151,7 +2151,7 @@ Add a new logging event containing libfmt formatted <code>...</code> to attached
*/
#define LOG4CXX_INFO_FMT(logger, ...) do { \
if (::LOG4CXX_NS::Logger::isInfoEnabledFor(logger)) {\
- logger->addEvent(::LOG4CXX_NS::Level::getInfo(), fmt::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
+ logger->addEvent(::LOG4CXX_NS::Level::getInfo(), ::LOG4CXX_FORMAT_NS::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
#else
#define LOG4CXX_INFO(logger, message)
#define LOG4CXX_INFO_FMT(logger, ...)
@@ -2185,7 +2185,7 @@ Add a new logging event containing libfmt formatted <code>...</code> to attached
*/
#define LOG4CXX_WARN_FMT(logger, ...) do { \
if (::LOG4CXX_NS::Logger::isWarnEnabledFor(logger)) {\
- logger->addEvent(::LOG4CXX_NS::Level::getWarn(), fmt::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
+ logger->addEvent(::LOG4CXX_NS::Level::getWarn(), ::LOG4CXX_FORMAT_NS::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
#else
#define LOG4CXX_WARN(logger, message)
#define LOG4CXX_WARN_FMT(logger, ...)
@@ -2219,7 +2219,7 @@ Add a new logging event containing libfmt formatted <code>...</code> to attached
*/
#define LOG4CXX_ERROR_FMT(logger, ...) do { \
if (::LOG4CXX_NS::Logger::isErrorEnabledFor(logger)) {\
- logger->addEvent(::LOG4CXX_NS::Level::getError(), fmt::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
+ logger->addEvent(::LOG4CXX_NS::Level::getError(), ::LOG4CXX_FORMAT_NS::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
/**
If \c condition is not true, add a new logging event containing \c message to attached appender(s) if \c logger is enabled for <code>ERROR</code> events.
@@ -2244,7 +2244,7 @@ If \c condition is not true, add a new logging event containing libfmt formatted
#define LOG4CXX_ASSERT_FMT(logger, condition, ...) do { \
if (!(condition) && ::LOG4CXX_NS::Logger::isErrorEnabledFor(logger)) {\
LOG4CXX_STACKTRACE \
- logger->addEvent(::LOG4CXX_NS::Level::getError(), fmt::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
+ logger->addEvent(::LOG4CXX_NS::Level::getError(), ::LOG4CXX_FORMAT_NS::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
#else
#define LOG4CXX_ERROR(logger, message)
@@ -2278,7 +2278,7 @@ Add a new logging event containing libfmt formatted <code>...</code> to attached
*/
#define LOG4CXX_FATAL_FMT(logger, ...) do { \
if (::LOG4CXX_NS::Logger::isFatalEnabledFor(logger)) {\
- logger->addEvent(::LOG4CXX_NS::Level::getFatal(), fmt::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
+ logger->addEvent(::LOG4CXX_NS::Level::getFatal(), ::LOG4CXX_FORMAT_NS::format( __VA_ARGS__ ), LOG4CXX_LOCATION); }} while (0)
#else
#define LOG4CXX_FATAL(logger, message)
#define LOG4CXX_FATAL_FMT(logger, ...)
diff --git a/src/test/cpp/benchmark/benchmark.cpp b/src/test/cpp/benchmark/benchmark.cpp
index 638a1590..620570e8 100644
--- a/src/test/cpp/benchmark/benchmark.cpp
+++ b/src/test/cpp/benchmark/benchmark.cpp
@@ -4,7 +4,11 @@
#include <log4cxx/helpers/optionconverter.h>
#include <log4cxx/helpers/stringhelper.h>
#include <log4cxx/asyncappender.h>
+#if LOG4CXX_USING_STD_FORMAT
+#include <format>
+#else
#include <fmt/format.h>
+#endif
#include <benchmark/benchmark.h>
#include <thread>
diff --git a/src/test/cpp/throughput/log4cxxbenchmarker.cpp b/src/test/cpp/throughput/log4cxxbenchmarker.cpp
index 0a1568d5..f60574d5 100644
--- a/src/test/cpp/throughput/log4cxxbenchmarker.cpp
+++ b/src/test/cpp/throughput/log4cxxbenchmarker.cpp
@@ -20,8 +20,11 @@
#include <log4cxx/patternlayout.h>
#include <log4cxx/appenderskeleton.h>
+#if LOG4CXX_USING_STD_FORMAT
+#include <format>
+#else
#include <fmt/format.h>
-
+#endif
namespace LOG4CXX_NS
{
diff --git a/src/test/cpp/throughput/throughput-main.cpp b/src/test/cpp/throughput/throughput-main.cpp
index 23807563..ae6dc169 100644
--- a/src/test/cpp/throughput/throughput-main.cpp
+++ b/src/test/cpp/throughput/throughput-main.cpp
@@ -24,9 +24,13 @@
#include <string>
#include <thread>
+#if LOG4CXX_USING_STD_FORMAT
+#include <format>
+#else
#include <fmt/core.h>
#include <fmt/chrono.h>
#include <fmt/ostream.h>
+#endif
#include "log4cxxbenchmarker.h"
using log4cxx::LogString;