fsfw-example-common/example/test/testFmt.h

83 lines
2.3 KiB
C++

#ifndef FSFW_EXAMPLE_HOSTED_TESTFMT_H
#define FSFW_EXAMPLE_HOSTED_TESTFMT_H
#include <fmt/chrono.h>
#include <fmt/color.h>
#include <fmt/core.h>
#include <array>
#include <cstdint>
#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<char, 256> PRINT_BUF = {};
static MutexIF* PRINT_MUTEX = MutexFactory::instance()->createMutex();
template <typename... T>
void flog(const char* prefix, fmt::color color, fmt::format_string<T...> 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 <typename... T>
void flog_t(const char* prefix, fmt::color color, fmt::format_string<T...> 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 <typename... T>
void fdebug_t(fmt::format_string<T...> fmt, T&&... args) {
flog_t("DEBUG", fmt::color::deep_sky_blue, fmt, args...);
}
template <typename... T>
void fdebug(fmt::format_string<T...> fmt, T&&... args) {
flog("DEBUG", fmt::color::deep_sky_blue, fmt, args...);
}
template <typename... T>
void finfo_t(fmt::format_string<T...> fmt, T&&... args) {
flog_t("INFO", fmt::color::forest_green, fmt, args...);
}
template <typename... T>
void finfo(fmt::format_string<T...> fmt, T&&... args) {
flog("INFO", fmt::color::forest_green, fmt, args...);
}
template <typename... T>
void fwarning(fmt::format_string<T...> fmt, T&&... args) {
flog("WARNING", fmt::color::orange_red, fmt, args...);
}
template <typename... T>
void fwarning_t(fmt::format_string<T...> fmt, T&&... args) {
flog_t("WARNING", fmt::color::orange_red, fmt, args...);
}
template <typename... T>
void ferror(fmt::format_string<T...> fmt, T&&... args) {
flog("ERROR", fmt::color::red, fmt, args...);
}
template <typename... T>
void ferror_t(fmt::format_string<T...> fmt, T&&... args) {
flog_t("ERROR", fmt::color::red, fmt, args...);
}
}
#endif // FSFW_EXAMPLE_HOSTED_TESTFMT_H