Repository navigation
Expand file tree
/
Copy pathlogger.cpp
More file actions
62 lines (49 loc) · 1.35 KB
/
Copy pathlogger.cpp
File metadata and controls
62 lines (49 loc) · 1.35 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "logger.hpp"
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <sstream>
namespace dynamic_file_binder {
namespace {
std::filesystem::path GetLogPath()
{
const std::filesystem::path logDir = std::filesystem::current_path() / ".sccache";
std::error_code errorCode;
std::filesystem::create_directories(logDir, errorCode);
return logDir / "dynamic_file_binder.log";
}
std::string GetTimestamp()
{
const auto now = std::chrono::system_clock::now();
const std::time_t timeValue = std::chrono::system_clock::to_time_t(now);
std::tm tmValue{};
#if defined(_WIN32)
localtime_s(&tmValue, &timeValue);
#else
localtime_r(&timeValue, &tmValue);
#endif
std::ostringstream stream;
stream << std::put_time(&tmValue, "%Y-%m-%d %H:%M:%S");
return stream.str();
}
void WriteLog(const char* level, const std::string& message)
{
const std::filesystem::path logPath = GetLogPath();
std::ofstream logFile(logPath, std::ios::app);
if (!logFile.is_open()) {
return;
}
logFile << '[' << GetTimestamp() << "] [" << level << "] " << message << '\n';
}
} // namespace
void LogInfo(const std::string& message)
{
WriteLog("INFO", message);
}
void LogError(const std::string& message)
{
WriteLog("ERROR", message);
}
} // namespace dynamic_file_binder