Add zip files with past logs

This commit is contained in:
Lion Kortlepel 2021-09-30 23:24:23 +02:00
parent 33ebfa82f0
commit e948edca8d
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
7 changed files with 109 additions and 50 deletions

3
.gitmodules vendored
View File

@ -19,3 +19,6 @@
[submodule "deps/sol2"] [submodule "deps/sol2"]
path = deps/sol2 path = deps/sol2
url = https://github.com/ThePhD/sol2 url = https://github.com/ThePhD/sol2
[submodule "deps/libzip"]
path = deps/libzip
url = https://github.com/nih-at/libzip

View File

@ -30,6 +30,13 @@ endif()
set(SENTRY_BACKEND breakpad) set(SENTRY_BACKEND breakpad)
add_subdirectory("deps/sentry-native") add_subdirectory("deps/sentry-native")
set(BUILD_SHARED_LIBS OFF)
find_package(libzip)
if (NOT LIBZIP_FOUND)
add_subdirectory("deps/libzip")
find_package(libzip REQUIRED)
endif()
message(STATUS "Setting compiler flags") message(STATUS "Setting compiler flags")
if (WIN32) if (WIN32)
@ -63,8 +70,6 @@ message(STATUS "Adding local source dependencies")
# this has to happen before -DDEBUG since it wont compile properly with -DDEBUG # this has to happen before -DDEBUG since it wont compile properly with -DDEBUG
add_subdirectory(deps) add_subdirectory(deps)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
@ -93,7 +98,7 @@ add_executable(BeamMP-Server
include/SignalHandling.h src/SignalHandling.cpp) include/SignalHandling.h src/SignalHandling.cpp)
target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}") target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}")
include_directories(BeamMP-Server PUBLIC ${Boost_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/include) include_directories(BeamMP-Server PUBLIC ${libzip_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(BeamMP-Server PUBLIC target_include_directories(BeamMP-Server PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/include"
@ -113,7 +118,7 @@ message(STATUS "Looking for SSL")
find_package(OpenSSL REQUIRED) find_package(OpenSSL REQUIRED)
target_link_libraries(BeamMP-Server sol2::sol2 ${LUA_LIBRARIES}) target_link_libraries(BeamMP-Server sol2::sol2 ${LUA_LIBRARIES} libzip::zip)
message(STATUS "CURL IS ${CURL_LIBRARIES}") message(STATUS "CURL IS ${CURL_LIBRARIES}")
if (UNIX) if (UNIX)

1
deps/libzip vendored Submodule

@ -0,0 +1 @@
Subproject commit 76df02f86b9746e139fd9fc934a70e3a21bbc557

View File

@ -11,6 +11,7 @@ extern TSentry Sentry;
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <sstream> #include <sstream>
#include <zlib.h>
#include "Compat.h" #include "Compat.h"
@ -175,8 +176,56 @@ void RegisterThread(const std::string& str);
void LogChatMessage(const std::string& name, int id, const std::string& msg); void LogChatMessage(const std::string& name, int id, const std::string& msg);
#define Biggest 30000 #define Biggest 30000
std::string Comp(std::string Data);
std::string DeComp(std::string Compressed); template <typename T>
inline T Comp(const T& Data) {
std::array<char, Biggest> C {};
// obsolete
C.fill(0);
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in = (uInt)Data.size();
defstream.next_in = (Bytef*)&Data[0];
defstream.avail_out = Biggest;
defstream.next_out = reinterpret_cast<Bytef*>(C.data());
deflateInit(&defstream, Z_BEST_COMPRESSION);
deflate(&defstream, Z_SYNC_FLUSH);
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);
size_t TotalOut = defstream.total_out;
T Ret;
Ret.resize(TotalOut);
std::fill(Ret.begin(), Ret.end(), 0);
std::copy_n(C.begin(), TotalOut, Ret.begin());
return Ret;
}
template <typename T>
inline T DeComp(const T& Compressed) {
std::array<char, Biggest> C {};
// not needed
C.fill(0);
z_stream infstream;
infstream.zalloc = Z_NULL;
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
infstream.avail_in = Biggest;
infstream.next_in = (Bytef*)(&Compressed[0]);
infstream.avail_out = Biggest;
infstream.next_out = (Bytef*)(C.data());
inflateInit(&infstream);
inflate(&infstream, Z_SYNC_FLUSH);
inflate(&infstream, Z_FINISH);
inflateEnd(&infstream);
size_t TotalOut = infstream.total_out;
T Ret;
Ret.resize(TotalOut);
std::fill(Ret.begin(), Ret.end(), 0);
std::copy_n(C.begin(), TotalOut, Ret.begin());
return Ret;
}
std::string GetPlatformAgnosticErrorString(); std::string GetPlatformAgnosticErrorString();
#define S_DSN SU_RAW #define S_DSN SU_RAW

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "commandline.h"
#include "Cryptography.h" #include "Cryptography.h"
#include "commandline.h"
#include <atomic> #include <atomic>
#include <fstream> #include <fstream>
@ -14,6 +14,7 @@ public:
void Write(const std::string& str); void Write(const std::string& str);
void WriteRaw(const std::string& str); void WriteRaw(const std::string& str);
void InitializeLuaConsole(TLuaEngine& Engine); void InitializeLuaConsole(TLuaEngine& Engine);
void BackupOldLog();
private: private:
Commandline mCommandline; Commandline mCommandline;

View File

@ -8,7 +8,6 @@
#include <regex> #include <regex>
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include <zlib.h>
#include "CustomAssert.h" #include "CustomAssert.h"
#include "Http.h" #include "Http.h"
@ -97,49 +96,9 @@ void Application::CheckForUpdates() {
} }
} }
std::string Comp(std::string Data) {
std::array<char, Biggest> C {};
// obsolete
C.fill(0);
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in = (uInt)Data.length();
defstream.next_in = (Bytef*)&Data[0];
defstream.avail_out = Biggest;
defstream.next_out = reinterpret_cast<Bytef*>(C.data());
deflateInit(&defstream, Z_BEST_COMPRESSION);
deflate(&defstream, Z_SYNC_FLUSH);
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);
size_t TO = defstream.total_out;
std::string Ret(TO, 0);
std::copy_n(C.begin(), TO, Ret.begin());
return Ret;
}
std::string DeComp(std::string Compressed) {
std::array<char, Biggest> C {};
// not needed
C.fill(0);
z_stream infstream;
infstream.zalloc = Z_NULL;
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
infstream.avail_in = Biggest;
infstream.next_in = (Bytef*)(&Compressed[0]);
infstream.avail_out = Biggest;
infstream.next_out = (Bytef*)(C.data());
inflateInit(&infstream);
inflate(&infstream, Z_SYNC_FLUSH);
inflate(&infstream, Z_FINISH);
inflateEnd(&infstream);
size_t TO = infstream.total_out;
std::string Ret(TO, 0);
std::copy_n(C.begin(), TO, Ret.begin());
return Ret;
}
// thread name stuff // thread name stuff

View File

@ -6,6 +6,7 @@
#include <ctime> #include <ctime>
#include <sstream> #include <sstream>
#include <zip.h>
std::string GetDate() { std::string GetDate() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
@ -41,10 +42,50 @@ std::string GetDate() {
return date.str(); return date.str();
} }
void TConsole::BackupOldLog() {
fs::path Path = "Server.log";
if (fs::exists(Path)) {
int err = 0;
zip* z = zip_open("ServerLogs.zip", ZIP_CREATE, &err);
if (!z) {
std::cerr << GetPlatformAgnosticErrorString() << std::endl;
return;
}
FILE* File = std::fopen(Path.string().c_str(), "r");
if (!File) {
std::cerr << GetPlatformAgnosticErrorString() << std::endl;
return;
}
std::vector<uint8_t> Buffer;
Buffer.resize(fs::file_size(Path));
std::fread(Buffer.data(), 1, Buffer.size(), File);
std::fclose(File);
auto s = zip_source_buffer(z, Buffer.data(), Buffer.size(), 0);
auto TimePoint = fs::last_write_time(Path);
auto Secs = TimePoint.time_since_epoch().count();
auto MyTimeT = std::time(&Secs);
std::string NewName = Path.stem().string();
NewName += "_";
std::string Time;
Time.resize(32);
size_t n = strftime(Time.data(), Time.size(), "%F_%H.%M.%S", localtime(&MyTimeT));
Time.resize(n);
NewName += Time;
NewName += ".log";
zip_file_add(z, NewName.c_str(), s, 0);
zip_close(z);
}
}
TConsole::TConsole() { TConsole::TConsole() {
mCommandline.enable_history(); mCommandline.enable_history();
mCommandline.set_history_limit(20); mCommandline.set_history_limit(20);
mCommandline.set_prompt("> "); mCommandline.set_prompt("> ");
BackupOldLog();
bool success = mCommandline.enable_write_to_file("Server.log"); bool success = mCommandline.enable_write_to_file("Server.log");
if (!success) { if (!success) {
beammp_error("unable to open file for writing: \"Server.log\""); beammp_error("unable to open file for writing: \"Server.log\"");