29 #include <string_view>
62 using namespace std::chrono;
63 const auto now = system_clock::now();
64 const auto tt = system_clock::to_time_t(now);
66 #if defined(UNILINK_PLATFORM_WINDOWS)
67 localtime_s(&tm, &tt);
69 localtime_r(&tt, &tm);
71 const auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
72 std::ostringstream oss;
73 oss << std::put_time(&tm,
"%F %T") <<
'.' << std::setw(3) << std::setfill(
'0') << ms.count();
77 inline void log_message(std::string_view tag, std::string_view direction, std::string_view message) {
79 std::string_view clean_message = message;
80 if (!clean_message.empty() && clean_message.back() ==
'\n') {
81 clean_message.remove_suffix(1);
101 namespace safe_memory {
109 inline void safe_memcpy(uint8_t* dest,
const uint8_t* src,
size_t size) {
111 throw std::invalid_argument(
"Destination pointer is null");
114 throw std::invalid_argument(
"Source pointer is null");
119 if (size > 1024 * 1024) {
120 throw std::invalid_argument(
"Copy size too large (max 1MB)");
123 std::memcpy(dest, src, size);
133 inline void safe_memcpy(
char* dest,
const char* src,
size_t size) {
134 safe_memcpy(
reinterpret_cast<uint8_t*
>(dest),
reinterpret_cast<const uint8_t*
>(src), size);
139 namespace safe_convert {
147 if (!data || size == 0) {
148 return std::string{};
150 return std::string(data, data + size);
160 if (!data || size == 0) {
161 return std::vector<uint8_t>{};
163 return std::vector<uint8_t>(data, data + size);
172 return std::vector<uint8_t>(str.begin(), str.end());
181 return {
reinterpret_cast<const uint8_t*
>(str.data()), str.size()};
190 namespace safe_memory = base::safe_memory;
191 namespace safe_convert = base::safe_convert;
static Logger & instance()
Get singleton instance.
void info(std::string_view component, std::string_view operation, std::string_view message)
std::string uint8_to_string(const uint8_t *data, size_t size)
Safely convert uint8_t* to const char* for string operations.
std::vector< uint8_t > string_to_uint8(const char *data, size_t size)
Safely convert const char* to const uint8_t* for binary operations.
std::pair< const uint8_t *, size_t > string_to_bytes(std::string_view str)
Safely obtain a view of std::string as byte array without allocation.
void safe_memcpy(uint8_t *dest, const uint8_t *src, size_t size)
Safely copy memory with bounds checking.
std::string get_platform_warning()
void log_message(std::string_view tag, std::string_view direction, std::string_view message)
bool is_experimental_features_available()
bool is_performance_monitoring_available()
bool is_latest_optimizations_available()
bool is_advanced_logging_available()
const char * to_cstr(LinkState s)