88 lines
2.4 KiB
C++
88 lines
2.4 KiB
C++
/**
|
|
* \file utils.hpp
|
|
* \brief Utilities header
|
|
*
|
|
* \author
|
|
* Christos Choutouridis AEM:8997
|
|
* <cchoutou@ece.auth.gr>
|
|
*/
|
|
#ifndef UTILS_HPP_
|
|
#define UTILS_HPP_
|
|
|
|
#include <iostream>
|
|
#include <chrono>
|
|
#include <unistd.h>
|
|
|
|
#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<typename T>
|
|
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<std::chrono::microseconds>(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<microseconds>(t).count() < 10000)
|
|
std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast<microseconds>(t).count()) << " [usec]\n";
|
|
else if (std::chrono::duration_cast<milliseconds>(t).count() < 10000)
|
|
std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast<milliseconds>(t).count()) << " [msec]\n";
|
|
else
|
|
std::cout << "[Timing]: " << what << ": " << std::to_string(std::chrono::duration_cast<seconds>(t).count()) << " [sec]\n";
|
|
}
|
|
}
|
|
private:
|
|
Tpoint start_;
|
|
Tpoint stop_;
|
|
};
|
|
|
|
|
|
#endif /* UTILS_HPP_ */
|