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 namespace logging
17 {
18 FileSink::FileSink(std::string_view file)
19 {
20 const auto filepath = std::filesystem::path(file);
21 const auto parent_path = filepath.parent_path();
22 const auto zip_path = parent_path / "old_logs.zip";
23
24 struct zip_t* zip = nullptr;
25 if (std::filesystem::exists(zip_path))
26 {
27 zip = zip_open(zip_path.string().c_str(), ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
28 }
29 else
30 {
31 zip = zip_open(zip_path.string().c_str(), ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
32 }
33
34 for (const auto& path : std::filesystem::recursive_directory_iterator(parent_path))
35 {
36 const auto entry_path = std::filesystem::path(path);
37 if (std::filesystem::is_regular_file(entry_path) && entry_path.extension() == ".log" && entry_path.stem() != filepath.stem())
38 {
39 zip_entry_open(zip, entry_path.filename().string().c_str());
40 zip_entry_fwrite(zip, entry_path.string().c_str());
41 zip_entry_close(zip);
42
43 std::filesystem::remove(entry_path);
44 }
45 }
46
47 zip_close(zip);
48 m_file_stream.open(filepath, std::ofstream::out);
49 }
50
52 {
53 m_file_stream.close();
54 }
55
57 {
58 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);
59 }
60 } // namespace logging
61} // namespace galaxy
FileSink(std::string_view file)
Constructor.
Definition FileSink.cpp:18
virtual ~FileSink()
Destructor.
Definition FileSink.cpp:51
void sink_message(const LogMessage &message) override
Sink the message.
Definition FileSink.cpp:56
std::ofstream m_file_stream
File stream to write to.
Definition FileSink.hpp:45
Timer.hpp galaxy.
Definition Async.hpp:17
Parts of a log message to be passed to sinks.
std::string level
Level of message.
std::string time
Timestamp of message.
std::string message
The actual message to record.
std::string trace
Stack trace.
std::string line
Line the message occured on.
std::string file
File message occured in.