LCOV - code coverage report
Current view: top level - src - util.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 20 95.0 %
Date: 2025-09-09 12:09:29 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #ifndef UTIL_H_
       2             : #define UTIL_H_
       3             : 
       4             : #include <climits>
       5             : #include <string>
       6             : #include <vector>
       7             : #include <unistd.h>
       8             : #include <iostream>
       9             : #include <chrono>
      10             : #include <iomanip>
      11             : #include <sstream>
      12             : 
      13             : namespace Util {
      14             : 
      15             : /**
      16             :  * @brief split a string according to the passed separator.
      17             :  * @param text input string to split.
      18             :  * @param sep separator character.
      19             :  * @return vector of sub-strings.
      20             :  */
      21          24 : inline std::vector<std::string> split(const std::string &text, char sep) {
      22          24 :     std::vector<std::string> tokens;
      23          24 :     std::size_t start = 0, end = 0;
      24          59 :     while ((end = text.find(sep, start)) != std::string::npos) {
      25          70 :         tokens.push_back(text.substr(start, end - start));
      26          35 :         start = end + 1;
      27             :     }
      28          48 :     tokens.push_back(text.substr(start));
      29          24 :     return tokens;
      30           0 : }
      31             : 
      32             : 
      33             : /**
      34             :  * @brief Get local hostname.
      35             :  */
      36          29 : inline std::string get_full_hostname() {
      37          29 :     char hostname[HOST_NAME_MAX];
      38          29 :     int rc = ::gethostname(hostname, HOST_NAME_MAX);
      39          29 :     return std::string(rc < 0 ? "" : hostname);
      40             : }
      41             : 
      42             : 
      43             : inline std::string get_hostname() {
      44             :     return split(get_full_hostname(), '.')[0];
      45             : }
      46             : 
      47             : 
      48             : /**
      49             :  * @brief  Generate a UTC ISO8601-formatted timestamp
      50             :  * @return timestamp as std::string
      51             :  */
      52         200 : inline std::string ISO8601TimeUTC() {
      53         200 :     auto now = std::chrono::system_clock::now();
      54         200 :     auto itt = std::chrono::system_clock::to_time_t(now);
      55         200 :     std::ostringstream ss;
      56         200 :     ss << std::put_time(gmtime(&itt), "%FT%TZ");
      57         400 :     return ss.str();
      58         200 : }
      59             : 
      60             : } // namespace Util
      61             : 
      62             : #endif /* UTIL_H_ */

Generated by: LCOV version 1.0