mirror of https://github.com/gabime/spdlog.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
4.4 KiB
C++
147 lines
4.4 KiB
C++
![]()
8 years ago
|
//
|
||
![]()
10 years ago
|
// Copyright(c) 2015 Gabi Melman.
|
||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||
|
//
|
||
|
//
|
||
|
// spdlog usage example
|
||
|
//
|
||
![]()
9 years ago
|
//
|
||
![]()
8 years ago
|
|
||
|
#define SPDLOG_TRACE_ON
|
||
|
#define SPDLOG_DEBUG_ON
|
||
|
|
||
![]()
10 years ago
|
#include "spdlog/spdlog.h"
|
||
|
|
||
|
#include <iostream>
|
||
![]()
7 years ago
|
#include <string>
|
||
![]()
10 years ago
|
#include <memory>
|
||
|
|
||
|
void async_example();
|
||
|
void syslog_example();
|
||
![]()
9 years ago
|
void android_example();
|
||
![]()
9 years ago
|
void user_defined_example();
|
||
![]()
9 years ago
|
void err_handler_example();
|
||
![]()
10 years ago
|
|
||
|
namespace spd = spdlog;
|
||
![]()
8 years ago
|
int main(int, char *[])
|
||
![]()
10 years ago
|
{
|
||
![]()
9 years ago
|
try
|
||
|
{
|
||
![]()
9 years ago
|
// Console logger with color
|
||
![]()
9 years ago
|
auto console = spd::stdout_color_mt("console");
|
||
![]()
9 years ago
|
console->info("Welcome to spdlog!");
|
||
![]()
9 years ago
|
console->error("Some error message with arg{}..", 1);
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
// Formatting examples
|
||
|
console->warn("Easy padding in numbers like {:08d}", 12);
|
||
|
console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
|
||
|
console->info("Support for floats {:03.2f}", 1.23456);
|
||
|
console->info("Positional args are {1} {0}..", "too", "supported");
|
||
|
console->info("{:<30}", "left aligned");
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function");
|
||
|
|
||
|
// Customize msg format for all messages
|
||
![]()
8 years ago
|
spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v");
|
||
![]()
8 years ago
|
console->info("This an info message with custom format");
|
||
|
console->error("This an error message with custom format");
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Runtime log levels
|
||
![]()
8 years ago
|
spd::set_level(spd::level::info); // Set global log level to info
|
||
![]()
8 years ago
|
console->debug("This message should not be displayed!");
|
||
![]()
9 years ago
|
console->set_level(spd::level::debug); // Set specific logger's log level
|
||
![]()
8 years ago
|
console->debug("This message should be displayed..");
|
||
![]()
9 years ago
|
|
||
|
// Compile time log levels
|
||
|
// define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON
|
||
![]()
9 years ago
|
SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23);
|
||
|
SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23);
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Asynchronous logging is very fast..
|
||
|
// Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous..
|
||
|
async_example();
|
||
|
|
||
![]()
9 years ago
|
// syslog example. linux/osx only
|
||
![]()
9 years ago
|
syslog_example();
|
||
|
|
||
![]()
9 years ago
|
// android example. compile with NDK
|
||
|
android_example();
|
||
|
|
||
![]()
9 years ago
|
// Log user-defined types example
|
||
![]()
9 years ago
|
user_defined_example();
|
||
|
|
||
![]()
9 years ago
|
// Change default log error handler
|
||
|
err_handler_example();
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
// Apply a function on all registered loggers
|
||
![]()
8 years ago
|
spd::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); });
|
||
![]()
9 years ago
|
|
||
|
// Release and close all loggers
|
||
![]()
9 years ago
|
spdlog::drop_all();
|
||
|
}
|
||
![]()
9 years ago
|
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
|
||
![]()
8 years ago
|
catch (const spd::spdlog_ex &ex)
|
||
![]()
9 years ago
|
{
|
||
![]()
9 years ago
|
std::cout << "Log init failed: " << ex.what() << std::endl;
|
||
|
return 1;
|
||
![]()
9 years ago
|
}
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
void async_example()
|
||
|
{
|
||
![]()
7 years ago
|
size_t q_size = 4096;
|
||
![]()
9 years ago
|
spdlog::set_async_mode(q_size);
|
||
![]()
8 years ago
|
auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt");
|
||
![]()
9 years ago
|
for (int i = 0; i < 100; ++i)
|
||
![]()
8 years ago
|
{
|
||
![]()
9 years ago
|
async_file->info("Async message #{}", i);
|
||
![]()
8 years ago
|
}
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
// syslog example (linux/osx/freebsd)
|
||
![]()
10 years ago
|
void syslog_example()
|
||
|
{
|
||
![]()
9 years ago
|
#ifdef SPDLOG_ENABLE_SYSLOG
|
||
![]()
9 years ago
|
std::string ident = "spdlog-example";
|
||
|
auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID);
|
||
![]()
9 years ago
|
syslog_logger->warn("This is warning that will end up in syslog.");
|
||
![]()
10 years ago
|
#endif
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// Android example
|
||
|
void android_example()
|
||
|
{
|
||
|
#if defined(__ANDROID__)
|
||
|
std::string tag = "spdlog-android";
|
||
|
auto android_logger = spd::android_logger("android", tag);
|
||
|
android_logger->critical("Use \"adb shell logcat\" to view this message.");
|
||
|
#endif
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// user defined types logging by implementing operator<<
|
||
![]()
9 years ago
|
struct my_type
|
||
|
{
|
||
![]()
9 years ago
|
int i;
|
||
![]()
8 years ago
|
template<typename OStream>
|
||
|
friend OStream &operator<<(OStream &os, const my_type &c)
|
||
![]()
9 years ago
|
{
|
||
![]()
8 years ago
|
return os << "[my_type i=" << c.i << "]";
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
};
|
||
|
|
||
![]()
8 years ago
|
#include "spdlog/fmt/ostr.h" // must be included
|
||
![]()
9 years ago
|
void user_defined_example()
|
||
|
{
|
||
![]()
8 years ago
|
spd::get("console")->info("user defined type: {}", my_type{14});
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
//
|
||
![]()
8 years ago
|
// custom error handler
|
||
![]()
9 years ago
|
//
|
||
|
void err_handler_example()
|
||
![]()
9 years ago
|
{
|
||
![]()
8 years ago
|
// can be set globaly or per logger(logger->set_error_handler(..))
|
||
|
spdlog::set_error_handler([](const std::string &msg) { std::cerr << "my err handler: " << msg << std::endl; });
|
||
![]()
9 years ago
|
spd::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3);
|
||
![]()
9 years ago
|
}
|