### Configuring Logging Test Executable (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet defines an executable `log_test` for logging functionality, linking it against `fmtlog-static` and `fmt`. The commented line shows an alternative configuration for header-only usage. It also includes an `install` command for deployment. ```CMake add_executable(log_test log_test.cc) target_link_libraries(log_test fmtlog-static fmt) #target_compile_definitions(log_test PUBLIC FMTLOG_HEADER_ONLY) install(TARGETS log_test) ``` -------------------------------- ### Linking Executable with Static fmtlog and Static Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, creates an executable `link_static_static` and links it against the static version of `fmtlog`, `static_lib`, and the `fmt` library. This demonstrates a fully static linking scenario and includes installation. ```CMake add_executable(link_static_static link_test.cc) target_link_libraries(link_static_static fmtlog-static static_lib fmt) install(TARGETS link_static_static) ``` -------------------------------- ### Linking Executable with Header-Only Static Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, creates an executable `link_header_static` and links it against `static_header_lib` and `fmt`. This tests linking with a library configured as header-only and built statically, and includes installation. ```CMake add_executable(link_header_static link_test.cc) target_link_libraries(link_header_static static_header_lib fmt) install(TARGETS link_header_static) ``` -------------------------------- ### Defining and Installing a Conditional Shared Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, defines a shared library named `shared_lib` from `lib.cc` and includes an `install` command to ensure it is installed as part of the project build, making it available for system-wide use. ```CMake add_library(shared_lib SHARED lib.cc) install(TARGETS shared_lib) ``` -------------------------------- ### Configuring Encoding/Decoding Test Executable (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet defines an executable `enc_dec_test` for testing encoding and decoding features. It links against `fmtlog-static` and `fmt` and is set up for installation. ```CMake add_executable(enc_dec_test enc_dec_test.cc) target_link_libraries(enc_dec_test fmtlog-static fmt) install(TARGETS enc_dec_test) ``` -------------------------------- ### Defining and Installing fmtlog Static Library Source: https://github.com/mengrao/fmtlog/blob/main/CMakeLists.txt This snippet defines a static library named `fmtlog-static` from `fmtlog.cc`. Similar to the shared library, it conditionally links against the `fmt` library for MSVC and then installs the static library for use by other projects. ```CMake add_library(fmtlog-static fmtlog.cc) if(MSVC) target_link_libraries(fmtlog-static fmt) endif() install(TARGETS fmtlog-static) ``` -------------------------------- ### Linking Executable with Shared fmtlog and Shared Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, defines an executable `link_shared_shared` and links it with `fmtlog-shared`, `shared_lib`, and `fmt`. This demonstrates a fully shared linking scenario and includes installation. ```CMake add_executable(link_shared_shared link_test.cc) target_link_libraries(link_shared_shared fmtlog-shared shared_lib fmt) install(TARGETS link_shared_shared) ``` -------------------------------- ### Defining and Installing fmtlog Shared Library Source: https://github.com/mengrao/fmtlog/blob/main/CMakeLists.txt This snippet defines a shared library named `fmtlog-shared` from `fmtlog.cc`. It conditionally links against the `fmt` library if compiling with MSVC and then installs the created shared library, making it available for other projects. ```CMake add_library(fmtlog-shared SHARED fmtlog.cc) if(MSVC) target_link_libraries(fmtlog-shared fmt) endif() install(TARGETS fmtlog-shared) ``` -------------------------------- ### Linking Executable with Static fmtlog and Shared Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, defines an executable `link_static_shared` and links it with `fmtlog-static`, `shared_lib`, and `fmt`. This tests a mixed linking scenario where fmtlog is static but another dependency is shared, and includes installation. ```CMake add_executable(link_static_shared link_test.cc) target_link_libraries(link_static_shared fmtlog-static shared_lib fmt) install(TARGETS link_static_shared) ``` -------------------------------- ### Defining and Installing a Conditional Shared Header-Only Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, defines a shared library `shared_header_lib` with the `FMTLOG_HEADER_ONLY` compile definition, indicating it's a header-only shared library. It also includes an `install` command for deployment. ```CMake add_library(shared_header_lib SHARED lib.cc) target_compile_definitions(shared_header_lib PUBLIC FMTLOG_HEADER_ONLY) install(TARGETS shared_header_lib) ``` -------------------------------- ### Linking Executable with Header-Only Shared Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, defines an executable `link_header_shared` and links it with `shared_header_lib` and `fmt`. This tests linking with a library configured as header-only and built as a shared library, and includes installation. ```CMake add_executable(link_header_shared link_test.cc) target_link_libraries(link_header_shared shared_header_lib fmt) install(TARGETS link_header_shared) ``` -------------------------------- ### Linking Executable with Shared fmtlog and Static Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, creates an executable `link_shared_static` and links it against the shared version of `fmtlog`, `static_lib`, and `fmt`. This tests a scenario where fmtlog is shared but another dependency is static, and includes installation. ```CMake add_executable(link_shared_static link_test.cc) target_link_libraries(link_shared_static fmtlog-shared static_lib fmt) install(TARGETS link_shared_static) ``` -------------------------------- ### Configuring Multithread Test Executable (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet defines an executable `multithread_test` for multithreading tests. It sets `FMTLOG_HEADER_ONLY` and links only against `fmt`, implying `fmtlog` is used as header-only without explicit linking to its library target. It is also configured for installation. ```CMake add_executable(multithread_test multithread_test.cc) target_compile_definitions(multithread_test PUBLIC FMTLOG_HEADER_ONLY) #target_link_libraries(multithread_test fmtlog-static fmt) target_link_libraries(multithread_test fmt) install(TARGETS multithread_test) ``` -------------------------------- ### Passing Arguments by Pointer in fmtlog (C++) Source: https://github.com/mengrao/fmtlog/blob/main/README.md This example illustrates fmtlog's optimization for passing arguments by pointer to avoid copy overhead. It shows that while `std::string` content is copied by default, passing a `std::string*` only copies the pointer, requiring the user to assure the referred object's lifetime. The `fmtlog::poll()` call demonstrates when the actual logging occurs and values are resolved. ```C++ std::string str = "aaa"; logi("str: {}, pstr: {}", str, &str); str = "bbb"; fmtlog::poll(); // output: str: aaa, pstr: bbb ``` -------------------------------- ### Basic Logging with fmtlog in C++ Source: https://github.com/mengrao/fmtlog/blob/main/README.md This C++ snippet demonstrates the most basic usage of fmtlog, including the necessary header and a simple log statement using the `FMTLOG` macro at the `INF` (info) level. It shows how to log a formatted message with a placeholder for a variable. ```c++ #include "fmtlog/fmtlog.h" int main() { FMTLOG(fmtlog::INF, "The answer is {}.", 42); } ``` -------------------------------- ### Setting Link and Include Directories for fmtlog Source: https://github.com/mengrao/fmtlog/blob/main/CMakeLists.txt This snippet configures the build environment by adding the current directory to the link search paths and `fmt/include` to the include search paths. This ensures that the compiler and linker can find necessary headers and libraries. ```CMake link_directories(.) include_directories(fmt/include) ``` -------------------------------- ### Using Shortcut Macros and Setting Log Level in C++ Source: https://github.com/mengrao/fmtlog/blob/main/README.md This C++ snippet illustrates the use of shortcut macros like `logi` (info) and `logd` (debug) for logging. It also demonstrates how to dynamically change the global log level at runtime using `fmtlog::setLogLevel` to enable or disable messages of a certain severity, showing how debug messages can be made visible. ```c++ logi("A info msg"); logd("This msg will not be logged as the default log level is INF"); fmtlog::setLogLevel(fmtlog::DBG); logd("Now debug msg is shown"); ``` -------------------------------- ### Building fmtlog with CMake Source: https://github.com/mengrao/fmtlog/blob/main/README.md This snippet provides the console commands to clone, initialize submodules, update, and build the fmtlog library using CMake. It's used for generating static or shared library versions of fmtlog, which are then copied to the project. ```console $ git clone https://github.com/MengRao/fmtlog.git $ cd fmtlog $ git submodule init $ git submodule update $ ./build.sh ``` -------------------------------- ### Basic Formatting with fmtlog and fmtlib in C++ Source: https://github.com/mengrao/fmtlog/blob/main/README.md This snippet demonstrates how fmtlog leverages fmtlib's advanced formatting capabilities. It showcases positional and named arguments, formatting for standard containers (vectors, tuples), user-defined types, and various format specifiers for numbers and strings. It also highlights the compile-time error checking provided by FMT_STRING. ```C++ #include "fmt/ranges.h" using namespace fmt::literals; logi("I'd rather be {1} than {0}.", "right", "happy"); logi("Hello, {name}! The answer is {number}. Goodbye, {name}.", "name"_a = "World", "number"_a = 42); std::vector v = {1, 2, 3}; logi("ranges: {}", v); logi("std::move can be used for objects with non-trivial destructors: {}", std::move(v)); assert(v.size() == 0); std::tuple t = {1, 'a'}; logi("tuples: {}", fmt::join(t, ", ")); enum class color {red, green, blue}; template <> struct fmt::formatter: formatter { // parse is inherited from formatter. template auto format(color c, FormatContext& ctx) { string_view name = "unknown"; switch (c) { case color::red: name = "red"; break; case color::green: name = "green"; break; case color::blue: name = "blue"; break; } return formatter::format(name, ctx); } }; logi("user defined type: {:>10}", color::blue); logi("{:*^30}", "centered"); logi("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}", 42); logi("dynamic precision: {:.{}f}", 3.14, 1); // This gives a compile-time error because d is an invalid format specifier for a string. // FMT_STRING() is not needed from C++20 onward logi(FMT_STRING("{:d}"), "I am not a number"); ``` -------------------------------- ### Including fmt and Test Subdirectories in CMake Build Source: https://github.com/mengrao/fmtlog/blob/main/CMakeLists.txt This snippet incorporates the `fmt` and `test` subdirectories into the main build process. This allows CMake to process their respective `CMakeLists.txt` files, typically for building dependencies or running tests. ```CMake add_subdirectory(fmt) add_subdirectory(test) ``` -------------------------------- ### Defining a Conditional Static Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, defines a basic static library named `static_lib` from the source file `lib.cc`. This library can be linked by other targets within the project. ```CMake add_library(static_lib lib.cc) ``` -------------------------------- ### Defining fmtlog C++ Project Source: https://github.com/mengrao/fmtlog/blob/main/CMakeLists.txt This snippet defines the project name as 'fmtlog' and declares that it is a C++ project. This sets up the necessary language support for subsequent build commands. ```CMake project(fmtlog CXX) ``` -------------------------------- ### Setting Minimum CMake Version for fmtlog Project Source: https://github.com/mengrao/fmtlog/blob/main/CMakeLists.txt This snippet specifies the minimum required CMake version for building the project. CMake version 3.15 or newer is needed to ensure compatibility with the project's build configurations and features. ```CMake cmake_minimum_required(VERSION 3.15) ``` -------------------------------- ### Defining fmtlog Log Callback Function Signature (C++) Source: https://github.com/mengrao/fmtlog/blob/main/README.md This snippet defines the signature for a callback function that users can register with `fmtlog::setLogCB`. It details the parameters passed to the callback, including timestamp, log level, location, thread name, and the log message itself, allowing for custom handling of log messages. ```C++ // callback signature user can register // ns: nanosecond timestamp // level: logLevel // location: full file path with line num, e.g: /home/raomeng/fmtlog/fmtlog.h:45 // basePos: file base index in the location // threadName: thread id or the name user set with setThreadName // msg: full log msg with header // bodyPos: log body index in the msg // logFilePos: log file position of this msg typedef void (*LogCBFn)(int64_t ns, LogLevel level, fmt::string_view location, size_t basePos, fmt::string_view threadName, fmt::string_view msg, size_t bodyPos, size_t logFilePos); ``` -------------------------------- ### Defining a Conditional Static Header-Only Library (CMake) Source: https://github.com/mengrao/fmtlog/blob/main/test/CMakeLists.txt This snippet, conditional on `NOT MSVC`, defines a static library `static_header_lib` and configures it as header-only by adding the `FMTLOG_HEADER_ONLY` compile definition. This is useful for libraries that primarily expose functionality through headers. ```CMake add_library(static_header_lib lib.cc) target_compile_definitions(static_header_lib PUBLIC FMTLOG_HEADER_ONLY) ``` -------------------------------- ### Configuring Conditional Compile Options and Libraries for fmtlog Source: https://github.com/mengrao/fmtlog/blob/main/CMakeLists.txt This snippet sets compiler options based on the detected compiler. For MSVC, it uses C++latest standard. For other compilers (like GCC/Clang), it applies common warnings, optimizations, C++17 standard, and links against `pthread` for threading support. ```CMake if(MSVC) add_compile_options(/std:c++latest) else() add_compile_options(-Wall -O3 -std:c++17) #add_compile_options(-Wall -Ofast -std:c++2a -DNDEBUG -march=skylake -flto -fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables -DFMTLOG_NO_CHECK_LEVEL=1) #SET(CMAKE_AR "gcc-ar") #SET(CMAKE_RANLIB "gcc-ranlib") link_libraries(pthread) endif() ``` -------------------------------- ### Using Smart Pointers with fmtlog in C++ Source: https://github.com/mengrao/fmtlog/blob/main/README.md This snippet demonstrates fmtlog's support for `std::shared_ptr` and `std::unique_ptr`, which simplifies object lifetime management compared to raw pointers. It shows how values pointed to by smart pointers are captured at the time of logging and resolved when `fmtlog::poll()` is called, similar to raw pointers. ```C++ int a = 4; auto sptr = std::make_shared(5); auto uptr = std::make_unique(6); logi("void ptr: {}, ptr: {}, sptr: {}, uptr: {}", (void*)&a, &a, sptr, std::move(uptr)); a = 7; *sptr = 8; fmtlog::poll(); // output: void ptr: 0x7ffd08ac53ac, ptr: 7, sptr: 8, uptr: 6 ``` -------------------------------- ### Managing Asynchronous Logging with Polling and Thread Names in C++ Source: https://github.com/mengrao/fmtlog/blob/main/README.md This C++ snippet shows how to manage asynchronous logging by explicitly calling `fmtlog::poll()` to process queued log messages. It also demonstrates setting thread-specific names using `fmtlog::setThreadName` to include thread context in log outputs, highlighting that messages are processed when `poll()` is invoked. ```c++ fmtlog::setThreadName("aaa"); logi("Thread name is bbb in this msg"); fmtlog::setThreadName("bbb"); fmtlog::poll(); fmtlog::setThreadName("ccc"); logi("Thread name is ccc in this msg"); fmtlog::poll(); fmtlog::setThreadName("ddd"); ``` -------------------------------- ### Limiting Log Frequency with fmtlog::logil (C++) Source: https://github.com/mengrao/fmtlog/blob/main/README.md This C++ snippet demonstrates how to use the `logil` macro from `fmtlog` to limit the frequency of a log message. The first argument `1e9` specifies the minimum interval in nanoseconds (1 second in this case) between consecutive displays of this log. This prevents log spamming for high-frequency events, ensuring the message is displayed at most once per specified interval. ```C++ logil(1e9, "this log will be displayed at most once per second"). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.