mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
upd: replace {fmt} with imp::fmt
This commit is contained in:
parent
f43606639c
commit
89ba7c762f
@ -29,4 +29,6 @@ file(GLOB HEAD_FILES ${LIBIPC_PROJECT_DIR}/benchmark/*.h)
|
|||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME} benchmark_main imp ipc)
|
target_link_libraries(${PROJECT_NAME}
|
||||||
|
benchmark_main fmt
|
||||||
|
imp ipc)
|
||||||
|
|||||||
@ -54,7 +54,7 @@ public:
|
|||||||
LIBIMP_TRY {
|
LIBIMP_TRY {
|
||||||
data_ = std::forward<U>(src);
|
data_ = std::forward<U>(src);
|
||||||
} LIBIMP_CATCH(std::exception const &e) {
|
} LIBIMP_CATCH(std::exception const &e) {
|
||||||
log.error("failed: `data = std::forward<U>(src)`. error = {}", e.what());
|
log.error("failed: `data = std::forward<U>(src)`. error = ", e.what());
|
||||||
} LIBIMP_CATCH(...) {
|
} LIBIMP_CATCH(...) {
|
||||||
log.error("failed: `data = std::forward<U>(src)`. error = unknown");
|
log.error("failed: `data = std::forward<U>(src)`. error = unknown");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -95,7 +95,7 @@ LIBIMP_EXPORT std::string to_string(long double a, span<char const> fstr = {}) n
|
|||||||
LIBIMP_EXPORT std::string to_string(std::nullptr_t) noexcept;
|
LIBIMP_EXPORT std::string to_string(std::nullptr_t) noexcept;
|
||||||
template <typename T,
|
template <typename T,
|
||||||
typename = std::enable_if_t<std::is_same<T, void>::value>>
|
typename = std::enable_if_t<std::is_same<T, void>::value>>
|
||||||
LIBIMP_EXPORT std::string to_string(T *a) noexcept;
|
LIBIMP_EXPORT std::string to_string(T const volatile *a) noexcept;
|
||||||
|
|
||||||
/// @brief Date and time.
|
/// @brief Date and time.
|
||||||
LIBIMP_EXPORT std::string to_string(std::tm const &a, span<char const> fstr = {}) noexcept;
|
LIBIMP_EXPORT std::string to_string(std::tm const &a, span<char const> fstr = {}) noexcept;
|
||||||
@ -118,5 +118,11 @@ auto tag_invoke(fmt_to_string_t, T &&arg) noexcept
|
|||||||
return ::LIBIMP::to_string(std::forward<T>(arg));
|
return ::LIBIMP::to_string(std::forward<T>(arg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto tag_invoke(fmt_to_string_t, fmt_ref<T> arg) noexcept
|
||||||
|
-> decltype(::LIBIMP::to_string(static_cast<T>(arg.param), arg.fstr)) {
|
||||||
|
return ::LIBIMP::to_string(static_cast<T>(arg.param), arg.fstr);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
LIBIMP_NAMESPACE_END_
|
LIBIMP_NAMESPACE_END_
|
||||||
|
|||||||
@ -11,8 +11,6 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
#include "fmt/format.h"
|
|
||||||
|
|
||||||
#include "libimp/def.h"
|
#include "libimp/def.h"
|
||||||
#include "libimp/detect_plat.h"
|
#include "libimp/detect_plat.h"
|
||||||
#include "libimp/export.h"
|
#include "libimp/export.h"
|
||||||
@ -40,9 +38,7 @@ struct context {
|
|||||||
|
|
||||||
LIBIMP_EXPORT std::string to_string(context &&) noexcept;
|
LIBIMP_EXPORT std::string to_string(context &&) noexcept;
|
||||||
|
|
||||||
/**
|
/// @brief Custom defined fmt_to_string method for imp::fmt
|
||||||
* @brief Custom defined fmt_to_string method for imp::fmt
|
|
||||||
*/
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::string tag_invoke(decltype(::LIBIMP::fmt_to_string), context &&arg) noexcept {
|
std::string tag_invoke(decltype(::LIBIMP::fmt_to_string), context &&arg) noexcept {
|
||||||
return ::LIBIMP::log::to_string(std::move(arg));
|
return ::LIBIMP::log::to_string(std::move(arg));
|
||||||
@ -151,8 +147,8 @@ class grip {
|
|||||||
char const *func_;
|
char const *func_;
|
||||||
level level_limit_;
|
level level_limit_;
|
||||||
|
|
||||||
template <typename Fmt, typename... A>
|
template <typename... A>
|
||||||
grip &output(log::level l, Fmt &&ft, A &&... args) noexcept {
|
grip &output(log::level l, A &&... args) noexcept {
|
||||||
if (!printer_ || (enum_cast(l) < enum_cast(level_limit_))) {
|
if (!printer_ || (enum_cast(l) < enum_cast(level_limit_))) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -160,7 +156,7 @@ class grip {
|
|||||||
LIBIMP_TRY {
|
LIBIMP_TRY {
|
||||||
ctx = {
|
ctx = {
|
||||||
l, std::chrono::system_clock::now(), func_,
|
l, std::chrono::system_clock::now(), func_,
|
||||||
::fmt::format(std::forward<Fmt>(ft), std::forward<A>(args)...),
|
fmt(std::forward<A>(args)...),
|
||||||
};
|
};
|
||||||
} LIBIMP_CATCH(std::exception const &e) {
|
} LIBIMP_CATCH(std::exception const &e) {
|
||||||
/// @remark [TBD] std::string constructor may throw an exception
|
/// @remark [TBD] std::string constructor may throw an exception
|
||||||
@ -178,30 +174,12 @@ public:
|
|||||||
, func_ (func)
|
, func_ (func)
|
||||||
, level_limit_(level_limit) {}
|
, level_limit_(level_limit) {}
|
||||||
|
|
||||||
template <typename Fmt, typename... A>
|
template <typename... A> grip &trace (A &&...args) noexcept { return output(log::level::trace , std::forward<A>(args)...); }
|
||||||
grip &trace(Fmt &&ft, A &&... args) noexcept {
|
template <typename... A> grip &debug (A &&...args) noexcept { return output(log::level::debug , std::forward<A>(args)...); }
|
||||||
return output(log::level::trace, std::forward<Fmt>(ft), std::forward<A>(args)...);
|
template <typename... A> grip &info (A &&...args) noexcept { return output(log::level::info , std::forward<A>(args)...); }
|
||||||
}
|
template <typename... A> grip &warning(A &&...args) noexcept { return output(log::level::warning, std::forward<A>(args)...); }
|
||||||
template <typename Fmt, typename... A>
|
template <typename... A> grip &error (A &&...args) noexcept { return output(log::level::error , std::forward<A>(args)...); }
|
||||||
grip &debug(Fmt &&ft, A &&... args) noexcept {
|
template <typename... A> grip &failed (A &&...args) noexcept { return output(log::level::failed , std::forward<A>(args)...); }
|
||||||
return output(log::level::debug, std::forward<Fmt>(ft), std::forward<A>(args)...);
|
|
||||||
}
|
|
||||||
template <typename Fmt, typename... A>
|
|
||||||
grip &info(Fmt &&ft, A &&... args) noexcept {
|
|
||||||
return output(log::level::info, std::forward<Fmt>(ft), std::forward<A>(args)...);
|
|
||||||
}
|
|
||||||
template <typename Fmt, typename... A>
|
|
||||||
grip &warning(Fmt &&ft, A &&... args) noexcept {
|
|
||||||
return output(log::level::warning, std::forward<Fmt>(ft), std::forward<A>(args)...);
|
|
||||||
}
|
|
||||||
template <typename Fmt, typename... A>
|
|
||||||
grip &error(Fmt &&ft, A &&... args) noexcept {
|
|
||||||
return output(log::level::error, std::forward<Fmt>(ft), std::forward<A>(args)...);
|
|
||||||
}
|
|
||||||
template <typename Fmt, typename... A>
|
|
||||||
grip &failed(Fmt &&ft, A &&... args) noexcept {
|
|
||||||
return output(log::level::failed, std::forward<Fmt>(ft), std::forward<A>(args)...);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace log
|
} // namespace log
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <string>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#include "fmt/format.h"
|
#include "fmt/format.h"
|
||||||
@ -14,6 +15,7 @@
|
|||||||
#include "libimp/def.h"
|
#include "libimp/def.h"
|
||||||
#include "libimp/detect_plat.h"
|
#include "libimp/detect_plat.h"
|
||||||
#include "libimp/export.h"
|
#include "libimp/export.h"
|
||||||
|
#include "libimp/fmt.h"
|
||||||
|
|
||||||
LIBIMP_NAMESPACE_BEG_
|
LIBIMP_NAMESPACE_BEG_
|
||||||
|
|
||||||
@ -70,9 +72,12 @@ struct default_traits<T, std::enable_if_t<std::is_integral<T>::value>> {
|
|||||||
static T cast_from_code(result_type code) noexcept {
|
static T cast_from_code(result_type code) noexcept {
|
||||||
return static_cast<T>(code);
|
return static_cast<T>(code);
|
||||||
}
|
}
|
||||||
|
static std::string format(result<T> const &r) noexcept {
|
||||||
|
return fmt(*r);
|
||||||
|
}
|
||||||
template <typename Out>
|
template <typename Out>
|
||||||
static auto format(result<T> const &r, Out &&out) noexcept {
|
static auto format(result<T> const &r, Out &&out) noexcept {
|
||||||
return format_to(out, "{}", *r);
|
return format_to(out, format(r));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -86,12 +91,15 @@ struct default_traits<T, std::enable_if_t<std::is_pointer<T>::value>> {
|
|||||||
static T cast_from_code(result_type code) noexcept {
|
static T cast_from_code(result_type code) noexcept {
|
||||||
return reinterpret_cast<T>(code);
|
return reinterpret_cast<T>(code);
|
||||||
}
|
}
|
||||||
|
static std::string format(result<T> const &r) noexcept {
|
||||||
|
if LIBIMP_LIKELY(r) {
|
||||||
|
return fmt(static_cast<void *>(*r));
|
||||||
|
}
|
||||||
|
return fmt(static_cast<void *>(*r), ", code = ", r.code_value());
|
||||||
|
}
|
||||||
template <typename Out>
|
template <typename Out>
|
||||||
static auto format(result<T> const &r, Out &&out) noexcept {
|
static auto format(result<T> const &r, Out &&out) noexcept {
|
||||||
if LIBIMP_LIKELY(r) {
|
return format_to(out, format(r));
|
||||||
return format_to(out, "{}", static_cast<void *>(*r));
|
|
||||||
}
|
|
||||||
return format_to(out, "{}, code = {}", static_cast<void *>(*r), r.code_value());
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -157,6 +165,19 @@ public:
|
|||||||
friend bool operator!=(result const &lhs, result const &rhs) noexcept { return lhs.code_ != rhs.code_; }
|
friend bool operator!=(result const &lhs, result const &rhs) noexcept { return lhs.code_ != rhs.code_; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @brief Custom defined fmt_to_string method for imp::fmt
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
inline std::string tag_invoke(decltype(::LIBIMP::fmt_to_string), result_code r) noexcept {
|
||||||
|
return fmt("[", (r ? "succ" : "fail"), ", value = ", *r, "]");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename D>
|
||||||
|
std::string tag_invoke(decltype(::LIBIMP::fmt_to_string), result<T, D> r) noexcept {
|
||||||
|
return fmt("[", (r ? "succ" : "fail"), ", value = ", result<T, D>::default_traits_t::format(r), "]");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
LIBIMP_NAMESPACE_END_
|
LIBIMP_NAMESPACE_END_
|
||||||
|
|
||||||
template <typename T, typename D>
|
template <typename T, typename D>
|
||||||
|
|||||||
@ -13,6 +13,7 @@
|
|||||||
#include "libimp/def.h"
|
#include "libimp/def.h"
|
||||||
#include "libimp/export.h"
|
#include "libimp/export.h"
|
||||||
#include "libimp/result.h"
|
#include "libimp/result.h"
|
||||||
|
#include "libimp/fmt.h"
|
||||||
|
|
||||||
LIBIMP_NAMESPACE_BEG_
|
LIBIMP_NAMESPACE_BEG_
|
||||||
namespace sys {
|
namespace sys {
|
||||||
@ -63,6 +64,13 @@ enum class info : std::int32_t {
|
|||||||
};
|
};
|
||||||
LIBIMP_EXPORT result<std::int64_t> conf(info) noexcept;
|
LIBIMP_EXPORT result<std::int64_t> conf(info) noexcept;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief @brief Custom defined fmt_to_string method for imp::fmt
|
||||||
|
*/
|
||||||
|
inline std::string tag_invoke(decltype(::LIBIMP::fmt_to_string), error r) noexcept {
|
||||||
|
return error_msg(r.code());
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace sys
|
} // namespace sys
|
||||||
LIBIMP_NAMESPACE_END_
|
LIBIMP_NAMESPACE_END_
|
||||||
|
|
||||||
|
|||||||
@ -208,7 +208,7 @@ std::string to_string(std::nullptr_t) noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
std::string to_string<void, void>(void *a) noexcept {
|
std::string to_string<void, void>(void const volatile *a) noexcept {
|
||||||
if (a == nullptr) {
|
if (a == nullptr) {
|
||||||
return to_string(nullptr);
|
return to_string(nullptr);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,8 +3,6 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
#include "fmt/chrono.h"
|
|
||||||
|
|
||||||
LIBIMP_NAMESPACE_BEG_
|
LIBIMP_NAMESPACE_BEG_
|
||||||
namespace log {
|
namespace log {
|
||||||
|
|
||||||
@ -14,7 +12,7 @@ std::string to_string(context &&ctx) noexcept {
|
|||||||
};
|
};
|
||||||
LIBIMP_TRY {
|
LIBIMP_TRY {
|
||||||
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(ctx.tp).time_since_epoch().count() % 1000;
|
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(ctx.tp).time_since_epoch().count() % 1000;
|
||||||
return ::fmt::format("[{}][{:%Y-%m-%d %H:%M:%S}.{:03}][{}] {}", types[enum_cast(ctx.level)], ctx.tp, ms, ctx.func, ctx.text);
|
return fmt("[", types[enum_cast(ctx.level)], "][", ctx.tp, ".", spec("03")(ms), "][", ctx.func, "] ", ctx.text);
|
||||||
} LIBIMP_CATCH(std::exception const &e) {
|
} LIBIMP_CATCH(std::exception const &e) {
|
||||||
/// @remark [TBD] std::string constructor may throw an exception
|
/// @remark [TBD] std::string constructor may throw an exception
|
||||||
return e.what();
|
return e.what();
|
||||||
|
|||||||
@ -29,7 +29,7 @@ std::string demangle(span<char const> name) noexcept {
|
|||||||
int status = 0;
|
int status = 0;
|
||||||
char *realname = abi::__cxa_demangle(name.data(), buffer, &sz, &status);
|
char *realname = abi::__cxa_demangle(name.data(), buffer, &sz, &status);
|
||||||
if (realname == nullptr) {
|
if (realname == nullptr) {
|
||||||
log.error("failed: abi::__cxa_demangle(sz = {}), status = {}", sz, status);
|
log.error("failed: abi::__cxa_demangle(sz = ", sz, "), status = ", status);
|
||||||
std::free(buffer);
|
std::free(buffer);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,8 +46,9 @@ std::string error_str(result_code code) noexcept {
|
|||||||
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
|
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
|
||||||
LIBIMP_LOG_();
|
LIBIMP_LOG_();
|
||||||
if (::strerror_r((int)code.value(), msg_buf, sizeof(msg_buf)) != 0) {
|
if (::strerror_r((int)code.value(), msg_buf, sizeof(msg_buf)) != 0) {
|
||||||
log.error("failed: strerror_r(code = {}, buf, buf-size = {}). error = {}",
|
log.error("failed: strerror_r(code = ", (int)code.value(),
|
||||||
(int)code.value(), sizeof(msg_buf), error_code());
|
", buf, buf-size = ", sizeof(msg_buf),
|
||||||
|
"). error = ", error_code());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
return msg_buf;
|
return msg_buf;
|
||||||
@ -70,11 +71,11 @@ result<std::int64_t> conf(info r) noexcept {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.error("invalid info = {}", enum_cast(r));
|
log.error("invalid info = ", enum_cast(r));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("info = {}, error = {}", enum_cast(r), err);
|
log.error("info = ", enum_cast(r), ", error = ", err);
|
||||||
return {false, (int)err.value()};
|
return {false, (int)err.value()};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -36,7 +36,7 @@ std::size_t cvt_cstr(char const *src, std::size_t slen, wchar_t *des, std::size_
|
|||||||
int cch_wc = (des == nullptr) ? 0 : (int)dlen;
|
int cch_wc = (des == nullptr) ? 0 : (int)dlen;
|
||||||
int size_needed = ::MultiByteToWideChar(CP_ACP, 0, src, (int)slen, des, cch_wc);
|
int size_needed = ::MultiByteToWideChar(CP_ACP, 0, src, (int)slen, des, cch_wc);
|
||||||
if (size_needed <= 0) {
|
if (size_needed <= 0) {
|
||||||
log.error("failed: MultiByteToWideChar(CP_ACP). error = {}", sys::error_code());
|
log.error("failed: MultiByteToWideChar(CP_ACP). error = ", sys::error_code());
|
||||||
}
|
}
|
||||||
return size_needed;
|
return size_needed;
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ std::size_t cvt_cstr(wchar_t const *src, std::size_t slen, char *des, std::size_
|
|||||||
int cb_mb = (des == nullptr) ? 0 : (int)dlen;
|
int cb_mb = (des == nullptr) ? 0 : (int)dlen;
|
||||||
int size_needed = ::WideCharToMultiByte(CP_ACP, 0, src, (int)slen, des, cb_mb, NULL, NULL);
|
int size_needed = ::WideCharToMultiByte(CP_ACP, 0, src, (int)slen, des, cb_mb, NULL, NULL);
|
||||||
if (size_needed <= 0) {
|
if (size_needed <= 0) {
|
||||||
log.error("failed: WideCharToMultiByte(CP_ACP). error = {}", sys::error_code());
|
log.error("failed: WideCharToMultiByte(CP_ACP). error = ", sys::error_code());
|
||||||
}
|
}
|
||||||
return size_needed;
|
return size_needed;
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ std::size_t cvt_cstr(char8_t const *src, std::size_t slen, wchar_t *des, std::si
|
|||||||
int cch_wc = (des == nullptr) ? 0 : (int)dlen;
|
int cch_wc = (des == nullptr) ? 0 : (int)dlen;
|
||||||
int size_needed = ::MultiByteToWideChar(CP_UTF8, 0, (char *)src, (int)slen, des, cch_wc);
|
int size_needed = ::MultiByteToWideChar(CP_UTF8, 0, (char *)src, (int)slen, des, cch_wc);
|
||||||
if (size_needed <= 0) {
|
if (size_needed <= 0) {
|
||||||
log.error("failed: MultiByteToWideChar(CP_UTF8). error = {}", sys::error_code());
|
log.error("failed: MultiByteToWideChar(CP_UTF8). error = ", sys::error_code());
|
||||||
}
|
}
|
||||||
return size_needed;
|
return size_needed;
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ std::size_t cvt_cstr(wchar_t const *src, std::size_t slen, char8_t *des, std::si
|
|||||||
int cb_mb = (des == nullptr) ? 0 : (int)dlen;
|
int cb_mb = (des == nullptr) ? 0 : (int)dlen;
|
||||||
int size_needed = ::WideCharToMultiByte(CP_UTF8, 0, src, (int)slen, (char *)des, cb_mb, NULL, NULL);
|
int size_needed = ::WideCharToMultiByte(CP_UTF8, 0, src, (int)slen, (char *)des, cb_mb, NULL, NULL);
|
||||||
if (size_needed <= 0) {
|
if (size_needed <= 0) {
|
||||||
log.error("failed: WideCharToMultiByte(CP_UTF8). error = {}", sys::error_code());
|
log.error("failed: WideCharToMultiByte(CP_UTF8). error = ", sys::error_code());
|
||||||
}
|
}
|
||||||
return size_needed;
|
return size_needed;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -59,7 +59,7 @@ std::string error_str(result_code code) noexcept {
|
|||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||||
(LPTSTR)&lpErrText,
|
(LPTSTR)&lpErrText,
|
||||||
0, NULL) == 0) {
|
0, NULL) == 0) {
|
||||||
log.error("failed: FormatMessage(dwMessageId = {}). error = {}", err, error_code());
|
log.error("failed: FormatMessage(dwMessageId = ", err, "). error = ", error_code());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
LIBIMP_UNUSED auto buf_guard = std::unique_ptr<std::remove_pointer_t<LPVOID>,
|
LIBIMP_UNUSED auto buf_guard = std::unique_ptr<std::remove_pointer_t<LPVOID>,
|
||||||
@ -94,7 +94,7 @@ result<std::int64_t> conf(info r) noexcept {
|
|||||||
return (std::int64_t)info.dwPageSize;
|
return (std::int64_t)info.dwPageSize;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.error("invalid info = {}", enum_cast(r));
|
log.error("invalid info = ", enum_cast(r));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
|
|
||||||
#include "fmt/format.h"
|
|
||||||
|
|
||||||
#include "libimp/detect_plat.h"
|
#include "libimp/detect_plat.h"
|
||||||
#if defined(LIBIMP_OS_WIN)
|
#if defined(LIBIMP_OS_WIN)
|
||||||
# include "libimp/platform/win/system.h"
|
# include "libimp/platform/win/system.h"
|
||||||
@ -13,7 +11,7 @@ namespace sys {
|
|||||||
|
|
||||||
std::string error_msg(result_code code) noexcept {
|
std::string error_msg(result_code code) noexcept {
|
||||||
LIBIMP_TRY {
|
LIBIMP_TRY {
|
||||||
return ::fmt::format("[{}, \"{}\"]", code.value(), error_str(code));
|
return fmt("[", code.value(), ", \"", error_str(code), "\"]");
|
||||||
} LIBIMP_CATCH(...) {
|
} LIBIMP_CATCH(...) {
|
||||||
return error_str(code);
|
return error_str(code);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -69,7 +69,7 @@ result<int> shm_open_fd(std::string const &name, mode::type type) noexcept {
|
|||||||
flag |= O_CREAT;
|
flag |= O_CREAT;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
log.error("mode type is invalid. type = {}", type);
|
log.error("mode type is invalid. type = ", type);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ result_code ftruncate_fd(int fd, std::size_t size) noexcept {
|
|||||||
/// @see https://man7.org/linux/man-pages/man3/ftruncate.3p.html
|
/// @see https://man7.org/linux/man-pages/man3/ftruncate.3p.html
|
||||||
if (::ftruncate(fd, size) != posix::succ) {
|
if (::ftruncate(fd, size) != posix::succ) {
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("failed: ftruncate({}, {}). error = {}", fd, size, err);
|
log.error("failed: ftruncate(", fd, ", ", size, "). error = ", err);
|
||||||
return err.code();
|
return err.code();
|
||||||
}
|
}
|
||||||
return {posix::succ};
|
return {posix::succ};
|
||||||
@ -102,8 +102,9 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
|
|||||||
auto fd = shm_open_fd(name, type);
|
auto fd = shm_open_fd(name, type);
|
||||||
if (!fd) return {};
|
if (!fd) return {};
|
||||||
if (*fd == posix::failed) {
|
if (*fd == posix::failed) {
|
||||||
log.error("failed: shm_open(name = {}, type = {}). error = {}",
|
log.error("failed: shm_open(name = ", name,
|
||||||
name, type, sys::error());
|
", type = ", type,
|
||||||
|
"). error = ", sys::error());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
LIBIMP_UNUSED auto guard = std::unique_ptr<decltype(fd), void (*)(decltype(fd) *)> {
|
LIBIMP_UNUSED auto guard = std::unique_ptr<decltype(fd), void (*)(decltype(fd) *)> {
|
||||||
@ -114,7 +115,7 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
|
|||||||
/// @brief Try to get the size of this fd
|
/// @brief Try to get the size of this fd
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (::fstat(*fd, &st) == posix::failed) {
|
if (::fstat(*fd, &st) == posix::failed) {
|
||||||
log.error("failed: fstat(fd = {}). error = {}", *fd, sys::error());
|
log.error("failed: fstat(fd = ", *fd, "). error = ", sys::error());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,7 +133,7 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
|
|||||||
/// @brief Creates a new mapping in the virtual address space of the calling process.
|
/// @brief Creates a new mapping in the virtual address space of the calling process.
|
||||||
void *mem = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
|
void *mem = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
|
||||||
if (mem == MAP_FAILED) {
|
if (mem == MAP_FAILED) {
|
||||||
log.error("failed: mmap(size = {}, fd = {}). error = {}", size, *fd, sys::error());
|
log.error("failed: mmap(size = ", size, ", fd = ", *fd, "). error = ", sys::error());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
return new shm_handle{std::move(name), size, mem};
|
return new shm_handle{std::move(name), size, mem};
|
||||||
@ -147,7 +148,7 @@ result_code shm_close(shm_t h) noexcept {
|
|||||||
if (shm == nullptr) return {};
|
if (shm == nullptr) return {};
|
||||||
if (::munmap(shm->memp, shm->f_sz) == posix::failed) {
|
if (::munmap(shm->memp, shm->f_sz) == posix::failed) {
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("failed: munmap({}, {}). error = {}", shm->memp, shm->f_sz, err);
|
log.error("failed: munmap(", shm->memp, ", ", shm->f_sz"). error = ", err);
|
||||||
return err.code();
|
return err.code();
|
||||||
}
|
}
|
||||||
/// @brief no unlink the file.
|
/// @brief no unlink the file.
|
||||||
|
|||||||
@ -30,11 +30,11 @@ inline LPSECURITY_ATTRIBUTES get_sa() {
|
|||||||
using namespace ::LIBIMP;
|
using namespace ::LIBIMP;
|
||||||
log::grip log {"get_sa"};
|
log::grip log {"get_sa"};
|
||||||
if (!::InitializeSecurityDescriptor(&sd_, SECURITY_DESCRIPTOR_REVISION)) {
|
if (!::InitializeSecurityDescriptor(&sd_, SECURITY_DESCRIPTOR_REVISION)) {
|
||||||
log.error("failed: InitializeSecurityDescriptor(SECURITY_DESCRIPTOR_REVISION). error = {}", sys::error());
|
log.error("failed: InitializeSecurityDescriptor(SECURITY_DESCRIPTOR_REVISION). error = ", sys::error());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) {
|
if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) {
|
||||||
log.error("failed: SetSecurityDescriptorDacl. error = {}", sys::error());
|
log.error("failed: SetSecurityDescriptorDacl. error = ", sys::error());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sa_.nLength = sizeof(SECURITY_ATTRIBUTES);
|
sa_.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||||
|
|||||||
@ -41,7 +41,7 @@ result_code mmap_close(HANDLE h) {
|
|||||||
}
|
}
|
||||||
if (!::CloseHandle(h)) {
|
if (!::CloseHandle(h)) {
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("failed: CloseHandle({}). error = {}", h, err);
|
log.error("failed: CloseHandle(", h, "). error = ", err);
|
||||||
return err.code();
|
return err.code();
|
||||||
}
|
}
|
||||||
return {ERROR_SUCCESS};
|
return {ERROR_SUCCESS};
|
||||||
@ -77,7 +77,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
|||||||
HANDLE h = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, t_name.c_str());
|
HANDLE h = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, t_name.c_str());
|
||||||
if (h == NULL) {
|
if (h == NULL) {
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("failed: OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, {}). error = {}", file, err);
|
log.error("failed: OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, ", file, "). error = ", err);
|
||||||
return {nullptr, err.value()};
|
return {nullptr, err.value()};
|
||||||
}
|
}
|
||||||
return h;
|
return h;
|
||||||
@ -90,7 +90,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
|||||||
0, static_cast<DWORD>(size), t_name.c_str());
|
0, static_cast<DWORD>(size), t_name.c_str());
|
||||||
if (h == NULL) {
|
if (h == NULL) {
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("failed: CreateFileMapping(PAGE_READWRITE | SEC_COMMIT, {}, {}). error = {}", size, file, err);
|
log.error("failed: CreateFileMapping(PAGE_READWRITE | SEC_COMMIT, ", size, ", ", file, "). error = ", err);
|
||||||
return {nullptr, err.value()};
|
return {nullptr, err.value()};
|
||||||
}
|
}
|
||||||
return h;
|
return h;
|
||||||
@ -104,7 +104,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
|||||||
/// https://learn.microsoft.com/en-us/previous-versions/windows/embedded/aa517331(v=msdn.10)
|
/// https://learn.microsoft.com/en-us/previous-versions/windows/embedded/aa517331(v=msdn.10)
|
||||||
return try_open();
|
return try_open();
|
||||||
} else if (!(type & mode::create)) {
|
} else if (!(type & mode::create)) {
|
||||||
log.error("mode type is invalid. type = {}", type);
|
log.error("mode type is invalid. type = ", type);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto h = try_create();
|
auto h = try_create();
|
||||||
@ -112,7 +112,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
|
|||||||
/// @remark If the object exists before the function call, the function returns a handle to the existing object
|
/// @remark If the object exists before the function call, the function returns a handle to the existing object
|
||||||
/// (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS.
|
/// (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS.
|
||||||
if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) {
|
if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) {
|
||||||
log.info("the file being created already exists. file = {}, type = {}", file, type);
|
log.info("the file being created already exists. file = ", file, ", type = ", type);
|
||||||
mmap_close(*h);
|
mmap_close(*h);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@ -132,7 +132,7 @@ result<LPVOID> mmap_memof(HANDLE h) {
|
|||||||
LPVOID mem = ::MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, 0);
|
LPVOID mem = ::MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, 0);
|
||||||
if (mem == NULL) {
|
if (mem == NULL) {
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("failed: MapViewOfFile({}, FILE_MAP_ALL_ACCESS). error = {}", h, err);
|
log.error("failed: MapViewOfFile(", h, ", FILE_MAP_ALL_ACCESS). error = ", err);
|
||||||
return {nullptr, err.value()};
|
return {nullptr, err.value()};
|
||||||
}
|
}
|
||||||
return mem;
|
return mem;
|
||||||
@ -151,7 +151,7 @@ result<SIZE_T> mmap_sizeof(LPCVOID mem) {
|
|||||||
MEMORY_BASIC_INFORMATION mem_info {};
|
MEMORY_BASIC_INFORMATION mem_info {};
|
||||||
if (::VirtualQuery(mem, &mem_info, sizeof(mem_info)) == 0) {
|
if (::VirtualQuery(mem, &mem_info, sizeof(mem_info)) == 0) {
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("failed: VirtualQuery({}). error = {}", mem, err);
|
log.error("failed: VirtualQuery(", mem, "). error = ", err);
|
||||||
return {false, (SIZE_T)err.value()};
|
return {false, (SIZE_T)err.value()};
|
||||||
}
|
}
|
||||||
return mem_info.RegionSize;
|
return mem_info.RegionSize;
|
||||||
@ -172,7 +172,7 @@ result_code mmap_release(HANDLE h, LPCVOID mem) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
if (!::UnmapViewOfFile(mem)) {
|
if (!::UnmapViewOfFile(mem)) {
|
||||||
log.warning("failed: UnmapViewOfFile. error = {}", sys::error());
|
log.warning("failed: UnmapViewOfFile. error = ", sys::error());
|
||||||
}
|
}
|
||||||
return mmap_close(h);
|
return mmap_close(h);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,18 +21,18 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
|
|||||||
LIBIMP_LOG_();
|
LIBIMP_LOG_();
|
||||||
auto h = mmap_open(name, size, type);
|
auto h = mmap_open(name, size, type);
|
||||||
if (h == NULL) {
|
if (h == NULL) {
|
||||||
log.error("failed: mmap_open(name = {}, size = {}, type = {}).", name, size, type);
|
log.error("failed: mmap_open(name = ", name, ", size = ", size, ", type = ", type, ").");
|
||||||
return {nullptr, h.code_value()};
|
return {nullptr, h.code_value()};
|
||||||
}
|
}
|
||||||
auto mem = mmap_memof(*h);
|
auto mem = mmap_memof(*h);
|
||||||
if (*mem == NULL) {
|
if (*mem == NULL) {
|
||||||
log.error("failed: mmap_memof({}).", *h);
|
log.error("failed: mmap_memof(", *h, ").");
|
||||||
mmap_close(*h);
|
mmap_close(*h);
|
||||||
return {nullptr, mem.code_value()};
|
return {nullptr, mem.code_value()};
|
||||||
}
|
}
|
||||||
auto sz = mmap_sizeof(*mem);
|
auto sz = mmap_sizeof(*mem);
|
||||||
if (!sz) {
|
if (!sz) {
|
||||||
log.error("failed: mmap_sizeof({}).", *mem);
|
log.error("failed: mmap_sizeof(", *mem, ").");
|
||||||
mmap_close(*h);
|
mmap_close(*h);
|
||||||
return {nullptr, static_cast<result_type>(sz.value())};
|
return {nullptr, static_cast<result_type>(sz.value())};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ bool verify_args(::LIBIMP::log::grip &log, std::size_t bytes, std::size_t alignm
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ((alignment == 0) || (alignment & (alignment - 1)) != 0) {
|
if ((alignment == 0) || (alignment & (alignment - 1)) != 0) {
|
||||||
log.error("invalid bytes = {}, alignment = {}", bytes, alignment);
|
log.error("invalid bytes = ", bytes, ", alignment = ", alignment);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -66,8 +66,9 @@ void *new_delete_resource::allocate(std::size_t bytes, std::size_t alignment) no
|
|||||||
void *p = nullptr;
|
void *p = nullptr;
|
||||||
int ret = ::posix_memalign(&p, alignment, bytes);
|
int ret = ::posix_memalign(&p, alignment, bytes);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
log.error("failed: posix_memalign(alignment = {}, bytes = {}). error = {}",
|
log.error("failed: posix_memalign(alignment = ", alignment,
|
||||||
alignment, bytes, sys::error(ret));
|
", bytes = ", bytes,
|
||||||
|
"). error = ", sys::error(ret));
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
|
|||||||
@ -93,7 +93,7 @@ namespace {
|
|||||||
template <typename PC>
|
template <typename PC>
|
||||||
void test_concur(std::size_t np, std::size_t nc, std::size_t k) {
|
void test_concur(std::size_t np, std::size_t nc, std::size_t k) {
|
||||||
LIBIMP_LOG_();
|
LIBIMP_LOG_();
|
||||||
log.info("\n\tStart with: {}, {} producers, {} consumers...", imp::nameof<PC>(), np, nc);
|
log.info("\n\tStart with: ", imp::nameof<PC>(), ", ", np, " producers, ", nc, " consumers...");
|
||||||
|
|
||||||
constexpr static std::uint32_t loop_size = 100'0000;
|
constexpr static std::uint32_t loop_size = 100'0000;
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ void test_concur(std::size_t np, std::size_t nc, std::size_t k) {
|
|||||||
std::this_thread::yield();
|
std::this_thread::yield();
|
||||||
}
|
}
|
||||||
if (i % (loop_size / 10) == 0) {
|
if (i % (loop_size / 10) == 0) {
|
||||||
log.info("[{}] put count: {}", n, i);
|
log.info("[", n, "] put count: ", i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--running;
|
--running;
|
||||||
|
|||||||
@ -69,13 +69,13 @@ TEST(result, fmt) {
|
|||||||
imp::result<int> r1 {false, -123};
|
imp::result<int> r1 {false, -123};
|
||||||
EXPECT_EQ(fmt::format("{}", r1), fmt::format("[fail, value = {}]", -123));
|
EXPECT_EQ(fmt::format("{}", r1), fmt::format("[fail, value = {}]", -123));
|
||||||
imp::result<void *> r2 {&r1};
|
imp::result<void *> r2 {&r1};
|
||||||
EXPECT_EQ(fmt::format("{}", r2), fmt::format("[succ, value = {}]", (void *)&r1));
|
EXPECT_EQ(fmt::format("{}", r2), imp::fmt("[succ, value = ", (void *)&r1, "]"));
|
||||||
int aaa {};
|
int aaa {};
|
||||||
imp::result<int *> r3 {&aaa};
|
imp::result<int *> r3 {&aaa};
|
||||||
EXPECT_EQ(fmt::format("{}", r3), fmt::format("[succ, value = {}]", (void *)&aaa));
|
EXPECT_EQ(fmt::format("{}", r3), imp::fmt("[succ, value = ", (void *)&aaa, "]"));
|
||||||
imp::result<int *> r4 {nullptr};
|
imp::result<int *> r4 {nullptr};
|
||||||
EXPECT_EQ(fmt::format("{}", r4), fmt::format("[fail, value = {}, code = 0]", nullptr));
|
EXPECT_EQ(fmt::format("{}", r4), imp::fmt("[fail, value = ", nullptr, ", code = 0]"));
|
||||||
r4 = {nullptr, 1234};
|
r4 = {nullptr, 1234};
|
||||||
EXPECT_EQ(fmt::format("{}", r4), fmt::format("[fail, value = {}, code = 1234]", nullptr));
|
EXPECT_EQ(fmt::format("{}", r4), imp::fmt("[fail, value = ", nullptr, ", code = 1234]"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user