aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/logCompression.cpp
diff options
context:
space:
mode:
authorLegonzaur <34353603+Legonzaur@users.noreply.github.com>2022-02-16 00:06:31 +0100
committerGitHub <noreply@github.com>2022-02-15 20:06:31 -0300
commit0faa9cfb3a9015ca723f012ca84609f602c7c03c (patch)
tree42c3f32924131346031000356743273b6f303bf1 /NorthstarDedicatedTest/logCompression.cpp
parentfb363f16204478758a5ff62290c2bae159d7507c (diff)
downloadNorthstarLauncher-0faa9cfb3a9015ca723f012ca84609f602c7c03c.tar.gz
NorthstarLauncher-0faa9cfb3a9015ca723f012ca84609f602c7c03c.zip
Compress previous logs and dump files on launch (#73)
* added zlib + gzip Co-authored-by: Nathan TIEN YOU <nathan.tienyou@viacesi.fr>
Diffstat (limited to 'NorthstarDedicatedTest/logCompression.cpp')
-rw-r--r--NorthstarDedicatedTest/logCompression.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/NorthstarDedicatedTest/logCompression.cpp b/NorthstarDedicatedTest/logCompression.cpp
new file mode 100644
index 00000000..d9e89fdf
--- /dev/null
+++ b/NorthstarDedicatedTest/logCompression.cpp
@@ -0,0 +1,64 @@
+#include "pch.h"
+#include "logCompression.h"
+#include <fstream>
+#include <string>
+#include <iostream>
+#include <filesystem>
+#include "configurables.h"
+#include "zlib/gzip/compress.hpp"
+#include "zlib/gzip/config.hpp"
+#include <zlib.h>
+
+#define CHUNK 16384
+
+namespace fs = std::filesystem;
+using namespace std;
+
+bool compressFile(const fs::path path)
+{
+ // read log file
+ ofstream output;
+ string filename(path.string());
+ cout << "Compressing : '" + filename + "'" << endl;
+ ifstream input(filename, ios_base::binary);
+ if (!input.is_open())
+ {
+ cerr << "Could not open : '" + filename + "'" << endl;
+ return false;
+ }
+ string log_data((istreambuf_iterator<char>(input.rdbuf())), istreambuf_iterator<char>());
+ input.close();
+ // compress log file
+ string compressed_data = gzip::compress(log_data.data(), log_data.size());
+ // write log file gzip
+ output.open(filename + ".gz");
+ if (!output.is_open())
+ {
+ cerr << "Could not write : '" + filename + "'" << endl;
+ return false;
+ }
+ output << compressed_data;
+ output.close();
+ // delete log file
+ remove(path);
+ if (std::ifstream(path))
+ {
+ cerr << "Error deleting : '%s'" + filename + "'" << endl;
+ return false;
+ }
+ return true;
+}
+
+void CompressLogFiles()
+{
+ string path = GetNorthstarPrefix() + "/logs";
+ for (const auto& entry : fs::directory_iterator(path))
+ {
+ fs::path link = entry.path();
+ string extension = link.extension().string();
+ if (extension == ".txt" || extension == ".dmp")
+ {
+ compressFile(link);
+ }
+ }
+} \ No newline at end of file