-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.hpp
38 lines (31 loc) · 973 Bytes
/
Logger.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// Created by Carlo Ronconi on 15/07/23.
//
#ifndef DRONE_DELIVERY_LOGGER_HPP
#define DRONE_DELIVERY_LOGGER_HPP
#include <iostream>
#include <map>
#include <iomanip>
class Logger {
std::ostream& stream;
long lastLogTime;
long logTime;
public:
explicit Logger(std::ostream &stream, long logTime = 1.0) :
stream(stream), lastLogTime(time(nullptr)), logTime(logTime) {}
template<class T>
void log(const std::map<std::string, T>& info, std::function<std::string(T)> writer, int width = 20) {
long currTime = time(nullptr);
if (currTime - lastLogTime < logTime) return;
lastLogTime = currTime;
for (const auto& element : info) {
stream << element.first << std::setw(width);
}
stream << "\n";
for (const auto& element : info) {
stream << writer(element.second) << std::setw(width);
}
stream << "\n";
}
};
#endif //DRONE_DELIVERY_LOGGER_HPP