Apache Log4cxx: format-string.cpp (original) (raw)

#include <stdlib.h>

#if LOG4CXX_USING_STD_FORMAT

#include

#else

#include <fmt/core.h>

#include <fmt/color.h>

#include <fmt/ostream.h>

#endif

#include

struct MyStruct {

int x;

};

std::ostream& operator<<( std::ostream& stream, const MyStruct& mystruct ){

stream << "[MyStruct x:" << mystruct.x << "]";

return stream;

}

#if LOG4CXX_USING_STD_FORMAT

template

struct basic_ostream_formatter

: std::formatter<std::basic_string_view, Char>

{

template <typename T, typename OutputIt>

auto format(const T& value, std::basic_format_context<OutputIt, Char>& ctx) const -> OutputIt

{

std::basic_stringstream ss;

ss << value;

return std::formatter<std::basic_string_view, Char>::format(ss.view(), ctx);

}

};

template <> struct std::formatter : basic_ostream_formatter {};

#elif FMT_VERSION >= (9 * 10000)

template <> struct fmt::formatter : ostream_formatter {};

#endif

int main()

{

setlocale(LC_ALL, "");

BasicConfigurator::configure();

auto rootLogger = Logger::getRootLogger();

#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{ 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<<: "

<< std::setprecision(3) << 22.456

<< " And as hex: "

<< std::setbase( 16 ) << 123 );

LOG4CXX_INFO_FMT( rootLogger, "Numbers can be formatted with a format string {:.1f} and as hex: {:x}", 22.456, 123 );

return 0;

}

#define LOG4CXX_INFO_FMT(logger, fmt,...)

Add a new logging event containing a message defined by fmt and ... to attached appender(s) if logger...

Definition: log4cxx/logger.h:2339

#define LOG4CXX_INFO(logger, message)

Add a new logging event containing message to attached appender(s) if logger is enabled for INFO even...

Definition: log4cxx/logger.h:2318

log4cxx::helpers::UniCharMessageBuffer & operator<<(log4cxx::helpers::UniCharMessageBuffer &mb, const QString &msg)

Definition: log4cxx-qt/messagebuffer.h:24

Definition: configuration.h:25