-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
30 lines (29 loc) · 854 Bytes
/
Copy pathlogger.cpp
File metadata and controls
30 lines (29 loc) · 854 Bytes
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
#include "logger.hpp"
#include <fstream>
#include <chrono>
#include <filesystem>
#include <cstdlib>
namespace sflk {
static std::string log_path() {
const char* home = getenv("HOME");
std::filesystem::path p = (home?home:".");
p /= ".sflk";
std::filesystem::create_directories(p);
p /= "sflk.log";
return p.string();
}
static std::string ts() {
using namespace std::chrono;
auto now = system_clock::now();
auto t = system_clock::to_time_t(now);
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t));
return std::string(buf);
}
void log_info(const std::string& msg) {
std::ofstream(log_path(), std::ios::app) << ts() << " [INFO] " << msg << "\n";
}
void log_error(const std::string& msg) {
std::ofstream(log_path(), std::ios::app) << ts() << " [ERROR] " << msg << "\n";
}
}