Skip to content

Commit

Permalink
Merge branch 'main' into concurrency-options
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushieno committed Dec 22, 2024
2 parents 46e6306 + 64e5fd1 commit 1d2d9bb
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 187 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRTLOG_FULL_WARNINGS=ON -DRTLOG_BUILD_TESTS=ON -DRTLOG_BUILD_EXAMPLES=ON

- name: Build
# Build your program with the given configuration
Expand All @@ -47,7 +47,7 @@ jobs:
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRTLOG_USE_FMTLIB=ON -DRTLOG_FULL_WARNINGS=ON
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

- name: Build
# Build your program with the given configuration
Expand Down
18 changes: 12 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.6)
project(rtlog VERSION 1.0.0)

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

# Add library header files
set(HEADERS
Expand All @@ -14,6 +15,12 @@ set(HEADERS
add_library(rtlog INTERFACE)
add_library(rtlog::rtlog ALIAS rtlog)

if (CMAKE_CXX_STANDARD LESS 17)
message(WARNING "C++17 or higher is required for rtlog. Setting C++17 for the target..")
endif()

target_compile_features(rtlog INTERFACE cxx_std_17)

# Set include directories for library
target_include_directories(rtlog INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand Down Expand Up @@ -85,6 +92,7 @@ if (RTLOG_USE_FMTLIB AND NOT TARGET fmt::fmt)

FetchContent_Declare(fmtlib
GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG 11.0.2
)
FetchContent_MakeAvailable(fmtlib)
endif()
Expand All @@ -110,15 +118,13 @@ target_compile_options(rtlog
$<$<BOOL:${RTLOG_FULL_WARNINGS}>:-Wall -Werror -Wformat -Wextra -Wformat-security -Wno-unused-function>
)

option(RTLOG_BUILD_TESTS "Build tests" ON)
if(RTLOG_BUILD_TESTS)
include(CTest)
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
enable_testing()
add_subdirectory(test)
endif()

option(RTLOG_BUILD_EXAMPLES "Build examples" ON)
if(RTLOG_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
Expand Down
46 changes: 34 additions & 12 deletions include/rtlog/rtlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@

#include <stb_sprintf.h>

#if defined(__has_feature)
#if __has_feature(realtime_sanitizer)
#define RTLOG_NONBLOCKING [[clang::nonblocking]]
#endif
#endif

#ifndef RTLOG_NONBLOCKING
#define RTLOG_NONBLOCKING
#endif

#ifndef __MSC_VER__
#define RTLOG_ATTRIBUTE_FORMAT __attribute__((format(printf, 3, 4)))
#else
#define RTLOG_ATTRIBUTE_FORMAT
#endif

namespace rtlog {

enum class Status {
Expand Down Expand Up @@ -94,7 +110,8 @@ class Logger {
* message was truncated, the function returns
* `Status::Error_MessageTruncated`. Otherwise, it returns `Status::Success`.
*/
Status Logv(LogData &&inputData, const char *format, va_list args) {
Status Logv(LogData &&inputData, const char *format,
va_list args) RTLOG_NONBLOCKING {
auto retVal = Status::Success;

InternalLogData dataToQueue;
Expand Down Expand Up @@ -144,11 +161,8 @@ class Logger {
* message was truncated, the function returns
* `Status::Error_MessageTruncated`. Otherwise, it returns `Status::Success`.
*/
Status Log(LogData &&inputData, const char *format, ...)
#ifndef __MSC_VER__
__attribute__((format(printf, 3, 4)))
#endif
{
Status Log(LogData &&inputData, const char *format,
...) RTLOG_NONBLOCKING RTLOG_ATTRIBUTE_FORMAT {
va_list args;
va_start(args, format);
auto retVal = Logv(std::move(inputData), format, args);
Expand Down Expand Up @@ -187,7 +201,7 @@ class Logger {
*/
template <typename... T>
Status LogFmt(LogData &&inputData, fmt::format_string<T...> fmtString,
T &&...args) {
T &&...args) RTLOG_NONBLOCKING {
auto retVal = Status::Success;

InternalLogData dataToQueue;
Expand Down Expand Up @@ -277,15 +291,19 @@ class Logger {
class InternalQueueMPSC : public InternalQueue {
farbot::fifo<InternalLogData, farbot::fifo_options::concurrency::single,
farbot::fifo_options::concurrency::multiple,
farbot::fifo_options::full_empty_failure_mode::return_false_on_full_or_empty,
farbot::fifo_options::full_empty_failure_mode::overwrite_or_return_default>
farbot::fifo_options::full_empty_failure_mode::
return_false_on_full_or_empty,
farbot::fifo_options::full_empty_failure_mode::
overwrite_or_return_default>
mQueue{MaxNumMessages};

public:
InternalQueueMPSC() {
static_assert((MaxNumMessages & (MaxNumMessages - 1)) == 0 ||
Concurrency != QueueConcurrency::Multi_Producer_Single_Consumer,
"you have to assign 2^n to MaxNumMessages (farbot backend restriction)");
static_assert((MaxNumMessages & (MaxNumMessages - 1)) == 0 ||
Concurrency !=
QueueConcurrency::Multi_Producer_Single_Consumer,
"you have to assign 2^n to MaxNumMessages (farbot backend "
"restriction)");
}
bool tryEnqueue(InternalLogData &&value) override {
return mQueue.push(std::move(value));
Expand Down Expand Up @@ -378,4 +396,8 @@ template <typename LoggerType, typename PrintLogFn> class LogProcessingThread {
std::chrono::milliseconds mWaitTime{};
};

template <typename LoggerType, typename PrintLogFn>
LogProcessingThread(LoggerType &, PrintLogFn)
-> LogProcessingThread<LoggerType, PrintLogFn>;

} // namespace rtlog
33 changes: 10 additions & 23 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
if (NOT TARGET doctest::doctest)
if (NOT TARGET gtest_main)
include(FetchContent)
FetchContent_Declare(doctest
GIT_REPOSITORY https://github.com/doctest/doctest
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main
)
FetchContent_MakeAvailable(doctest)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
endif()

# These two are painfully not easily accessed.
# TODO: CAPPLE submitted a PR that got accepted into doctest, switch to that newer version when we can
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
set(_DOCTEST_DISCOVER_TESTS_SCRIPT "${_DOCTEST_DISCOVER_TESTS_SCRIPT}" CACHE PATH "Path to doctest cmake helper file" FORCE)

target_compile_definitions(doctest
INTERFACE
DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
)

add_executable(rtlog_tests test_rtlog.cpp)

target_link_libraries(rtlog_tests
PRIVATE
doctest::doctest
gtest_main
rtlog::rtlog
)

set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED ON)

if (CMAKE_GENERATOR STREQUAL "Xcode")
add_test(NAME rtlog_tests COMMAND rtlog_tests)
else()
doctest_discover_tests(rtlog_tests)
endif()


include(GoogleTest)
gtest_discover_tests(rtlog_tests)
Loading

0 comments on commit 1d2d9bb

Please sign in to comment.