Skip to content

Commit 4ab0c94

Browse files
committed
Merge branch 'main' into concurrency-options
2 parents 46e6306 + 64e5fd1 commit 4ab0c94

5 files changed

Lines changed: 164 additions & 188 deletions

File tree

.github/workflows/cmake.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Configure CMake
2424
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
2525
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
26-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
26+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRTLOG_FULL_WARNINGS=ON -DRTLOG_BUILD_TESTS=ON -DRTLOG_BUILD_EXAMPLES=ON
2727

2828
- name: Build
2929
# Build your program with the given configuration
@@ -47,7 +47,7 @@ jobs:
4747
- name: Configure CMake
4848
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
4949
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
50-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRTLOG_USE_FMTLIB=ON -DRTLOG_FULL_WARNINGS=ON
50+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRTLOG_USE_FMTLIB=ON -DRTLOG_FULL_WARNINGS=ON -DRTLOG_BUILD_TESTS=ON -DRTLOG_BUILD_EXAMPLES=ON
5151

5252
- name: Build
5353
# Build your program with the given configuration

CMakeLists.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
cmake_minimum_required(VERSION 3.0)
1+
cmake_minimum_required(VERSION 3.6)
22
project(rtlog VERSION 1.0.0)
33

4-
# Set C++ standard
5-
set(CMAKE_CXX_STANDARD 17) # TODO: we only use 17 for CTAD type things, can we get away with 11 ?
6-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
4+
option(RTLOG_USE_FMTLIB "Use fmtlib for formatting" OFF)
5+
option(RTLOG_FULL_WARNINGS "Enable full warnings" OFF)
6+
option(RTLOG_BUILD_TESTS "Build tests" OFF)
7+
option(RTLOG_BUILD_EXAMPLES "Build examples" OFF)
78

89
# Add library header files
910
set(HEADERS
@@ -14,6 +15,12 @@ set(HEADERS
1415
add_library(rtlog INTERFACE)
1516
add_library(rtlog::rtlog ALIAS rtlog)
1617

18+
if (CMAKE_CXX_STANDARD LESS 17)
19+
message(WARNING "C++17 or higher is required for rtlog. Setting C++17 for the target..")
20+
endif()
21+
22+
target_compile_features(rtlog INTERFACE cxx_std_17)
23+
1724
# Set include directories for library
1825
target_include_directories(rtlog INTERFACE
1926
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
@@ -85,6 +92,7 @@ if (RTLOG_USE_FMTLIB AND NOT TARGET fmt::fmt)
8592

8693
FetchContent_Declare(fmtlib
8794
GIT_REPOSITORY https://github.com/fmtlib/fmt
95+
GIT_TAG 11.0.2
8896
)
8997
FetchContent_MakeAvailable(fmtlib)
9098
endif()
@@ -110,15 +118,13 @@ target_compile_options(rtlog
110118
$<$<BOOL:${RTLOG_FULL_WARNINGS}>:-Wall -Werror -Wformat -Wextra -Wformat-security -Wno-unused-function>
111119
)
112120

113-
option(RTLOG_BUILD_TESTS "Build tests" ON)
114121
if(RTLOG_BUILD_TESTS)
115122
include(CTest)
116123
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
117124
enable_testing()
118125
add_subdirectory(test)
119126
endif()
120127

121-
option(RTLOG_BUILD_EXAMPLES "Build examples" ON)
122128
if(RTLOG_BUILD_EXAMPLES)
123129
add_subdirectory(examples)
124130
endif()

include/rtlog/rtlog.h

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323

2424
#include <stb_sprintf.h>
2525

26+
#if defined(__has_feature)
27+
#if __has_feature(realtime_sanitizer)
28+
#define RTLOG_NONBLOCKING [[clang::nonblocking]]
29+
#endif
30+
#endif
31+
32+
#ifndef RTLOG_NONBLOCKING
33+
#define RTLOG_NONBLOCKING
34+
#endif
35+
36+
#ifndef __MSC_VER__
37+
#define RTLOG_ATTRIBUTE_FORMAT __attribute__((format(printf, 3, 4)))
38+
#else
39+
#define RTLOG_ATTRIBUTE_FORMAT
40+
#endif
41+
2642
namespace rtlog {
2743

2844
enum class Status {
@@ -94,7 +110,8 @@ class Logger {
94110
* message was truncated, the function returns
95111
* `Status::Error_MessageTruncated`. Otherwise, it returns `Status::Success`.
96112
*/
97-
Status Logv(LogData &&inputData, const char *format, va_list args) {
113+
Status Logv(LogData &&inputData, const char *format,
114+
va_list args) RTLOG_NONBLOCKING {
98115
auto retVal = Status::Success;
99116

100117
InternalLogData dataToQueue;
@@ -144,11 +161,8 @@ class Logger {
144161
* message was truncated, the function returns
145162
* `Status::Error_MessageTruncated`. Otherwise, it returns `Status::Success`.
146163
*/
147-
Status Log(LogData &&inputData, const char *format, ...)
148-
#ifndef __MSC_VER__
149-
__attribute__((format(printf, 3, 4)))
150-
#endif
151-
{
164+
Status Log(LogData &&inputData, const char *format,
165+
...) RTLOG_NONBLOCKING RTLOG_ATTRIBUTE_FORMAT {
152166
va_list args;
153167
va_start(args, format);
154168
auto retVal = Logv(std::move(inputData), format, args);
@@ -187,7 +201,7 @@ class Logger {
187201
*/
188202
template <typename... T>
189203
Status LogFmt(LogData &&inputData, fmt::format_string<T...> fmtString,
190-
T &&...args) {
204+
T &&...args) RTLOG_NONBLOCKING {
191205
auto retVal = Status::Success;
192206

193207
InternalLogData dataToQueue;
@@ -277,15 +291,19 @@ class Logger {
277291
class InternalQueueMPSC : public InternalQueue {
278292
farbot::fifo<InternalLogData, farbot::fifo_options::concurrency::single,
279293
farbot::fifo_options::concurrency::multiple,
280-
farbot::fifo_options::full_empty_failure_mode::return_false_on_full_or_empty,
281-
farbot::fifo_options::full_empty_failure_mode::overwrite_or_return_default>
294+
farbot::fifo_options::full_empty_failure_mode::
295+
return_false_on_full_or_empty,
296+
farbot::fifo_options::full_empty_failure_mode::
297+
overwrite_or_return_default>
282298
mQueue{MaxNumMessages};
283299

284300
public:
285301
InternalQueueMPSC() {
286-
static_assert((MaxNumMessages & (MaxNumMessages - 1)) == 0 ||
287-
Concurrency != QueueConcurrency::Multi_Producer_Single_Consumer,
288-
"you have to assign 2^n to MaxNumMessages (farbot backend restriction)");
302+
static_assert((MaxNumMessages & (MaxNumMessages - 1)) == 0 ||
303+
Concurrency !=
304+
QueueConcurrency::Multi_Producer_Single_Consumer,
305+
"you have to assign 2^n to MaxNumMessages (farbot backend "
306+
"restriction)");
289307
}
290308
bool tryEnqueue(InternalLogData &&value) override {
291309
return mQueue.push(std::move(value));
@@ -378,4 +396,8 @@ template <typename LoggerType, typename PrintLogFn> class LogProcessingThread {
378396
std::chrono::milliseconds mWaitTime{};
379397
};
380398

399+
template <typename LoggerType, typename PrintLogFn>
400+
LogProcessingThread(LoggerType &, PrintLogFn)
401+
-> LogProcessingThread<LoggerType, PrintLogFn>;
402+
381403
} // namespace rtlog

test/CMakeLists.txt

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
1-
if (NOT TARGET doctest::doctest)
1+
if (NOT TARGET gtest_main)
22
include(FetchContent)
3-
FetchContent_Declare(doctest
4-
GIT_REPOSITORY https://github.com/doctest/doctest
3+
FetchContent_Declare(
4+
googletest
5+
GIT_REPOSITORY https://github.com/google/googletest.git
6+
GIT_TAG main
57
)
6-
FetchContent_MakeAvailable(doctest)
8+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
9+
FetchContent_MakeAvailable(googletest)
710
endif()
811

9-
# These two are painfully not easily accessed.
10-
# TODO: CAPPLE submitted a PR that got accepted into doctest, switch to that newer version when we can
11-
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
12-
set(_DOCTEST_DISCOVER_TESTS_SCRIPT "${_DOCTEST_DISCOVER_TESTS_SCRIPT}" CACHE PATH "Path to doctest cmake helper file" FORCE)
13-
14-
target_compile_definitions(doctest
15-
INTERFACE
16-
DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
17-
DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
18-
)
19-
2012
add_executable(rtlog_tests test_rtlog.cpp)
2113

2214
target_link_libraries(rtlog_tests
2315
PRIVATE
24-
doctest::doctest
16+
gtest_main
2517
rtlog::rtlog
2618
)
2719

2820
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED ON)
2921

30-
if (CMAKE_GENERATOR STREQUAL "Xcode")
31-
add_test(NAME rtlog_tests COMMAND rtlog_tests)
32-
else()
33-
doctest_discover_tests(rtlog_tests)
34-
endif()
35-
36-
22+
include(GoogleTest)
23+
gtest_discover_tests(rtlog_tests)

0 commit comments

Comments
 (0)