From e948edca8d383ace0706d4160f7be82de9ccb976 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 30 Sep 2021 23:24:23 +0200 Subject: [PATCH] Add zip files with past logs --- .gitmodules | 3 +++ CMakeLists.txt | 13 ++++++++---- deps/libzip | 1 + include/Common.h | 53 ++++++++++++++++++++++++++++++++++++++++++++-- include/TConsole.h | 3 ++- src/Common.cpp | 45 ++------------------------------------- src/TConsole.cpp | 41 +++++++++++++++++++++++++++++++++++ 7 files changed, 109 insertions(+), 50 deletions(-) create mode 160000 deps/libzip diff --git a/.gitmodules b/.gitmodules index 083c21f..1822884 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "deps/sol2"] path = deps/sol2 url = https://github.com/ThePhD/sol2 +[submodule "deps/libzip"] + path = deps/libzip + url = https://github.com/nih-at/libzip diff --git a/CMakeLists.txt b/CMakeLists.txt index a8be07c..a197127 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,13 @@ endif() set(SENTRY_BACKEND breakpad) 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") 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 add_subdirectory(deps) - - set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG") @@ -93,7 +98,7 @@ add_executable(BeamMP-Server include/SignalHandling.h src/SignalHandling.cpp) 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 "${CMAKE_CURRENT_SOURCE_DIR}/include" @@ -113,7 +118,7 @@ message(STATUS "Looking for SSL") 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}") if (UNIX) diff --git a/deps/libzip b/deps/libzip new file mode 160000 index 0000000..76df02f --- /dev/null +++ b/deps/libzip @@ -0,0 +1 @@ +Subproject commit 76df02f86b9746e139fd9fc934a70e3a21bbc557 diff --git a/include/Common.h b/include/Common.h index f8436d6..c6b8b1e 100644 --- a/include/Common.h +++ b/include/Common.h @@ -11,6 +11,7 @@ extern TSentry Sentry; #include #include #include +#include #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); #define Biggest 30000 -std::string Comp(std::string Data); -std::string DeComp(std::string Compressed); + +template +inline T Comp(const T& Data) { + std::array 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(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 +inline T DeComp(const T& Compressed) { + std::array 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(); #define S_DSN SU_RAW diff --git a/include/TConsole.h b/include/TConsole.h index 8e6ad31..4f9cad6 100644 --- a/include/TConsole.h +++ b/include/TConsole.h @@ -1,7 +1,7 @@ #pragma once -#include "commandline.h" #include "Cryptography.h" +#include "commandline.h" #include #include @@ -14,6 +14,7 @@ public: void Write(const std::string& str); void WriteRaw(const std::string& str); void InitializeLuaConsole(TLuaEngine& Engine); + void BackupOldLog(); private: Commandline mCommandline; diff --git a/src/Common.cpp b/src/Common.cpp index f1bcfab..7bfae72 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include "CustomAssert.h" #include "Http.h" @@ -97,49 +96,9 @@ void Application::CheckForUpdates() { } } -std::string Comp(std::string Data) { - std::array 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(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 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 diff --git a/src/TConsole.cpp b/src/TConsole.cpp index 43834da..7905839 100644 --- a/src/TConsole.cpp +++ b/src/TConsole.cpp @@ -6,6 +6,7 @@ #include #include +#include std::string GetDate() { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); @@ -41,10 +42,50 @@ std::string GetDate() { 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 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() { mCommandline.enable_history(); mCommandline.set_history_limit(20); mCommandline.set_prompt("> "); + BackupOldLog(); bool success = mCommandline.enable_write_to_file("Server.log"); if (!success) { beammp_error("unable to open file for writing: \"Server.log\"");