This example shows logging using the {fmt} library.
#include <stdlib.h>
#include <fmt/core.h>
#include <fmt/color.h>
#include <fmt/ostream.h>
#include <iomanip>
struct MyStruct {
int x;
};
std::ostream& operator<<( std::ostream& stream, const MyStruct& mystruct ){
stream << "[MyStruct x:" << mystruct.x << "]";
return stream;
}
#if FMT_VERSION >= (9 * 10000)
template <> struct fmt::formatter<MyStruct> : ostream_formatter {};
#endif
int main()
{
setlocale(LC_ALL, "");
BasicConfigurator::configure();
LoggerPtr rootLogger = Logger::getRootLogger();
LOG4CXX_INFO_FMT( rootLogger, fmt::fg(fmt::color::red),
"Messages can be colored" );
LOG4CXX_INFO_FMT( rootLogger,
"We can also align text to the {:<10} or {:>10}",
"left",
"right" );
MyStruct mine;
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(logger, message)
Add a new logging event containing message to attached appender(s) if logger is enabled for INFO even...
Definition: logger.h:2102
#define LOG4CXX_INFO_FMT(logger,...)
Add a new logging event containing libfmt formatted ... to attached appender(s) if logger is enabled ...
Definition: logger.h:2113
Definition: propertysetter.h:27
Definition: configuration.h:25
std::shared_ptr< Logger > LoggerPtr
Definition: defaultloggerfactory.h:27