logmeow/log.cpp

40 lines
917 B
C++

#include <ctime>
#include <string>
#include <vector>
#include <exception>
#include <system_error>
#include "log.h"
std::string log_entries;
void log(std::string entry, time_t time) {
size_t newline_pos;
char time_as_str[128] = {0};
strftime(time_as_str, 127 * sizeof(char), "%c", localtime(&time));
while ((newline_pos = entry.find('\n')) != std::string::npos) {
entry.replace(newline_pos, 1, 1, ' ');
}
if (!log_entries.empty()) {
log_entries += '\n';
}
log_entries += '[';
log_entries += time_as_str;
log_entries += "] ";
log_entries += std::move(entry);
}
void log(std::string entry) {
log(std::move(entry), time(nullptr));
}
void log(std::string action, const std::exception& e, time_t time) {
log(action + ": " + e.what(), time);
}
void log(std::string action, const std::exception& e) {
log(std::move(action), e, time(nullptr));
}