[Fix] support latest spdlog (#423)
* support formatting `PixelFormat` & `DataType` * format enum for legacy spdlog * fix formatpull/438/head
parent
8aba06d2f3
commit
16ee9c7843
|
@ -3,7 +3,11 @@
|
|||
#ifndef MMDEPLOY_SRC_UTILS_FORMATTER_H_
|
||||
#define MMDEPLOY_SRC_UTILS_FORMATTER_H_
|
||||
|
||||
#include <ostream>
|
||||
|
||||
#include "core/logger.h"
|
||||
#include "core/types.h"
|
||||
#include "spdlog/fmt/ostr.h"
|
||||
|
||||
#if FMT_VERSION >= 50000
|
||||
#include "spdlog/fmt/bundled/ranges.h"
|
||||
|
@ -17,12 +21,55 @@ class Value;
|
|||
|
||||
MMDEPLOY_API std::string format_value(const Value& value);
|
||||
|
||||
inline std::string to_string(PixelFormat format) {
|
||||
switch (format) {
|
||||
case PixelFormat::kBGR:
|
||||
return "BGR";
|
||||
case PixelFormat::kRGB:
|
||||
return "RGB";
|
||||
case PixelFormat::kGRAYSCALE:
|
||||
return "GRAYSCALE";
|
||||
case PixelFormat::kNV12:
|
||||
return "NV12";
|
||||
case PixelFormat::kNV21:
|
||||
return "NV21";
|
||||
case PixelFormat::kBGRA:
|
||||
return "BGRA";
|
||||
default:
|
||||
return "invalid_format_enum";
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string to_string(DataType type) {
|
||||
switch (type) {
|
||||
case DataType::kFLOAT:
|
||||
return "FLOAT";
|
||||
case DataType::kHALF:
|
||||
return "HALF";
|
||||
case DataType::kINT8:
|
||||
return "INT8";
|
||||
case DataType::kINT32:
|
||||
return "INT32";
|
||||
case DataType::kINT64:
|
||||
return "INT64";
|
||||
default:
|
||||
return "invalid_data_type_enum";
|
||||
}
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, PixelFormat format) {
|
||||
return os << to_string(format);
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, DataType type) { return os << to_string(type); }
|
||||
|
||||
} // namespace mmdeploy
|
||||
|
||||
namespace fmt {
|
||||
|
||||
#if FMT_VERSION >= 50000
|
||||
|
||||
// `Value` maybe an incomplete type at this point, making `operator<<` not usable
|
||||
template <>
|
||||
struct formatter<mmdeploy::Value> {
|
||||
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
|
||||
|
@ -34,21 +81,16 @@ struct formatter<mmdeploy::Value> {
|
|||
|
||||
#else
|
||||
|
||||
inline void format_arg(BasicFormatter<char> &f, const char *, const mmdeploy::Value &d) {
|
||||
inline void format_arg(BasicFormatter<char>& f, const char*, const mmdeploy::Value& d) {
|
||||
f.writer() << mmdeploy::format_value(d);
|
||||
}
|
||||
|
||||
template <typename T, std::enable_if_t<std::is_enum<std::decay_t<T> >::value, bool> = true>
|
||||
void format_arg(BasicFormatter<char> &f, const char *, const T &v) {
|
||||
f.writer() << (int)v;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
auto format_arg(BasicFormatter<char> &f, const char *, const T &v)
|
||||
auto format_arg(BasicFormatter<char>& f, const char*, const T& v)
|
||||
-> std::void_t<decltype(begin(v), end(v))> {
|
||||
f.writer() << "[";
|
||||
bool first = true;
|
||||
for (const auto &x : v) {
|
||||
for (const auto& x : v) {
|
||||
f.writer() << (first ? "" : ", ") << fmt::format("{}", x);
|
||||
first = false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue