CppLogging  1.0.4.0
C++ Logging Library
async.cpp

Asynchronous logger processor example

#include "logging/config.h"
#include "logging/logger.h"
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
void ConfigureLogger()
{
// Create default logging sink processor with a text layout
auto sink = std::make_shared<CppLogging::AsyncWaitProcessor>(std::make_shared<CppLogging::TextLayout>());
// Add file appender with time-based rolling policy and archivation
sink->appenders().push_back(std::make_shared<CppLogging::RollingFileAppender>(".", CppLogging::TimeRollingPolicy::SECOND, "{UtcDateTime}.log", true));
// Configure example logger
// Startup the logging infrastructure
}
int main(int argc, char** argv)
{
// Configure logger
ConfigureLogger();
std::cout << "Press Enter to stop..." << std::endl;
int concurrency = 1;
// Start some threads
std::atomic<bool> stop(false);
std::vector<std::thread> threads;
for (int thread = 0; thread < concurrency; ++thread)
{
threads.push_back(std::thread([&stop]()
{
// Create example logger
CppLogging::Logger logger("example");
while (!stop)
{
// Log some messages with different level
logger.Debug("Debug message");
logger.Info("Info message");
logger.Warn("Warning message");
logger.Error("Error message");
logger.Fatal("Fatal message");
// Yield for a while...
CppCommon::Thread::Yield();
}
}));
}
// Wait for input
std::cin.get();
// Stop threads
stop = true;
// Wait for all threads
for (auto& thread : threads)
thread.join();
return 0;
}
static void ConfigLogger(const std::shared_ptr< Processor > &sink)
Configure default logger with a given logging sink processor.
Definition: config.cpp:19
static void Startup()
Startup the logging infrastructure.
Definition: config.cpp:68
Logger interface.
Definition: logger.h:23
Logger configuration definition.
Logger interface definition.
@ SECOND
Second rolling policy.