logmeow/log.cpp

41 lines
948 B
C++
Raw Normal View History

2023-01-05 10:27:14 +00:00
#include <ctime>
#include <string>
#include <vector>
#include <exception>
#include "log.h"
std::string log_entries;
2023-01-16 08:56:07 +00:00
std::string format_log(std::string entry, time_t time) {
size_t newline_pos;
2023-01-05 10:27:14 +00:00
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, ' ');
}
2023-01-05 10:27:14 +00:00
2023-01-16 08:56:07 +00:00
return std::string(1, '[') + time_as_str + "] " + std::move(entry);
}
std::string format_log(std::string entry) {
return format_log(std::move(entry), time(nullptr));
}
void log_raw(std::string line) {
printf("%s\n", line.c_str());
if (!log_entries.empty()) {
log_entries += '\n';
}
2023-01-16 08:56:07 +00:00
log_entries += std::move(line);
}
void log(std::string entry, time_t time) {
log_raw(format_log(std::move(entry), time));
2023-01-05 10:27:14 +00:00
}
2023-01-07 09:15:21 +00:00
void log(std::string entry) {
2023-01-16 08:56:07 +00:00
log_raw(format_log(std::move(entry)));
2023-01-07 09:15:21 +00:00
}