2021-08-28 03:13:01 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2021-09-19 04:13:10 +02:00
|
|
|
#include <vector>
|
2021-08-28 03:13:01 +02:00
|
|
|
|
|
|
|
#ifndef _LOG_
|
|
|
|
#define _LOG_
|
|
|
|
|
|
|
|
class Log {
|
|
|
|
public:
|
2021-09-19 04:13:10 +02:00
|
|
|
Log() {}
|
|
|
|
Log(unsigned int s) { size = s; }
|
|
|
|
~Log() {}
|
|
|
|
template <typename T> Log &operator<<(const T &msg) {
|
|
|
|
std::stringstream current_string;
|
|
|
|
current_string << msg;
|
|
|
|
os << msg;
|
2021-08-28 03:13:01 +02:00
|
|
|
|
2021-09-19 04:13:10 +02:00
|
|
|
if (current_string.str().back() == '\n') {
|
|
|
|
std::cout << "LOG: " << os.str();
|
|
|
|
if (current >= size || full) {
|
|
|
|
if (ctr >= size) {
|
|
|
|
ctr = 0;
|
2021-08-28 03:13:01 +02:00
|
|
|
}
|
2021-09-19 04:13:10 +02:00
|
|
|
buffer[ctr] = os.str();
|
|
|
|
full = true;
|
|
|
|
} else {
|
|
|
|
buffer.push_back(os.str());
|
2021-08-28 03:13:01 +02:00
|
|
|
}
|
2021-09-19 04:13:10 +02:00
|
|
|
ctr++;
|
|
|
|
current++;
|
|
|
|
os.str("");
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
std::vector<std::string> getLogs() {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
int iterator_limit = std::min(size, current);
|
|
|
|
for (int i = iterator_limit - 1; i >= 0; i--) {
|
|
|
|
result.emplace_back(buffer[(current - 1 - i) % size]);
|
2021-08-28 03:13:01 +02:00
|
|
|
}
|
2021-09-19 04:13:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
2021-08-28 03:13:01 +02:00
|
|
|
|
|
|
|
private:
|
2021-09-19 04:13:10 +02:00
|
|
|
unsigned int current = 0;
|
|
|
|
unsigned int ctr = 0;
|
|
|
|
bool full = false;
|
|
|
|
std::vector<std::string> buffer;
|
|
|
|
std::stringstream os;
|
|
|
|
unsigned int size = 255;
|
2021-08-28 03:13:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|