From e1aa39f5e4ccd7dc5694e2f5bffb5e3161778097 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 8 May 2022 17:11:43 +0200 Subject: [PATCH] i guess no way around exceptions.. --- example/test/testFmt.cpp | 18 +++--- example/test/testFmt.h | 133 ++++++++++++++++++++++++++++++--------- 2 files changed, 115 insertions(+), 36 deletions(-) diff --git a/example/test/testFmt.cpp b/example/test/testFmt.cpp index 35504ee..e66ddc9 100644 --- a/example/test/testFmt.cpp +++ b/example/test/testFmt.cpp @@ -1,12 +1,14 @@ #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"); + sif::fdebug(__FILENAME__, __LINE__, "Hello {} {}", "World\n"); + sif::fdebug_t(__FILENAME__, __LINE__, "Hallo\n"); + FSFW_LOGD("{}", "Hallo\n"); + // MY_LOG("{}", "test\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/example/test/testFmt.h b/example/test/testFmt.h index 0a73e55..c47d683 100644 --- a/example/test/testFmt.h +++ b/example/test/testFmt.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -13,70 +14,146 @@ #include "fsfw/ipc/MutexIF.h" #include "fsfw/timemanager/Clock.h" +#define __FILENAME_REL__ (((const char*)__FILE__ + SOURCE_PATH_SIZE)) +#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) + void fmtTests(); namespace sif { -static std::array PRINT_BUF = {}; +static std::array PRINT_BUF = {}; + +static const char INFO_PREFIX[] = "INFO"; +static const char DEBUG_PREFIX[] = "DEBUG"; +static const char WARNING_PREFIX[] = "WARNING"; +static const char ERROR_PREFIX[] = "ERROR"; + +enum class LogLevel : unsigned int { DEBUG = 0, INFO = 1, WARNING = 2, ERROR = 3 }; + +static const char* PREFIX_ARR[4] = {DEBUG_PREFIX, INFO_PREFIX, WARNING_PREFIX, ERROR_PREFIX}; + +static const std::array LOG_COLOR_ARR = { + fmt::color::deep_sky_blue, fmt::color::forest_green, fmt::color::orange_red, fmt::color::red}; + 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()); +static size_t writeTypePrefix(LogLevel level) { + auto idx = static_cast(level); + const auto result = + fmt::format_to_n(PRINT_BUF.begin(), PRINT_BUF.size() - 1, + fmt::runtime(fmt::format(fg(LOG_COLOR_ARR[idx]), PREFIX_ARR[idx]))); + return result.size; } 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...)); +size_t logTraced(LogLevel level, const char* file, unsigned int line, bool timed, + fmt::format_string fmt, T&&... args) noexcept { + try { + MutexGuard mg(PRINT_MUTEX); + size_t bufPos = writeTypePrefix(level); + auto currentIter = PRINT_BUF.begin() + bufPos; + if (timed) { + Clock::TimeOfDay_t logTime; + Clock::getDateAndTime(&logTime); + const auto result = fmt::format_to_n(currentIter, PRINT_BUF.size() - 1 - bufPos, + " | {}[l.{}] | {:02}:{:02}:{:02}.{:03} | {}", file, line, + logTime.hour, logTime.minute, logTime.second, + logTime.usecond / 1000, fmt::format(fmt, args...)); + *result.out = '\0'; + bufPos += result.size; + } else { + const auto result = fmt::format_to_n(currentIter, PRINT_BUF.size() - 1 - bufPos, + " | {}[l.{}] | {}", file, line, fmt::format(fmt, args...)); + *result.out = '\0'; + bufPos += result.size; + } + + fmt::print(fmt::runtime(PRINT_BUF.data())); + return bufPos; + } catch (const fmt::v8::format_error& e) { + fmt::print("Printing failed with error: {}\n", e.what()); + return 0; + } } template -void fdebug_t(fmt::format_string fmt, T&&... args) { - flog_t("DEBUG", fmt::color::deep_sky_blue, fmt, args...); +size_t log(LogLevel level, bool timed, fmt::format_string fmt, T&&... args) noexcept { + try { + MutexGuard mg(PRINT_MUTEX); + size_t bufPos = writeTypePrefix(level); + auto currentIter = PRINT_BUF.begin() + bufPos; + if (timed) { + Clock::TimeOfDay_t logTime; + Clock::getDateAndTime(&logTime); + const auto result = fmt::format_to_n( + currentIter, PRINT_BUF.size() - bufPos, " | {:02}:{:02}:{:02}.{:03} | {}", logTime.hour, + logTime.minute, logTime.second, logTime.usecond / 1000, fmt::format(fmt, args...)); + bufPos += result.size; + } + fmt::print(fmt::runtime(PRINT_BUF.data())); + return bufPos; + } catch (const fmt::v8::format_error& e) { + fmt::print("Printing failed with error: {}\n", e.what()); + return 0; + } } template -void fdebug(fmt::format_string fmt, T&&... args) { - flog("DEBUG", fmt::color::deep_sky_blue, fmt, args...); +void fdebug(const char* file, unsigned int line, fmt::format_string fmt, T&&... args) noexcept { + logTraced(LogLevel::DEBUG, file, line, false, fmt, args...); +} + +template +void fdebug_t(const char* file, unsigned int line, fmt::format_string fmt, T&&... args) noexcept { + logTraced(LogLevel::DEBUG, file, line, true, fmt, args...); } template void finfo_t(fmt::format_string fmt, T&&... args) { - flog_t("INFO", fmt::color::forest_green, fmt, args...); + log(LogLevel::INFO, true, fmt, args...); } template void finfo(fmt::format_string fmt, T&&... args) { - flog("INFO", fmt::color::forest_green, fmt, args...); + log(LogLevel::INFO, false, fmt, args...); } template -void fwarning(fmt::format_string fmt, T&&... args) { - flog("WARNING", fmt::color::orange_red, fmt, args...); +void fwarning(const char* file, unsigned int line, fmt::format_string fmt, T&&... args) { + logTraced(LogLevel::WARNING, file, line, false, fmt, args...); } template -void fwarning_t(fmt::format_string fmt, T&&... args) { - flog_t("WARNING", fmt::color::orange_red, fmt, args...); +void fwarning_t(const char* file, unsigned int line, fmt::format_string fmt, T&&... args) { + logTraced(LogLevel::WARNING, file, line, true, fmt, args...); } template -void ferror(fmt::format_string fmt, T&&... args) { - flog("ERROR", fmt::color::red, fmt, args...); +void ferror(const char* file, unsigned int line, fmt::format_string fmt, T&&... args) { + logTraced(LogLevel::ERROR, file, line, false, fmt, args...); } template -void ferror_t(fmt::format_string fmt, T&&... args) { - flog_t("ERROR", fmt::color::red, fmt, args...); +void ferror_t(const char* file, unsigned int line, fmt::format_string fmt, T&&... args) { + logTraced(LogLevel::ERROR, file, line, true, fmt, args...); } -} +} // namespace sif + +#define FSFW_LOGI(format, ...) finfo(FMT_STRING(format), __VA_ARGS__) + +#define FSFW_LOGIT(format, ...) finfo_t(FMT_STRING(format), __VA_ARGS__) + +#define FSFW_LOGD(format, ...) sif::fdebug(__FILENAME__, __LINE__, FMT_STRING(format), __VA_ARGS__) + +#define FSFW_LOGDT(format, ...) fdebug_t(__FILENAME__, __LINE__, FMT_STRING(format), __VA_ARGS__) + +#define FSFW_LOGW(format, ...) fdebug(__FILENAME__, __LINE__, FMT_STRING(format), __VA_ARGS__) + +#define FSFW_LOGWT(format, ...) fdebug_t(__FILENAME__, __LINE__, FMT_STRING(format), __VA_ARGS__) + +#define FSFW_LOGE(format, ...) fdebug(__FILENAME__, __LINE__, FMT_STRING(format), __VA_ARGS__) + +#define FSFW_LOGET(format, ...) fdebug_t(__FILENAME__, __LINE__, FMT_STRING(format), __VA_ARGS__) #endif // FSFW_EXAMPLE_HOSTED_TESTFMT_H