Skip to content

Commit

Permalink
Optionally use std::format in LOG4CXX_XXXXX_FMT macros instead of fmt…
Browse files Browse the repository at this point in the history
…::format
  • Loading branch information
stephen-webb committed Nov 16, 2023
1 parent 4b20aa6 commit 024ea65
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
25 changes: 23 additions & 2 deletions src/examples/cpp/format-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand All @@ -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<<: "
Expand Down
7 changes: 7 additions & 0 deletions src/main/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions src/main/include/log4cxx/log4cxx.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 8 additions & 8 deletions src/main/include/log4cxx/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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, ...)
Expand Down
4 changes: 4 additions & 0 deletions src/test/cpp/benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
5 changes: 4 additions & 1 deletion src/test/cpp/throughput/log4cxxbenchmarker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand Down
4 changes: 4 additions & 0 deletions src/test/cpp/throughput/throughput-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 024ea65

Please sign in to comment.