### Using Convenience Logger Aliases in C++ Source: https://github.com/christianpanov/lwlog/blob/master/README.md This example illustrates the use of lwlog's convenience aliases for creating loggers. It shows how to instantiate a basic_logger with a stdout_sink, a console_logger for standard output, and a file_logger for logging to a specified file path. These aliases simplify logger setup with predefined default configurations. It requires the lwlog.h header. ```C++ #include "lwlog.h" int main() { auto basic = std::make_shared>("CONSOLE"); auto console = std::make_shared("CONSOLE"); auto file = std::make_shared("FILE", "C:/Users/user/Desktop/LogFolder/LOGS.txt"); return 0; } ``` -------------------------------- ### Installing lwlog Headers in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This section includes standard CMake modules for installation and then configures the installation of header files from the 'lwlog/include' directory to the system's standard include path. ```CMake include(GNUInstallDirs) include(CMakePackageConfigHelpers) install( DIRECTORY ${CMAKE_SOURCE_DIR}/lwlog/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/lwlog FILES_MATCHING PATTERN "*.h" ) ``` -------------------------------- ### Installing lwlog CMake Package Source: https://github.com/christianpanov/lwlog/blob/master/README.md This CMake command builds and installs the lwlog library to the specified installation directory. Users can choose between 'Debug' or 'Release' configurations for the build, affecting optimization and debugging symbols. ```CMake cmake --build --target install --config ``` -------------------------------- ### Configuring lwlog CMake Package Source: https://github.com/christianpanov/lwlog/blob/master/README.md This CMake command configures the lwlog project for building, specifying the build directory, source directory, and the desired installation prefix. It's the initial step in preparing the CMake package for installation. ```CMake cmake -B -S -DCMAKE_INSTALL_PREFIX= ``` -------------------------------- ### Installing lwlog Library Targets in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This snippet configures the installation of the 'lwlog_lib' target, specifying its destination for libraries and includes, and exporting its targets for use by other CMake projects. ```CMake install(TARGETS lwlog_lib EXPORT lwlog_libTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) ``` -------------------------------- ### Example Output for lwlog Topic Feature Source: https://github.com/christianpanov/lwlog/blob/master/README.md This snippet displays the expected console output when using `lwlog`'s topic feature. It illustrates how messages logged within nested topics are prefixed with the full topic path, demonstrating the hierarchical organization. ```text Main Topic/Sub Topic [19:44:50] [CONSOLE] [critical]: Message in both topics Main Topic [19:44:50] [CONSOLE] [critical]: Message in main topic ``` -------------------------------- ### Installing lwlog Package Configuration Files in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This snippet installs the generated CMake package configuration files ('lwlog_lib-config.cmake' and 'lwlog_lib-configVersion.cmake') to the designated CMake library directory. ```CMake install( FILES "${CMAKE_CURRENT_BINARY_DIR}/lwlog_lib-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/lwlog_lib-configVersion.cmake" DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake ) ``` -------------------------------- ### Categorizing Logs with Topics and Subtopics (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This example showcases `lwlog`'s topic management feature, enabling categorization of log messages. It demonstrates how to define a main topic and a nested subtopic using `start_topic()` and `end_topic()`, with the `{full_topic}` flag in the pattern to display the topic hierarchy. ```cpp #include "lwlog.h" int main() { auto console = std::make_shared("CONSOLE"); console->set_pattern("{full_topic} .red([%T] [%n]) .level([%l]): .cyan(%v)"); console->start_topic("Main Topic"); { console->start_topic("Sub Topic"); { console->critical("Message in both topics"); } console->end_topic(); console->critical("Message in main topic"); } console->end_topic(); return 0; } ``` -------------------------------- ### Applying Level-based Colors to Log Messages (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This example illustrates the use of the `.level()` flag in `lwlog` patterns to automatically color log messages based on their severity. It sets up a console logger with a pattern that dynamically colors the log level according to its predefined severity color. ```cpp #include "lwlog.h" int main() { auto console = std::make_shared("CONSOLE"); console->set_pattern(".red([%T] [%n]) .level([%l]): .cyan(%v)"); console->critical("First critical message"); return 0; } ``` -------------------------------- ### Exporting lwlog Library Targets for CMake Packages Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This code block installs the exported 'lwlog_libTargets' to a CMake-specific directory, allowing other projects to find and link against 'lwlog' using CMake's `find_package` mechanism. ```CMake install(EXPORT lwlog_libTargets FILE lwlog_lib-targets.cmake NAMESPACE lwlog:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake ) ``` -------------------------------- ### Complete stdout_sink Class Definition (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This comprehensive C++ example provides the full class definition and implementation for `stdout_sink` within the `lwlog::sinks` namespace. It illustrates the inheritance from the base `sink` class and `stream_writer`, the constructor initialization with `stdout`, and the complete `sink_it()` method. This snippet serves as a blueprint for creating stream-based log sinks that output to standard streams. ```C++ namespace lwlog::sinks { template class stdout_sink : public sink , private details::stream_writer { private: using sink_t = sink; public: stdout_sink(); void sink_it(const details::record& record) override; }; template stdout_sink::stdout_sink() : details::stream_writer(stdout) {} template void stdout_sink::sink_it(const details::record& record) { sink_t::m_current_level = record.log_level; details::stream_writer::write(sink_t::m_pattern.compile(record)); sink_t::m_pattern.reset_pattern(); } } ``` -------------------------------- ### Configuring lwlog Package Config File in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This code configures the 'lwlog_lib-config.cmake' file from a template, specifying its installation destination, which helps CMake locate the library's configuration during package discovery. ```CMake configure_package_config_file( ${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/lwlog_lib-config.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake ) ``` -------------------------------- ### Generating lwlog Package Version File in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This snippet generates a basic package version file, 'lwlog_lib-configVersion.cmake', which is crucial for CMake's `find_package` command to determine compatibility with the installed library. ```CMake write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/lwlog_lib-configVersion.cmake" VERSION ${version} COMPATIBILITY AnyNewerVersion ) ``` -------------------------------- ### Configuring Multiple Sinks at Compile-Time in lwlog (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This example illustrates how to configure a lwlog logger with multiple sinks (stdout and file) at compile-time using template parameters. The logger is initialized with a name and a file path for the file sink, and a common pattern is set for all sinks. Log messages are then distributed to both configured sinks. ```C++ #include "lwlog.h" int main() { auto logger = std::make_shared< lwlog::logger< lwlog::default_memory_buffer_limits, lwlog::synchronous_policy, lwlog::default_flush_policy, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink lwlog::sinks::file_sink > >("LOGGER", "C:/Users/user/Desktop/LogFolder/LOGS.txt"); // Color attributes will be ignored for the file sink logger->set_pattern("[%T] [%n] [%l]: %v"); // Log message will be distributed to both sinks logger->critical("First critical message"); return 0; } ``` -------------------------------- ### Initializing Synchronous Console Logger in C++ Source: https://github.com/christianpanov/lwlog/blob/master/README.md This snippet demonstrates how to initialize a synchronous logger named 'CONSOLE' using the lwlog library. It configures the logger with default memory buffer limits, a synchronous policy, default flush policy, single-threaded policy, and directs output to the standard output sink. This setup is suitable for basic, blocking log operations. ```C++ #include "lwlog.h" int main() { auto console = std::make_shared< lwlog::logger< lwlog::default_memory_buffer_limits, lwlog::synchronous_policy, lwlog::default_flush_policy, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink > >("CONSOLE"); return 0; } ``` -------------------------------- ### Finding lwlog CMake Package Source: https://github.com/christianpanov/lwlog/blob/master/README.md This CMake command locates the installed lwlog library package within your project. The HINTS argument helps CMake find the package if it's not in a standard system location, ensuring proper dependency resolution. ```CMake find_package(lwlog_lib HINTS /lib/cmake) ``` -------------------------------- ### Configuring lwlog Logger with Custom Buffer Limits in C++ Source: https://github.com/christianpanov/lwlog/blob/master/README.md This C++ example demonstrates how to initialize an `lwlog::logger` instance with custom memory buffer limits using `lwlog::memory_buffer_limits`. It specifies maximum lengths for patterns, messages, arguments, argument counts, padding, and conversion flags, tailoring the logger's resource usage. The logger is configured with synchronous, default flush, and single-threaded policies, outputting to `stdout_sink`. ```C++ #include "lwlog.h" int main() { using buffer_limits = lwlog::memory_buffer_limits< lwlog::pattern_limit<256>, lwlog::message_limit<128>, lwlog::argument_limit<12>, lwlog::arg_count_limit<4>, lwlog::padding_limit<24>, lwlog::conv_limit<64> >; auto console = std::make_shared< lwlog::logger< buffer_limits, lwlog::synchronous_policy, lwlog::default_flush_policy, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink > >("CONSOLE"); return 0; } ``` -------------------------------- ### Configuring Asynchronous Console Logger in C++ Source: https://github.com/christianpanov/lwlog/blob/master/README.md This example illustrates the configuration of an asynchronous logger in lwlog. It uses `lwlog::asynchronous_policy` with default queue overflow policy, default queue size, and default thread affinity. The logger is named 'CONSOLE' and outputs to stdout, demonstrating how to log a critical message asynchronously. ```C++ #include "lwlog.h" int main() { auto console = std::make_shared< lwlog::logger< lwlog::default_memory_buffer_limits, lwlog::asynchronous_policy< lwlog::default_overflow_policy, lwlog::default_async_queue_size, lwlog::default_thread_affinity >, lwlog::default_flush_policy, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink > >("CONSOLE"); console->critical("First critical message"); return 0; } ``` -------------------------------- ### Inheriting from Base Sink Class for Custom Sinks in lwlog (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This code snippet illustrates the first step in creating a custom lwlog sink: inheriting from the lwlog::sinks::sink base class. It shows stdout_sink as an example, demonstrating how to use template parameters for buffer limits, flush policy, and threading policy, and how to include details::stream_writer for stream writing capabilities. Custom sinks must also implement the sink_it() method. ```C++ namespace lwlog::sinks { template class stdout_sink : public sink , private details::stream_writer { // ... }; } ``` -------------------------------- ### Initializing Project and Setting Version in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This snippet sets the minimum required CMake version, defines the project name, finds the 'Threads' package, displays the compiler version, and sets the project's version number. ```CMake cmake_minimum_required(VERSION 3.16) project(lwlog) find_package(Threads REQUIRED) message("Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}") set(version 1.3.0) ``` -------------------------------- ### Using FMT-style Message Formatting (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This snippet demonstrates `lwlog`'s support for FMT-style formatting, allowing dynamic insertion of values into log messages using placeholders. It shows how to pass multiple arguments ("foo", 5, "bar") to a critical log message, which will be formatted into the output string. ```cpp #include "lwlog.h" int main() { auto console = std::make_shared("CONSOLE"); console->set_pattern(".red([%T] [%n]) .level([%l]): .cyan(%v)"); console->critical("First critical message {} {} {}", "foo", 5, "bar"); return 0; } ``` -------------------------------- ### Creating lwlog Sandbox Executable in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This code block defines an executable target named 'lwlog_sandbox' using a specific source file and includes the necessary header directories for the lwlog library. ```CMake message("Creating lwlog sandbox") add_executable(lwlog_sandbox ${CMAKE_SOURCE_DIR}/Sandbox/Sandbox.cpp) target_include_directories(lwlog_sandbox PRIVATE ${CMAKE_SOURCE_DIR}/lwlog/include/) ``` -------------------------------- ### Creating and Configuring lwlog Static Library in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This snippet creates a static library named 'lwlog_lib' from the defined source files, sets its C++ standard to C++17, and includes necessary private directories. An alias 'lwlog::lwlog_lib' is also created for easier linking. ```CMake message("Creating lwlog library") add_library(lwlog_lib STATIC ${LWLOG_SOURCE_FILES}) target_compile_features(lwlog_lib PUBLIC cxx_std_17) target_include_directories(lwlog_lib PRIVATE ${CMAKE_SOURCE_DIR}/lwlog/include/) add_library(lwlog::lwlog_lib ALIAS lwlog_lib) ``` -------------------------------- ### Linking Libraries for lwlog Targets in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This snippet links the 'lwlog_lib' with the 'Threads' library and links the 'lwlog_sandbox' executable with the 'lwlog_lib', establishing their dependencies. ```CMake message("Linking libraries") target_link_libraries(lwlog_lib PRIVATE Threads::Threads) target_link_libraries(lwlog_sandbox PRIVATE lwlog_lib) ``` -------------------------------- ### Cloning lwlog Repository using Git Source: https://github.com/christianpanov/lwlog/blob/master/README.md This command clones the lwlog repository from GitHub, including all submodules, which is the first step to obtaining the library. It ensures all necessary dependencies are fetched. ```Shell git clone --recursive https://github.com/ChristianPanov/lwlog ``` -------------------------------- ### Configuring Console Logger with Color Flags (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This snippet demonstrates how to apply foreground and background colors to log patterns using `lwlog`. It initializes a console logger and sets a pattern that colors the timestamp, logger name, log level, and message using `.red()`, `.green()`, and `.cyan()` flags respectively. ```cpp #include "lwlog.h" int main() { auto console = std::make_shared("CONSOLE"); console->set_pattern(".red([%T] [%n]) .green([%l]): .cyan(%v)"); console->critical("First critical message"); return 0; } ``` -------------------------------- ### Configuring Multiple Sinks at Runtime in lwlog (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This snippet demonstrates various ways to configure lwlog loggers with multiple sinks at runtime. It shows creating individual sink instances (console and file) and then initializing different logger types using an iterator range of sinks, a sink list, a single sink, or a combination of template-defined and runtime-provided sinks. ```C++ #include "lwlog.h" int main() { auto console_sink = std::make_shared>(); auto file_sink = std::make_shared>("C:/Users/user/Desktop/LogFolder/LOGS.txt"); lwlog::sink_list sinks = { console_sink, file_sink }; auto logger_iterator = std::make_shared>("ITERATOR", sinks.begin(), sinks.end()); auto logger_sink_list = std::make_shared>("SINK_LIST", sinks); auto logger_single_sink = std::make_shared>("SINGLE_SINK", console_sink); auto logger_combined = std::make_shared< lwlog::logger< lwlog::default_memory_buffer_limits, lwlog::synchronous_policy, lwlog::default_flush_policy, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink > >("COMBINED", file_sink); return 0; } ``` -------------------------------- ### Initializing a Custom Console Logger in C++ Source: https://github.com/christianpanov/lwlog/blob/master/README.md This snippet demonstrates how to initialize a custom lwlog::logger instance for console output. It configures the logger with specific policies for memory buffering, logging, flushing, and threading, sets a level filter for info, debug, and critical messages, defines a custom output pattern, and then logs a critical message. It requires the lwlog.h header. ```C++ #include "lwlog.h" int main() { auto console = std::make_shared< lwlog::logger< lwlog::default_memory_buffer_limits, lwlog::default_log_policy, lwlog::default_flush_policy, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink > >("CONSOLE"); console->set_level_filter(lwlog::level::info | lwlog::level::debug | lwlog::level::critical); console->set_pattern("[%T] [%n] [%l]: %v"); console->critical("First critical message"); return 0; } ``` -------------------------------- ### Exporting lwlog Targets to Binary Directory in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This command exports the 'lwlog_libTargets' to a file within the build's binary directory, making them available for use by other CMake projects that might be built alongside or depend on this project. ```CMake export(EXPORT lwlog_libTargets FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/lwlog_lib-targets.cmake" NAMESPACE lwlog:: ) ``` -------------------------------- ### Linking lwlog to a C++ Project with CMake Source: https://github.com/christianpanov/lwlog/blob/master/README.md This CMake command links the lwlog library to a target executable or library named 'MyExe'. The 'PRIVATE' keyword ensures that lwlog is only linked to 'MyExe' and its symbols are not exposed to other targets that link 'MyExe'. ```CMake target_link_libraries(MyExe PRIVATE lwlog::lwlog_lib) ``` -------------------------------- ### Defining Source Files for lwlog Library in CMake Source: https://github.com/christianpanov/lwlog/blob/master/CMakeLists.txt This section defines the source files that constitute the 'lwlog' library, using the CMAKE_SOURCE_DIR variable to specify their absolute paths. ```CMake message("Adding source files") set(LWLOG_SOURCE_FILES ${CMAKE_SOURCE_DIR}/lwlog/include/details/topic_registry.cpp ${CMAKE_SOURCE_DIR}/lwlog/include/details/pattern/attribute.cpp ) ``` -------------------------------- ### Configuring lwlog Console Logger with Custom Pattern in C++ Source: https://github.com/christianpanov/lwlog/blob/master/README.md This C++ snippet demonstrates how to initialize a lwlog::console_logger, set a custom log pattern, and then log messages at different levels. The pattern `[%T] [%n] [:^12%l]: %v` includes timestamp, logger name, a centered log level (12 characters wide), and the log message. It shows basic usage of `info` and `critical` logging functions. ```C++ #include "lwlog.h" int main() { auto console = std::make_shared("CONSOLE"); console->set_pattern("[%T] [%n] [:^12%l]: %v"); console->info("First info message"); console->critical("First critical message"); return 0; } ``` -------------------------------- ### Defining a Custom Log Sink (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This C++ snippet outlines the structure for creating a new custom log sink, `new_custom_sink`, by inheriting from the `lwlog::sinks::sink` base class. It highlights the `sink_it()` method where custom log message processing logic should be implemented, emphasizing the importance of resetting the pattern after processing. This template allows developers to integrate lwlog with diverse logging destinations. ```C++ #include "sink.h" #include "policy/sink_color_policy.h" namespace lwlog::sinks { template class new_custom_sink : public sink { using sink_t = sink; public: void sink_it(const details::record& record) override { sink_t::m_current_level = record.log_level; // sink message to somewhere sink_t::m_pattern.reset_pattern(); } }; } ``` -------------------------------- ### Aligning Atomic Indices for Cache Efficiency in lwlog (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This C++ snippet illustrates how lwlog mitigates false sharing by aligning atomic read and write indices to cache lines. It leverages `std::hardware_destructive_interference_size` to determine the optimal cache line size and `alignas` to ensure that `m_write_index` and `m_read_index` reside on separate cache lines, preventing unnecessary cache invalidations and enhancing performance in multi-threaded ring buffer implementations. ```C++ static constexpr auto cache_line_size{ std::hardware_destructive_interference_size }; alignas(cache_line_size) std::atomic_size_t m_write_index{}; alignas(cache_line_size) std::atomic_size_t m_read_index{}; ``` -------------------------------- ### Configuring lwlog for Local Time via CMake Source: https://github.com/christianpanov/lwlog/blob/master/README.md This CMake command demonstrates how to build the lwlog library with the `LWLOG_LOCALTIME` macro enabled. By passing `-DCMAKE_CXX_FLAGS="-DLWLOG_LOCALTIME"` during CMake configuration, the logger is instructed to use local time for its time-based attributes instead of the default UTC, providing flexibility in timestamp formatting. ```CMake cmake -B -S \ -DCMAKE_INSTALL_PREFIX= \ -DCMAKE_CXX_FLAGS="-DLWLOG_LOCALTIME" ``` -------------------------------- ### Implementing sink_it() for lwlog Sinks (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This snippet demonstrates the core `sink_it()` function implementation for an lwlog sink, specifically `stdout_sink`. It processes a log record by setting the current log level, compiling the message using the sink's pattern, writing the formatted output via a `stream_writer`, and crucially, resetting the pattern to prevent data bleeding in subsequent logs. This function is central to how a sink handles individual log messages. ```C++ template void stdout_sink::sink_it(const details::record& record) { sink_t::m_current_level = record.log_level; details::stream_writer::write(sink_t::m_pattern.compile(record)); sink_t::m_pattern.reset_pattern(); } ``` -------------------------------- ### Optimizing Console Output Buffer in C++ Source: https://github.com/christianpanov/lwlog/blob/master/README.md This C++ snippet demonstrates how to manually increase the buffer size for stdout using `std::setvbuf` to improve I/O throughput in logging solutions. It then shows a basic `std::fwrite` call. Increasing the buffer can reduce flushes but might consume more memory and potentially lead to truncated output in specific scenarios. ```cpp std::setvbuf(stdout, NULL, _IOFBF, 4194304); std::fwrite("Hello, World!", 14, 1, stdout); ``` -------------------------------- ### Using Custom Attributes with Callbacks in lwlog (C++) Source: https://github.com/christianpanov/lwlog/blob/master/README.md This snippet demonstrates how to use custom attributes with lwlog::console_logger. It shows adding a dynamic attribute '{status}' that updates its value before logging, and setting a custom log pattern to include this attribute. The lwlog library automatically invokes a default callback to convert the attribute's value to a string. ```C++ #include "lwlog.h" int main() { std::string current_status = "inactive"; auto console = std::make_shared("CONSOLE"); console->add_attribute("{status}", current_status); console->set_pattern("{status} --- [%T] [%n] [%l]: %v"); current_status = "active"; console->info("First info message"); return 0; } ``` -------------------------------- ### Defining Default lwlog Memory Buffer Limits in C++ Source: https://github.com/christianpanov/lwlog/blob/master/README.md This C++ snippet defines `lwlog::default_memory_buffer_limits` as a type alias for `lwlog::memory_buffer_limits`. This alias provides a convenient way to use a predefined set of buffer limits for the logger, including default maximums for pattern length, message length, argument length, argument count, padding, and conversion flag length, without explicitly specifying each limit. ```C++ using default_memory_buffer_limits = lwlog::memory_buffer_limits< lwlog::pattern_limit<256>, lwlog::message_limit<128>, lwlog::argument_limit<12>, lwlog::arg_count_limit<4>, lwlog::padding_limit<24>, lwlog::conv_limit<64> >; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.