ifw-fnd 1.0.0
Loading...
Searching...
No Matches
base.hpp
Go to the documentation of this file.
1
14#ifndef IFW_FND_DEFS_BASE_HPP_H_
15#define IFW_FND_DEFS_BASE_HPP_H_
16
17#include <array>
18#include <iostream>
19#include <thread>
20#include <ctime>
21#include <iomanip>
22#include <variant>
23
24#include <fmt/format.h>
25
26
28
29
30namespace ifw::fnd {
31
37 inline double TimeSecs() {
38 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
39 return (std::chrono::duration_cast<std::chrono::nanoseconds>(
40 std::chrono::system_clock::now().time_since_epoch()).count() / 1e9);
41 }
42
49 inline void SleepSecs(const float sleep_time_in_seconds) {
50 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
51 std::chrono::duration<double> chrono_sleep_time(sleep_time_in_seconds);
52 std::this_thread::sleep_for(chrono_sleep_time);
53 }
54
62 inline std::string IsoTime(const double time_since_epoch, const uint8_t precision) {
63 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
64
65 // Split the time into whole seconds and fractional part
66 std::time_t whole_seconds = static_cast<std::time_t>(time_since_epoch);
67 double fractional_seconds = time_since_epoch - whole_seconds;
68
69 // Get the base time structure (only whole seconds)
70 std::tm* gmt_time = std::gmtime(&whole_seconds);
71 std::ostringstream oss;
72
73 // Format the date and time up to the seconds part
74 oss << std::put_time(gmt_time, "%Y-%m-%dT%H:%M:");
75
76 // Ensure the seconds part is always two digits
77 oss << std::setw(2) << std::setfill('0') << gmt_time->tm_sec;
78
79 // If no precision is required, return the base ISO time
80 if (precision == 0) {
81 return oss.str();
82 }
83
84 // Format the fractional seconds with the specified precision
85 std::string fractional_str = fmt::format("{:.{}}", fractional_seconds, precision);
86
87 // Skip the "0." part from the fractional part and append the rest
88 oss << fractional_str.substr(1);
89
90 return oss.str();
91 }
92
99 inline std::string IsoTimeNow(const int8_t precision) {
100 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
101 return IsoTime(TimeSecs(), precision);
102 }
103
105 inline std::string ThreadName() {
106 std::array<char, 32> thread_name{};
107 pthread_getname_np(pthread_self(), thread_name.data(), 32);
108 return std::string(thread_name.data());
109 }
110
111 // template <typename... Ts>
112 // std::string VariantToString(const std::variant<Ts...>& var) {
113 // return std::visit([](const auto& v) { return fmt::format("{}", v); }, var);
114 // }
115
116}
117
118// TODO: Replace this with std::source_location + inline functions when C++23 used.
120#define IFWLOC (ifw::fnd::IsoTimeNow(6) + ":" + std::string(__FILE__) + ":" +\
121 std::to_string(__LINE__) + ":" + std::string(__FUNCTION__) + ":" + ifw::fnd::ThreadName())
122
123// TODO: Future implementation from C++20 (or C++23):
124// #include <source_location>
125
127// inline std::string IfwLoc(const std::source_location& location =
128// std::source_location::current()) {
129// auto thr_name = ifw::fnd::ThreadName();
130// if (thr_name != "") {
131// return std::string(location.file_name()) + ":" +
132// std::to_string(location.line()) + ":" +
133// std::string(location.function_name()) + ":" +
134// thr_name;
135// } else {
136// return std::string(location.file_name()) + ":" +
137// std::to_string(location.line()) + ":" +
138// std::string(location.function_name());
139// }
140// }
141
142namespace ifw::fnd {
143
144 // template <typename... Ts>
145 // std::string VariantToString(const std::variant<Ts...>& var) {
146 // return std::visit([](const auto& v) { return fmt::format("{}", v); }, var);
147 // }
148
149 template <typename... Ts>
150 std::string VariantToString(const std::variant<Ts...>& var) {
151 try {
152 return std::visit([](const auto& v) { return fmt::format("{}", v); }, var);
153 } catch (...) {
154 return fmt::format("{}: Invalid value", IFWLOC);
155 }
156 }
157
158}
159
160#endif
#define IFWLOC
Macro generating a location identifier: "<file>:<line>:<function>:<thread>".
Definition base.hpp:120
This source file contains definitions of the types and constants to handle data types.
Definition defs.cpp:16
std::string VariantToString(const std::variant< Ts... > &var)
Definition base.hpp:150
std::string ThreadName()
Return name of current thread.
Definition base.hpp:105
std::string IsoTimeNow(const int8_t precision)
Definition base.hpp:99
std::string IsoTime(const double time_since_epoch, const uint8_t precision)
Definition base.hpp:62
log4cplus::Logger & Logger()
Definition defs.cpp:18
double TimeSecs()
Definition base.hpp:37
void SleepSecs(const float sleep_time_in_seconds)
Definition base.hpp:49