aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/logCompression.cpp
blob: d9e89fdf39438426f2d1aacbc2086199382fc3fd (plain)
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
63
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);
		}
	}
}