galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
FileSink.cpp
Go to the documentation of this file.
1
7
8#include <filesystem>
9
10#include <zip.h>
11
12#include "FileSink.hpp"
13
14namespace galaxy
15{
16 FileSink::FileSink(std::string_view file)
17 {
18 const auto filepath = std::filesystem::path(file);
19 const auto parent_path = filepath.parent_path();
20 const auto zip_path = parent_path / "old_logs.zip";
21
22 struct zip_t* zip = nullptr;
23 if (std::filesystem::exists(zip_path))
24 {
25 zip = zip_open(zip_path.string().c_str(), ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
26 }
27 else
28 {
29 zip = zip_open(zip_path.string().c_str(), ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
30 }
31
32 for (const auto& path : std::filesystem::recursive_directory_iterator(parent_path))
33 {
34 const auto entry_path = std::filesystem::path(path);
35 if (std::filesystem::is_regular_file(entry_path) && entry_path.extension() == ".log" && entry_path.stem() != filepath.stem())
36 {
37 zip_entry_open(zip, entry_path.filename().string().c_str());
38 zip_entry_fwrite(zip, entry_path.string().c_str());
39 zip_entry_close(zip);
40
41 std::filesystem::remove(entry_path);
42 }
43 }
44
45 zip_close(zip);
46 m_file_stream.open(filepath, std::ofstream::out);
47 }
48
50 {
51 m_file_stream.close();
52 }
53
54 void FileSink::sink(const LogMessage& message)
55 {
56 m_file_stream << std::format("[{0}] [{2}, Ln {3}] {1}: {4}\n{5}\n", message.time, message.level, message.file, message.line, message.message, message.trace);
57 }
58} // namespace galaxy
void sink(const LogMessage &message) override
Sink the message.
Definition FileSink.cpp:54
FileSink(std::string_view file)
Constructor.
Definition FileSink.cpp:16
std::ofstream m_file_stream
File stream to write to.
Definition FileSink.hpp:43
virtual ~FileSink() noexcept
Destructor.
Definition FileSink.cpp:49
Timer.hpp galaxy.
Definition Timer.cpp:18
Parts of a log message to be passed to sinks.
LogLevel level
Level of message.
std::string time
Timestamp of message.
std::string trace
Stack trace.
std::string file
File message occured in.
std::string message
The actual message to record.
unsigned int line
Line the message occured on.