17 #ifndef LIB_WINSS_LOG_LOG_HPP_ 18 #define LIB_WINSS_LOG_LOG_HPP_ 27 #include "easylogging/easylogging++.hpp" 28 #include "../filesystem_interface.hpp" 29 #include "../not_owning_ptr.hpp" 30 #include "../path_mutex.hpp" 31 #include "../utils.hpp" 35 namespace fs = std::experimental::filesystem;
55 template<
typename TMutex>
75 auto now = std::chrono::system_clock::now();
76 std::ostringstream os;
77 os << kArchivePrefix << now.time_since_epoch().count() <<
".u";
79 fs::path archive = settings.
log_dir / os.str();
83 return writer->
Open(current);
93 VLOG(3) <<
"Cleaning old archives";
96 if (files.size() <= settings.
number) {
97 VLOG(3) <<
"Skipping archive clean up";
101 std::vector<winss::LogArchiveFile> archives;
102 for (
const fs::path&
file : files) {
103 std::string s =
file.filename().string();
105 if (std::regex_search(s, match, pattern) && match.size() > 1) {
106 std::string m = match[1].str();
107 unsigned __int64
time = std::strtoull(m.c_str(),
nullptr, 10);
109 archives.push_back(std::move(archive));
113 std::sort(archives.begin(), archives.end(),
116 return f1.
time < f2.time;
119 __int64 to_delete = archives.size() - settings.
number;
120 if (to_delete <= 0) {
122 <<
"Not cleaning up any archives because there are only " 127 VLOG(3) <<
"Removing " << to_delete <<
" archives";
128 for (
size_t i = 0; to_delete; ++i, --to_delete) {
134 static const int kMutexTaken = 100;
135 static const int kFatalExitCode = 111;
136 static constexpr
const char kCurrentLog[8] =
"current";
137 static constexpr
const char kArchivePrefix[2] =
"@";
138 static constexpr
const char kMutexName[4] =
"log";
150 settings(settings), mutex(settings.log_dir, kMutexName),
151 pattern(
"^" + std::string(kArchivePrefix) +
"([\\d]+)\\.\\w$") {
152 current = settings.
log_dir / fs::path(kCurrentLog);
172 <<
" does not exist";
173 return kFatalExitCode;
180 if (!writer->
Open(current)) {
181 return kFatalExitCode;
184 unsigned int size = 0;
185 while (!reader->
IsEOF()) {
186 std::streamoff pos = writer->
GetPos();
190 return kFatalExitCode;
198 std::string line = reader->
GetLine();
201 auto now = std::chrono::system_clock::now();
224 #endif // LIB_WINSS_LOG_LOG_HPP_ static std::string ConvertToISOString(const std::chrono::system_clock::time_point &time_point)
Convert the time to a ISO string.
Definition: utils.cpp:104
virtual bool Open(fs::path log_path)
Opens the file stream at the given location.
Definition: log_stream_wrapper.cpp:37
winss::NotOwningPtr< winss::LogStreamWriter > writer
Log output.
Definition: log.hpp:59
bool Rotate() const
Rotates the current log file.
Definition: log.hpp:72
virtual void Close()
Closes the currently open stream.
Definition: log_stream_wrapper.cpp:61
virtual void Write(const std::string &line)
Writes the given string to the log stream.
Definition: log_stream_wrapper.cpp:49
void CleanArchives() const
Deletes old archive files.
Definition: log.hpp:92
Definition: case_ignore.hpp:23
winss::NotOwningPtr< winss::LogStreamReader > reader
Log input.
Definition: log.hpp:58
const winss::LogSettings & settings
Logger settings.
Definition: log.hpp:60
fs::path file
The archive file name.
Definition: log.hpp:45
virtual bool IsEOF() const
Gets the end of file state.
Definition: log_stream_wrapper.cpp:25
fs::path current
Current log file.
Definition: log.hpp:61
unsigned __int64 time
The time the archive was taken.
Definition: log.hpp:46
Settings for the logger.
Definition: log_settings.hpp:28
TMutex mutex
Log dir global mutex.
Definition: log.hpp:62
LogTmpl< winss::PathMutex > Log
Concrete log implementation.
Definition: log.hpp:221
fs::path log_dir
The log directory.
Definition: log_settings.hpp:32
virtual std::streamoff GetPos()
Gets the current position in the stream.
Definition: log_stream_wrapper.cpp:57
The logger template.
Definition: log.hpp:56
std::regex pattern
Log file pattern when rotating files.
Definition: log.hpp:63
#define FILESYSTEM
Definition: filesystem_interface.hpp:26
unsigned int file_size
The max file size in bytes.
Definition: log_settings.hpp:30
LogTmpl(winss::NotOwningPtr< winss::LogStreamReader > reader, winss::NotOwningPtr< winss::LogStreamWriter > writer, const winss::LogSettings &settings)
Log template constructor.
Definition: log.hpp:147
virtual std::string GetLine()
Blocks for the next log line.
Definition: log_stream_wrapper.cpp:29
A container for pointers where the lifetime should be owned by the caller.
Definition: not_owning_ptr.hpp:33
unsigned int number
The number of archives to keep.
Definition: log_settings.hpp:29
An archived log file.
Definition: log.hpp:44
virtual void WriteLine()
Writes a line terminator to the stream.
Definition: log_stream_wrapper.cpp:53
bool timestamp
Prepend a ISO 8601 timestamp.
Definition: log_settings.hpp:31
int Start()
Starts the logging implementation.
Definition: log.hpp:167