16 #include <tinyformat.h> 21 namespace fs = std::filesystem;
23 extern std::atomic<bool> g_debug;
24 extern std::atomic<bool> g_log_timestamps;
30 std::string ToString(
const T& t)
32 std::ostringstream oss;
33 oss.imbue(std::locale::classic());
44 [[nodiscard]] std::vector<std::string> StringSplit(
const std::string& s,
const std::string& delim);
52 [[nodiscard]] std::string TrimString(
const std::string& str,
const std::string& pattern =
" \f\n\r\t\v");
60 [[nodiscard]] std::string StripQuotes(
const std::string& str);
72 constexpr
char ToLower(
char c);
83 std::string ToLower(
const std::string& str);
89 int64_t GetUnixEpochTime();
96 std::string FormatISO8601DateTime(int64_t time);
103 bool IsValidTimestamp(
const int64_t& timestamp);
105 template <
typename... Args>
112 static inline std::string LogPrintStr(
const char* fmt,
const Args&... args)
117 if (g_log_timestamps.load(std::memory_order_relaxed)) {
118 log_msg = FormatISO8601DateTime(GetUnixEpochTime()) +
" ";
122 log_msg += tfm::format(fmt, args...);
125 log_msg +=
"Error \"" + std::string(fmterr.what()) +
"\" while formatting log message: " + fmt;
133 template <
typename... Args>
139 void normal_log(
const char* fmt,
const Args&... args)
141 std::cout << LogPrintStr(fmt, args...);
144 template <
typename... Args>
150 void debug_log(
const char* fmt,
const Args&... args)
152 if (g_debug.load()) {
153 normal_log(fmt, args...);
157 template <
typename... Args>
163 void error_log(
const char* fmt,
const Args&... args)
165 std::string error_fmt =
"ERROR: ";
168 std::cerr << LogPrintStr(error_fmt.c_str(), args...);
171 [[nodiscard]]
int ParseStringToInt(
const std::string& str);
173 [[nodiscard]] int64_t ParseStringtoInt64(
const std::string& str);
181 std::vector<fs::path> FindDirEntriesWithWildcard(
const fs::path& directory,
const std::string& wildcard);
188 std::optional<std::string> GetEnvVariable(
const std::string& var_name);
199 const char* what()
const noexcept
override {
200 return m_message.c_str();
204 std::string m_message;
214 const std::filesystem::path& path()
const {
return m_path; }
217 std::filesystem::path m_path;
227 typedef std::variant<bool, int, std::string, fs::path> config_variant;
253 config_variant
GetArg(
const std::string& arg);
263 std::string
GetArgString(
const std::string& arg,
const std::string& default_value)
const;
269 std::multimap<std::string, config_variant>
m_config;
333 EventMessage(std::string timestamp_str, std::string event_type_str);
std::string ToString()
Returns the string message format of the EventMessage object. This is meant to go on the pipe...
Definition: util.cpp:339
std::string GetArgString(const std::string &arg, const std::string &default_value) const
Private version of GetArg that operates on m_config_in and also selects the provided default value if...
Definition: util.cpp:261
std::multimap< std::string, config_variant > m_config
Holds the processed parameter-values, which are strongly typed and in a config_variant union...
Definition: util.h:269
EventType
The EventType enum defines event types for the EventMessage class. Currently there is only one event ...
Definition: util.h:308
File system related exceptions.
Definition: util.h:208
The Config class is a singleton that stores program config read from the config file, with applied defaults if the config file cannot be read, or a config parameter is not in the config file.
Definition: util.h:233
std::mutex mtx_config
This is the mutex member that provides lock control for the config object. This is used to ensure the...
Definition: util.h:283
virtual void ProcessArgs()=0
Private helper method used by ReadAndUpdateConfig. Note this is pure virtual. It must be implemented ...
The EventMessage class is a small class that encapsulates the "event message", which is a message sen...
Definition: util.h:301
Threading Related Exceptions.
Definition: util.h:221
bool IsValid()
Validates the EventMessage object.
Definition: util.cpp:334
std::multimap< std::string, std::string > m_config_in
Holds the raw parsed parameter-values from the config file.
Definition: util.h:288
void ReadAndUpdateConfig(const fs::path &config_file)
Reads and parses the config file provided by the argument and populates m_config_in, then calls private method ProcessArgs() to populate m_config.
Definition: util.cpp:200
std::string EventTypeToString()
Converts m_event_type member variable in the EventMessage object to a string.
Definition: util.cpp:304
EventMessage()
Constructs an "empty" EventMessage with timestamp of 0 and EventType of UNKNOWN.
Definition: util.cpp:272
The EventDetectException class is a customized exception handling class for the event_detect applicat...
Definition: util.h:193
config_variant GetArg(const std::string &arg)
Provides the config_variant type value of the config parameter (argument).
Definition: util.cpp:248
EventType EventTypeStringToEnum(const std::string &event_type_str)
This converts the event type string to the proper enum value. It is the converse of EventTypeToString...
Definition: util.cpp:277
Config()
Constructor.
Definition: util.cpp:197