/** * \file utils.hpp * \brief Utilities header * * \author * Christos Choutouridis AEM:8997 * */ #ifndef UTILS_HPP_ #define UTILS_HPP_ #include #include #include #include "matrix.hpp" #include "config.h" /*! * A Logger for entire program. */ struct Log { struct Endl { } endl; //!< a tag object to to use it as a new line request. //! We provide logging via << operator template Log &operator<<(T &&t) { if (session.verbose) { if (line_) { std::cout << "[Log]: " << t; line_ = false; } else std::cout << t; } return *this; } // overload for special end line handling Log &operator<<(Endl e) { (void) e; if (session.verbose) { std::cout << '\n'; line_ = true; } return *this; } private: bool line_{true}; }; extern Log logger; /*! * A small timing utility based on chrono. */ struct Timing { using Tpoint = std::chrono::steady_clock::time_point; using microseconds = std::chrono::microseconds; using milliseconds = std::chrono::milliseconds; using seconds = std::chrono::seconds; //! tool to mark the starting point Tpoint start() noexcept { return start_ = std::chrono::steady_clock::now(); } //! tool to mark the ending point Tpoint stop() noexcept { return stop_ = std::chrono::steady_clock::now(); } auto dt() noexcept { return std::chrono::duration_cast(stop_ - start_).count(); } //! tool to print the time interval void print_dt(const char *what) noexcept { if (session.timing) { auto t = stop_ - start_; if (std::chrono::duration_cast(t).count() < 10000) std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast(t).count()) << " [usec]\n"; else if (std::chrono::duration_cast(t).count() < 10000) std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast(t).count()) << " [msec]\n"; else std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast(t).count()) << " [sec]\n"; } } private: Tpoint start_; Tpoint stop_; }; #endif /* UTILS_HPP_ */