diff --git a/CMakeLists.txt b/CMakeLists.txt index 27b456d..27ee34e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(config) add_subdirectory(example) +add_subdirectory(test) target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/example/test/FsfwTestTask.cpp b/example/test/FsfwTestTask.cpp index 355ef50..ac180ba 100644 --- a/example/test/FsfwTestTask.cpp +++ b/example/test/FsfwTestTask.cpp @@ -1,7 +1,10 @@ #include "FsfwTestTask.h" +#include "../test/testFmt.h" FsfwTestTask::FsfwTestTask(object_id_t objectId, bool periodicEvent) - : TestTask(objectId), periodicEvent(periodicEvent) {} + : TestTask(objectId), periodicEvent(periodicEvent) { + fmtTests(); +} ReturnValue_t FsfwTestTask::performPeriodicAction() { if (periodicEvent) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..b6a94e6 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,5 @@ +if(FSFW_ADD_FMT_TESTS) + target_sources(${TARGET_NAME} PRIVATE + testFmt.cpp + ) +endif() diff --git a/test/testFmt.cpp b/test/testFmt.cpp new file mode 100644 index 0000000..35504ee --- /dev/null +++ b/test/testFmt.cpp @@ -0,0 +1,12 @@ +#include "testFmt.h" + +void fmtTests() { + sif::fdebug_t("Hallo\n"); + sif::fdebug("Hallo\n"); + sif::finfo_t("Hallo\n"); + sif::finfo("Hallo\n"); + sif::fwarning("Hello\n"); + sif::fwarning_t("Hello\n"); + sif::ferror("Hello\n"); + sif::ferror_t("Hello\n"); +} \ No newline at end of file diff --git a/test/testFmt.h b/test/testFmt.h new file mode 100644 index 0000000..0a73e55 --- /dev/null +++ b/test/testFmt.h @@ -0,0 +1,82 @@ +#ifndef FSFW_EXAMPLE_HOSTED_TESTFMT_H +#define FSFW_EXAMPLE_HOSTED_TESTFMT_H + +#include +#include +#include + +#include +#include + +#include "fsfw/ipc/MutexFactory.h" +#include "fsfw/ipc/MutexGuard.h" +#include "fsfw/ipc/MutexIF.h" +#include "fsfw/timemanager/Clock.h" + +void fmtTests(); + +namespace sif { + +static std::array PRINT_BUF = {}; +static MutexIF* PRINT_MUTEX = MutexFactory::instance()->createMutex(); + +template +void flog(const char* prefix, fmt::color color, fmt::format_string fmt, T&&... args) { + MutexGuard mg(PRINT_MUTEX); + const auto result = fmt::format_to_n(PRINT_BUF.begin(), PRINT_BUF.size() - 1, "{} | {}", + format(fg(color), prefix), fmt::format(fmt, args...)); + *result.out = '\0'; + fmt::print("{}", PRINT_BUF.data()); +} + +template +void flog_t(const char* prefix, fmt::color color, fmt::format_string fmt, T&&... args) { + Clock::TimeOfDay_t logTime; + Clock::getDateAndTime(&logTime); + flog(prefix, color, "{:02}:{:02}:{:02}.{:03} | {}", logTime.hour, logTime.minute, + logTime.second, logTime.usecond / 1000, fmt::format(fmt, args...)); +} + +template +void fdebug_t(fmt::format_string fmt, T&&... args) { + flog_t("DEBUG", fmt::color::deep_sky_blue, fmt, args...); +} + +template +void fdebug(fmt::format_string fmt, T&&... args) { + flog("DEBUG", fmt::color::deep_sky_blue, fmt, args...); +} + +template +void finfo_t(fmt::format_string fmt, T&&... args) { + flog_t("INFO", fmt::color::forest_green, fmt, args...); +} + +template +void finfo(fmt::format_string fmt, T&&... args) { + flog("INFO", fmt::color::forest_green, fmt, args...); +} + +template +void fwarning(fmt::format_string fmt, T&&... args) { + flog("WARNING", fmt::color::orange_red, fmt, args...); +} + +template +void fwarning_t(fmt::format_string fmt, T&&... args) { + flog_t("WARNING", fmt::color::orange_red, fmt, args...); +} + +template +void ferror(fmt::format_string fmt, T&&... args) { + flog("ERROR", fmt::color::red, fmt, args...); +} + +template +void ferror_t(fmt::format_string fmt, T&&... args) { + flog_t("ERROR", fmt::color::red, fmt, args...); +} + +} + +#endif // FSFW_EXAMPLE_HOSTED_TESTFMT_H