Skip to content

Commit

Permalink
rename configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Macheta committed Jan 3, 2024
1 parent 2208097 commit b2a5c64
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 91 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/cpp_unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
- uses: actions/checkout@v4

- name: Configure
run: cmake -B build -G Ninja
run: cmake --preset devel

- name: Build
run: cmake --build build -t all
run: cmake --build --preset devel

- name: Test
run: ctest --test-dir build
run: ctest --preset default
51 changes: 35 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,60 @@ project(

include(CMakeDependentOption)

option(LOGGER_FP_SUPPORT "Enables formatted printing of floating point types" ON)
option(CONFIG_LOGGER_FP_SUPPORT "Enables formatted printing of floating point types" ON)

cmake_dependent_option(LOGGER_EXPONENT_SUPPORT "Enables printing floating point types using scientific notation" OFF LOGGER_FP_SUPPORT OFF)

option(LOGGER_PTRDIFF_SUPPORT "Enables formatted printing of ptrdiff_t" OFF)
cmake_dependent_option(
CONFIG_LOGGER_EXPONENT_SUPPORT "Enables printing floating point types using scientific notation" OFF
CONFIG_LOGGER_FP_SUPPORT OFF
)

option(LOGGER_TIMESTAMPS "Enables support for timestamping logger messages" ON)
option(CONFIG_LOGGER_PTRDIFF_SUPPORT "Enables formatted printing of ptrdiff_t" OFF)

cmake_dependent_option(LOGGER_HUMAN_READABLE_TIMESTAMP "Prints timestamps in human readable format (hh:mm::ss.ms)" OFF LOGGER_TIMESTAMPS OFF)
option(CONFIG_LOGGER_TIMESTAMPS "Enables support for timestamping logger messages" ON)

option(LOGGER_CUSTOM_AFFIXES "Enables function set to add custom message affixes" OFF)
cmake_dependent_option(
CONFIG_LOGGER_HUMAN_READABLE_TIMESTAMP "Prints timestamps in human readable format (hh:mm::ss.ms)" OFF
CONFIG_LOGGER_TIMESTAMPS OFF
)

option(LOGGER_RUNTIME_VERBOSITY "Enables changing logger verbosity at runtime via LOGGER_SetRuntimeLevel function" OFF)
option(CONFIG_LOGGER_CUSTOM_AFFIXES "Enables function set to add custom message affixes" OFF)

option(LOGGER_HEADER_WITH_LOCATION "Appends logger header with source file name. By default, uses __FILE__ but the displayed name may be truncated using logger_normalize_printable_filenames CMake function" OFF)
option(CONFIG_LOGGER_RUNTIME_VERBOSITY
"Enables changing logger verbosity at runtime via LOGGER_SetRuntimeLevel function" OFF
)

option(LOGGER_THREAD_SAFETY_HOOKS "Enables LOGGER_SetLockingMechanism funtion that allows setting custom lock mechanism" OFF)
option(
CONFIG_LOGGER_HEADER_WITH_LOCATION
"Appends logger header with source file name. By default, uses __FILE__ but the displayed name may be truncated using logger_normalize_printable_filenames CMake function"
OFF
)

option(LOGGER_FLUSH_HOOKS "Enables LOGGER_SetFlushHook funtion that will be called after each logger message" OFF)
option(CONFIG_LOGGER_THREAD_SAFETY_HOOKS
"Enables LOGGER_SetLockingMechanism funtion that allows setting custom lock mechanism" OFF
)

option(LOGGER_VERBOSE_ERRORS "Uses custom compiler pragmas to display additional debug information on compilation errors" OFF)
option(CONFIG_LOGGER_FLUSH_HOOKS "Enables LOGGER_SetFlushHook funtion that will be called after each logger message"
OFF
)

option(LOGGER_BUILD_TESTS "Forces building test cases" OFF)
option(CONFIG_LOGGER_VERBOSE_ERRORS
"Uses custom compiler pragmas to display additional debug information on compilation errors" OFF
)

option(CONFIG_LOGGER_BUILD_TESTS "Forces building test cases" PROJECT_IS_TOP_LEVEL)

set(EXTENDED_MODULE_PATH ${CMAKE_MODULE_PATH};${CMAKE_CURRENT_LIST_DIR}/cmake)
list(REMOVE_DUPLICATES EXTENDED_MODULE_PATH)
set(CMAKE_MODULE_PATH ${EXTENDED_MODULE_PATH} CACHE INTERNAL "" FORCE)

set(CMAKE_MODULE_PATH
${EXTENDED_MODULE_PATH}
CACHE INTERNAL "" FORCE
)

include(requirements.cmake)

add_subdirectory(src)

if (PROJECT_IS_TOP_LEVEL OR LOGGER_BUILD_TESTS)
if (CONFIG_LOGGER_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif ()
56 changes: 56 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"version": 6,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20,
"patch": 0
},
"include": [],
"configurePresets": [
{
"name": "default",
"displayName": "Default Config",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CONFIG_RING_BUFFER_16_ENABLE": "ON",
"CONFIG_RING_BUFFER_BUILD_TESTS": "OFF"
}
},
{
"name": "devel",
"displayName": "Unit testing",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CONFIG_RING_BUFFER_16_ENABLE": "ON",
"CONFIG_RING_BUFFER_BUILD_TESTS": "ON"
}
}
],
"buildPresets": [
{
"name": "default",
"configurePreset": "default"
},
{
"name": "devel",
"configurePreset": "devel"
}
],
"testPresets": [
{
"name": "default",
"configurePreset": "devel",
"output": {
"outputOnFailure": true
},
"execution": {
"noTestsAction": "error",
"stopOnFailure": true
}
}
]
}
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Logger is compatibile with almost every possible CMake include option:

example configuration using find_package:
``` cmake
set(LOGGER_CUSTOM_AFFIXES ON) # Available options are described later
set(CONFIG_LOGGER_CUSTOM_AFFIXES ON) # Available options are described later
find_package(logger REQUIRED)
target_link_libraries(example PUBLIC embetech::logger)
```
Expand Down Expand Up @@ -94,7 +94,7 @@ Global configuration shall be stored in logger_config.h file The example below p

## Features configuration
In order to stay flexible and minimalistic logger uses compile-time configuration options that will affect global behaviour. The list below reflects options available in CMakeLists.txt file:
### LOGGER_TIMESTAMPS
### CONFIG_LOGGER_TIMESTAMPS
If turned on, the Logger header format will contain timestamp, acquired from the user defined callback via LOGGER_SetTimeSource function:
```C
LOGGER_SetTimeSource([](){return std::uint32_t(8000085);}); // Short C++ lambda to save the world
Expand All @@ -105,13 +105,13 @@ will be printed as (assuming DEFAULT channel):
8000085 DEFAULT (I): test message
```
### LOGGER_HUMAN_READABLE_TIMESTAMP
This option is available only when LOGGER_TIMESTAMPS is ON.
### CONFIG_LOGGER_HUMAN_READABLE_TIMESTAMP
This option is available only when CONFIG_LOGGER_TIMESTAMPS is ON.
Changes the format of timestamp to hh:mm::ss.ms:
```
02:13:20.085 DEFAULT (I): test message
```
### LOGGER_CUSTOM_AFFIXES
### CONFIG_LOGGER_CUSTOM_AFFIXES
Allows setting custom prefix/suffix for every Logger message, using LOGGER_SetPrefix/LOGGER_SetSuffix function.
Both will be printed as binary data, so no '\0' termination is required.
This feature might be useful when working with custom terminal protocols.
Expand All @@ -125,7 +125,7 @@ will produce
```
pre- DEFAULT (N): test message -post
```
### LOGGER_RUNTIME_VERBOSITY
### CONFIG_LOGGER_RUNTIME_VERBOSITY
Enables reducing verbosity of printed messages AT RUNTIME using LOGGER_SetRuntimeLevel function. Of course, compile time level is still stronger so the resulting set of messages will be no greater than both:
```C
LOGGER_NOTICE("test message1");
Expand All @@ -142,7 +142,7 @@ DEFAULT (N): test message3
```
the second message will not be printed, however its code is still available to be enabled (assuming that DEFAULT_LOG_CHANNEL_LEVEL is at least at NOTICE)
### LOGGER_HEADER_WITH_LOCATION
### CONFIG_LOGGER_HEADER_WITH_LOCATION
Expands the header with code location [file:line]:
```
DEFAULT (N) [x:\long_path\file.cpp:66]: test message
Expand All @@ -156,7 +156,7 @@ logger_normalize_printable_filenames()
this would change the above message to
```
DEFAULT (N) [file.cpp:66]: test message
### LOGGER_THREAD_SAFETY_HOOKS
### CONFIG_LOGGER_THREAD_SAFETY_HOOKS
By design, logger is as thread-safe as your output callback (so probably no).
When this option is enabled. Logger will use user-provided lock/unlock functions to ensure thread safety.
To do so, you should register lock/unlock callbacks:
Expand All @@ -182,7 +182,7 @@ The API of callbacks is fairly universal... You might be able to simply plug you

LOGGER will always try to lock before printing message. In scoped printing case, The lock will be acquired by LOGGER_START, and released by LOGGER_END/LOGGER_ENDL.

### LOGGER_FLUSH_HOOKS
### CONFIG_LOGGER_FLUSH_HOOKS
Enables user to bind function, to flush output. The function may either be called explicitly, using LOGGER_Flush(), or automatically on the end of each message (So every LOGGER_INFO/etc. or after each LOGGER_END/LOGGER_ENDL):
```C
LOGGER_DisableHeader();
Expand All @@ -205,7 +205,7 @@ marco!
polo!!
```
### LOGGER_VERBOSE_ERRORS
### CONFIG_LOGGER_VERBOSE_ERRORS
When user messed up, it may be difficult to dig through all preprocessor magic. When this option is enabled, and your compiler is eiter clang or GCC-like, each compile-time error will be appended with message, which logger channel was the culprit
Expand Down
47 changes: 23 additions & 24 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
message(
DEBUG
"Selected logger options:
- LOGGER_FP_SUPPORT: ${LOGGER_FP_SUPPORT}
- LOGGER_PTRDIFF_SUPPORT: ${LOGGER_PTRDIFF_SUPPORT}
- LOGGER_EXPONENT_SUPPORT ${LOGGER_EXPONENT_SUPPORT}
- LOGGER_TIMESTAMPS: ${LOGGER_TIMESTAMPS}
- LOGGER_HUMAN_READABLE_TIMESTAMP: ${LOGGER_HUMAN_READABLE_TIMESTAMP}
- LOGGER_CUSTOM_AFFIXES: ${LOGGER_CUSTOM_AFFIXES}
- LOGGER_RUNTIME_VERBOSITY: ${LOGGER_RUNTIME_VERBOSITY}
- LOGGER_HEADER_WITH_LOCATION: ${LOGGER_HEADER_WITH_LOCATION}
- LOGGER_THREAD_SAFETY_HOOKS: ${LOGGER_THREAD_SAFETY_HOOKS}
- LOGGER_FLUSH_HOOKS: ${LOGGER_FLUSH_HOOKS}
- LOGGER_VERBOSE_ERRORS: ${LOGGER_VERBOSE_ERRORS}
- LOGGER_BUILD_TESTS: ${LOGGER_BUILD_TESTS}"
- CONFIG_LOGGER_FP_SUPPORT: ${CONFIG_LOGGER_FP_SUPPORT}
- CONFIG_LOGGER_PTRDIFF_SUPPORT: ${CONFIG_LOGGER_PTRDIFF_SUPPORT}
- CONFIG_LOGGER_EXPONENT_SUPPORT ${CONFIG_LOGGER_EXPONENT_SUPPORT}
- CONFIG_LOGGER_TIMESTAMPS: ${CONFIG_LOGGER_TIMESTAMPS}
- CONFIG_LOGGER_HUMAN_READABLE_TIMESTAMP: ${CONFIG_LOGGER_HUMAN_READABLE_TIMESTAMP}
- CONFIG_LOGGER_CUSTOM_AFFIXES: ${CONFIG_LOGGER_CUSTOM_AFFIXES}
- CONFIG_LOGGER_RUNTIME_VERBOSITY: ${CONFIG_LOGGER_RUNTIME_VERBOSITY}
- CONFIG_LOGGER_HEADER_WITH_LOCATION: ${CONFIG_LOGGER_HEADER_WITH_LOCATION}
- CONFIG_LOGGER_THREAD_SAFETY_HOOKS: ${CONFIG_LOGGER_THREAD_SAFETY_HOOKS}
- CONFIG_LOGGER_FLUSH_HOOKS: ${CONFIG_LOGGER_FLUSH_HOOKS}
- CONFIG_LOGGER_VERBOSE_ERRORS: ${CONFIG_LOGGER_VERBOSE_ERRORS}
- CONFIG_LOGGER_BUILD_TESTS: ${CONFIG_LOGGER_BUILD_TESTS}"
)

add_library(logger STATIC logger.c third_party/printf.c)
Expand All @@ -25,22 +25,21 @@ target_include_directories(

target_compile_definitions(
logger
PRIVATE PRINTF_SUPPORT_FLOAT=$<BOOL:${LOGGER_FP_SUPPORT}>
PRINTF_SUPPORT_EXPONENTIAL=$<BOOL:${LOGGER_EXPONENT_SUPPORT}>
PRINTF_SUPPORT_PTRDIFF_T=$<BOOL:${LOGGER_PTRDIFF_SUPPORT}>
LOGGER_TIMESTAMPS=$<BOOL:${LOGGER_TIMESTAMPS}>
LOGGER_HUMAN_READABLE_TIMESTAMP=$<BOOL:${LOGGER_HUMAN_READABLE_TIMESTAMP}>
PUBLIC LOGGER_CUSTOM_AFFIXES=$<BOOL:${LOGGER_CUSTOM_AFFIXES}>
LOGGER_RUNTIME_VERBOSITY=$<BOOL:${LOGGER_RUNTIME_VERBOSITY}>
LOGGER_HEADER_WITH_LOCATION=$<BOOL:${LOGGER_HEADER_WITH_LOCATION}>
LOGGER_THREAD_SAFETY_HOOKS=$<BOOL:${LOGGER_THREAD_SAFETY_HOOKS}>
LOGGER_FLUSH_HOOKS=$<BOOL:${LOGGER_FLUSH_HOOKS}>
LOGGER_VERBOSE_ERRORS=$<BOOL:${LOGGER_VERBOSE_ERRORS}>
PRIVATE PRINTF_SUPPORT_FLOAT=$<BOOL:${CONFIG_LOGGER_FP_SUPPORT}>
PRINTF_SUPPORT_EXPONENTIAL=$<BOOL:${CONFIG_LOGGER_EXPONENT_SUPPORT}>
PRINTF_SUPPORT_PTRDIFF_T=$<BOOL:${CONFIG_LOGGER_PTRDIFF_SUPPORT}>
CONFIG_LOGGER_TIMESTAMPS=$<BOOL:${CONFIG_LOGGER_TIMESTAMPS}>
CONFIG_LOGGER_HUMAN_READABLE_TIMESTAMP=$<BOOL:${CONFIG_LOGGER_HUMAN_READABLE_TIMESTAMP}>
PUBLIC CONFIG_LOGGER_CUSTOM_AFFIXES=$<BOOL:${CONFIG_LOGGER_CUSTOM_AFFIXES}>
CONFIG_LOGGER_RUNTIME_VERBOSITY=$<BOOL:${CONFIG_LOGGER_RUNTIME_VERBOSITY}>
CONFIG_LOGGER_HEADER_WITH_LOCATION=$<BOOL:${CONFIG_LOGGER_HEADER_WITH_LOCATION}>
CONFIG_LOGGER_THREAD_SAFETY_HOOKS=$<BOOL:${CONFIG_LOGGER_THREAD_SAFETY_HOOKS}>
CONFIG_LOGGER_FLUSH_HOOKS=$<BOOL:${CONFIG_LOGGER_FLUSH_HOOKS}>
CONFIG_LOGGER_VERBOSE_ERRORS=$<BOOL:${CONFIG_LOGGER_VERBOSE_ERRORS}>
)

target_compile_features(logger PUBLIC c_std_99)

add_library(embetech::logger ALIAS logger)


include(${PROJECT_SOURCE_DIR}/cmake/component_install.cmake)
Loading

0 comments on commit b2a5c64

Please sign in to comment.