mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
updated example and added more creation functions
This commit is contained in:
@@ -1,27 +1,45 @@
|
||||
// example.cpp : Simple logger example
|
||||
//
|
||||
#define FFLOG_ENABLE_TRACE
|
||||
// example.cpp : spdlog usage example
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "spitlog/logger.h"
|
||||
#include "spitlog/sinks/stdout_sinks.h"
|
||||
#include "spitlog/sinks/file_sinks.h"
|
||||
using namespace std;
|
||||
using namespace spitlog;
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
|
||||
details::fast_oss f(const std::string& what)
|
||||
{
|
||||
details::fast_oss oss;
|
||||
oss << what;
|
||||
return oss;
|
||||
}
|
||||
int main_(int, char* [])
|
||||
int main(int, char* [])
|
||||
{
|
||||
|
||||
auto foss = f("test2");
|
||||
foss.str();
|
||||
namespace spd = spdlog;
|
||||
try
|
||||
{
|
||||
std::string filename = "spdlog_example";
|
||||
auto console = spd::stderr_logger_mt("console");
|
||||
console->info("Welcome to spdlog!");
|
||||
console->info() << "Creating file " << filename << "..";
|
||||
|
||||
auto file_logger = spd::rotating_logger_mt("file_logger", filename, 1024 * 1024 * 5, 3);
|
||||
file_logger->info("Log file message number", 1);
|
||||
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
auto square = i*i;
|
||||
file_logger->info() << i << '*' << i << '=' << square << " (" << "0x" << std::hex << square << ")";
|
||||
}
|
||||
|
||||
|
||||
// Change log level to all loggers to warning and above
|
||||
spd::set_level(spd::level::WARN);
|
||||
console->info("This should not be displayed");
|
||||
console->warn("This should!");
|
||||
|
||||
// Change format pattern to all loggers
|
||||
spd::set_pattern(" **** %Y-%m-%d %H:%M:%S.%e %l **** %t");
|
||||
spd::get("console")->warn("This is another message with different format");
|
||||
}
|
||||
catch (const spd::spdlog_ex& ex)
|
||||
{
|
||||
std::cout << "Log failed: " << ex.what() << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user