Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::writer_factory Class Reference

Factory for creating log writer instances. More...

#include <writer_factory.h>

Collaboration diagram for kcenon::logger::writer_factory:
Collaboration graph

Public Types

using creator_fn = std::function<log_writer_ptr()>
 

Static Public Member Functions

static log_writer_ptr create_console (bool use_stderr=false, bool auto_detect_color=true)
 Create a console writer.
 
static log_writer_ptr create_file (const std::string &filename, bool append=true, std::size_t buffer_size=8192)
 Create a file writer.
 
static log_writer_ptr create_rotating_file (const std::string &filename, std::size_t max_size, std::size_t max_files, std::size_t check_interval=100)
 Create a rotating file writer (size-based)
 
static log_writer_ptr create_rotating_file (const std::string &filename, rotation_type type, std::size_t max_files, std::size_t check_interval=100)
 Create a rotating file writer (time-based)
 
static log_writer_ptr create_network (const std::string &host, uint16_t port, network_writer::protocol_type protocol=network_writer::protocol_type::tcp, std::size_t buffer_size=8192, std::chrono::seconds reconnect_interval=std::chrono::seconds(5))
 Create a network writer.
 
static log_writer_ptr create_batch (log_writer_ptr writer, std::size_t batch_size=100, std::chrono::milliseconds flush_interval=std::chrono::milliseconds(1000))
 Wrap a writer with batch processing.
 
static log_writer_ptr create_development ()
 Create a development preset writer.
 
static log_writer_ptr create_production (const std::string &log_directory="./logs", const std::string &filename="app.log")
 Create a production preset writer.
 
static log_writer_ptr create_high_performance (const std::string &filename="./logs/app.log")
 Create a high-performance preset writer.
 
static void register_type (const std::string &name, creator_fn creator)
 Register a custom writer type.
 
static log_writer_ptr create (const std::string &name)
 Create a writer by registered name.
 
static bool has_type (const std::string &name)
 Check if a writer type is registered.
 

Static Private Member Functions

static std::unordered_map< std::string, creator_fn > & registry ()
 

Detailed Description

Factory for creating log writer instances.

Provides static factory methods for all built-in writer types and a registry pattern for custom writer types.

Since
2.0.0

Definition at line 40 of file writer_factory.h.

Member Typedef Documentation

◆ creator_fn

Definition at line 42 of file writer_factory.h.

Member Function Documentation

◆ create()

static log_writer_ptr kcenon::logger::writer_factory::create ( const std::string & name)
inlinestatic

Create a writer by registered name.

Parameters
nameRegistered type name
Returns
Unique pointer to writer, or nullptr if not found

Definition at line 214 of file writer_factory.h.

214 {
215 auto it = registry().find(name);
216 if (it == registry().end()) {
217 return nullptr;
218 }
219 return it->second();
220 }
static std::unordered_map< std::string, creator_fn > & registry()

References registry().

Here is the call graph for this function:

◆ create_batch()

static log_writer_ptr kcenon::logger::writer_factory::create_batch ( log_writer_ptr writer,
std::size_t batch_size = 100,
std::chrono::milliseconds flush_interval = std::chrono::milliseconds(1000) )
inlinestatic

Wrap a writer with batch processing.

Parameters
writerThe writer to wrap
batch_sizeMaximum batch size
flush_intervalAuto-flush interval
Returns
Unique pointer to batch writer

Definition at line 142 of file writer_factory.h.

146 {
147 batch_writer::config cfg;
148 cfg.max_batch_size = batch_size;
149 cfg.flush_interval = flush_interval;
150 return std::make_unique<batch_writer>(std::move(writer), cfg);
151 }

References kcenon::logger::batch_writer::config::flush_interval, and kcenon::logger::batch_writer::config::max_batch_size.

Referenced by create_high_performance(), and create_production().

Here is the caller graph for this function:

◆ create_console()

static log_writer_ptr kcenon::logger::writer_factory::create_console ( bool use_stderr = false,
bool auto_detect_color = true )
inlinestatic

Create a console writer.

Parameters
use_stderrSend output to stderr (default: stdout)
auto_detect_colorAuto-detect terminal color support
Returns
Unique pointer to console writer

Definition at line 54 of file writer_factory.h.

57 {
58 return std::make_unique<console_writer>(use_stderr, auto_detect_color);
59 }

Referenced by create_development().

Here is the caller graph for this function:

◆ create_development()

static log_writer_ptr kcenon::logger::writer_factory::create_development ( )
inlinestatic

Create a development preset writer.

Returns
Console writer with colors enabled

Definition at line 161 of file writer_factory.h.

161 {
162 return create_console(false, true);
163 }
static log_writer_ptr create_console(bool use_stderr=false, bool auto_detect_color=true)
Create a console writer.

References create_console().

Here is the call graph for this function:

◆ create_file()

static log_writer_ptr kcenon::logger::writer_factory::create_file ( const std::string & filename,
bool append = true,
std::size_t buffer_size = 8192 )
inlinestatic

Create a file writer.

Parameters
filenamePath to log file
appendAppend to existing file (default: true)
buffer_sizeOutput buffer size
Returns
Unique pointer to file writer

Definition at line 68 of file writer_factory.h.

72 {
73 return std::make_unique<file_writer>(filename, append, buffer_size);
74 }

◆ create_high_performance()

static log_writer_ptr kcenon::logger::writer_factory::create_high_performance ( const std::string & filename = "./logs/app.log")
inlinestatic

Create a high-performance preset writer.

Parameters
filenameLog file path
Returns
Batched file writer with large buffers

Definition at line 189 of file writer_factory.h.

191 {
192 auto file = std::make_unique<file_writer>(filename, true, 65536);
193 return create_batch(std::move(file), 500, std::chrono::milliseconds(5000));
194 }
static log_writer_ptr create_batch(log_writer_ptr writer, std::size_t batch_size=100, std::chrono::milliseconds flush_interval=std::chrono::milliseconds(1000))
Wrap a writer with batch processing.

References create_batch().

Here is the call graph for this function:

◆ create_network()

static log_writer_ptr kcenon::logger::writer_factory::create_network ( const std::string & host,
uint16_t port,
network_writer::protocol_type protocol = network_writer::protocol_type::tcp,
std::size_t buffer_size = 8192,
std::chrono::seconds reconnect_interval = std::chrono::seconds(5) )
inlinestatic

Create a network writer.

Parameters
hostRemote host address
portRemote port
protocolNetwork protocol (TCP/UDP)
buffer_sizeInternal buffer size
reconnect_intervalReconnection interval
Returns
Unique pointer to network writer

Definition at line 123 of file writer_factory.h.

129 {
130 return std::make_unique<network_writer>(
131 host, port, protocol, buffer_size, reconnect_interval
132 );
133 }

◆ create_production()

static log_writer_ptr kcenon::logger::writer_factory::create_production ( const std::string & log_directory = "./logs",
const std::string & filename = "app.log" )
inlinestatic

Create a production preset writer.

Parameters
log_directoryDirectory for log files
filenameLog file name
Returns
Batched rotating file writer

Definition at line 171 of file writer_factory.h.

174 {
175 std::string full_path = log_directory + "/" + filename;
176 auto rotating = create_rotating_file(
177 full_path,
178 100 * 1024 * 1024, // 100MB
179 10
180 );
181 return create_batch(std::move(rotating), 200, std::chrono::milliseconds(2000));
182 }
static log_writer_ptr create_rotating_file(const std::string &filename, std::size_t max_size, std::size_t max_files, std::size_t check_interval=100)
Create a rotating file writer (size-based)

References create_batch(), and create_rotating_file().

Here is the call graph for this function:

◆ create_rotating_file() [1/2]

static log_writer_ptr kcenon::logger::writer_factory::create_rotating_file ( const std::string & filename,
rotation_type type,
std::size_t max_files,
std::size_t check_interval = 100 )
inlinestatic

Create a rotating file writer (time-based)

Parameters
filenamePath to log file
typeRotation type (daily/hourly)
max_filesMaximum number of backup files
check_intervalWrites between rotation checks
Returns
Unique pointer to rotating file writer

Definition at line 103 of file writer_factory.h.

108 {
109 return std::make_unique<rotating_file_writer>(
110 filename, type, max_files, check_interval
111 );
112 }

◆ create_rotating_file() [2/2]

static log_writer_ptr kcenon::logger::writer_factory::create_rotating_file ( const std::string & filename,
std::size_t max_size,
std::size_t max_files,
std::size_t check_interval = 100 )
inlinestatic

Create a rotating file writer (size-based)

Parameters
filenamePath to log file
max_sizeMaximum file size before rotation
max_filesMaximum number of backup files
check_intervalWrites between rotation checks
Returns
Unique pointer to rotating file writer

Definition at line 84 of file writer_factory.h.

89 {
90 return std::make_unique<rotating_file_writer>(
91 filename, max_size, max_files, check_interval
92 );
93 }

Referenced by create_production().

Here is the caller graph for this function:

◆ has_type()

static bool kcenon::logger::writer_factory::has_type ( const std::string & name)
inlinestatic

Check if a writer type is registered.

Parameters
nameType name to check
Returns
true if registered

Definition at line 227 of file writer_factory.h.

227 {
228 return registry().find(name) != registry().end();
229 }

References registry().

Here is the call graph for this function:

◆ register_type()

static void kcenon::logger::writer_factory::register_type ( const std::string & name,
creator_fn creator )
inlinestatic

Register a custom writer type.

Parameters
nameType name for the writer
creatorFactory function to create the writer

Definition at line 205 of file writer_factory.h.

205 {
206 registry()[name] = std::move(creator);
207 }

References registry().

Here is the call graph for this function:

◆ registry()

static std::unordered_map< std::string, creator_fn > & kcenon::logger::writer_factory::registry ( )
inlinestaticprivate

Definition at line 232 of file writer_factory.h.

232 {
233 static std::unordered_map<std::string, creator_fn> instance;
234 return instance;
235 }

Referenced by create(), has_type(), and register_type().

Here is the caller graph for this function:

The documentation for this class was generated from the following file: