From 96fdaebb5c8c8902c98c1fb43e755cf90fc15198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Tue, 18 Aug 2015 02:25:24 +0200 Subject: GH-926 implement log cleaning functionality Also adds gzip compressed log support --- logic/BaseInstance.h | 7 +++- logic/CMakeLists.txt | 28 +++++++++++++- logic/GZip.cpp | 71 +++++++++++++++++++++++++++++++++++ logic/GZip.h | 9 +++++ logic/NullInstance.h | 4 ++ logic/RecursiveFileSystemWatcher.cpp | 12 ++++-- logic/RecursiveFileSystemWatcher.h | 24 +++++++++--- logic/minecraft/MinecraftInstance.cpp | 10 +++++ logic/minecraft/MinecraftInstance.h | 2 + logic/pathmatcher/FSTreeMatcher.h | 19 ++++++++++ logic/pathmatcher/IPathMatcher.h | 12 ++++++ logic/pathmatcher/MultiMatcher.h | 31 +++++++++++++++ logic/pathmatcher/RegexpMatcher.h | 29 ++++++++++++++ 13 files changed, 246 insertions(+), 12 deletions(-) create mode 100644 logic/GZip.cpp create mode 100644 logic/GZip.h create mode 100644 logic/pathmatcher/FSTreeMatcher.h create mode 100644 logic/pathmatcher/IPathMatcher.h create mode 100644 logic/pathmatcher/MultiMatcher.h create mode 100644 logic/pathmatcher/RegexpMatcher.h (limited to 'logic') diff --git a/logic/BaseInstance.h b/logic/BaseInstance.h index 9c282bbc..bcd1e0da 100644 --- a/logic/BaseInstance.h +++ b/logic/BaseInstance.h @@ -26,6 +26,7 @@ #include "BaseVersionList.h" #include "auth/MojangAccount.h" #include "launch/MessageLevel.h" +#include "pathmatcher/IPathMatcher.h" class QDir; class Task; @@ -158,12 +159,16 @@ public: */ virtual std::shared_ptr createJarModdingTask() = 0; - /*! * Create envrironment variables for running the instance */ virtual QProcessEnvironment createEnvironment() = 0; + /*! + * Returns a matcher that can maps relative paths within the instance to whether they are 'log files' + */ + virtual IPathMatcher::Ptr getLogFileMatcher() = 0; + /*! * does any necessary cleanups after the instance finishes. also runs before\ * TODO: turn into a task that can run asynchronously diff --git a/logic/CMakeLists.txt b/logic/CMakeLists.txt index fe445c05..fce89ac8 100644 --- a/logic/CMakeLists.txt +++ b/logic/CMakeLists.txt @@ -59,6 +59,16 @@ set(LOGIC_SOURCES resources/ResourceProxyModel.h resources/ResourceProxyModel.cpp + # Path matchers + pathmatcher/FSTreeMatcher.h + pathmatcher/IPathMatcher.h + pathmatcher/MultiMatcher.h + pathmatcher/RegexpMatcher.h + + # Compression support + GZip.h + GZip.cpp + # network stuffs net/NetAction.h net/MD5EtagDownload.h @@ -293,12 +303,28 @@ set(LOGIC_SOURCES ) ################################ COMPILE ################################ +if(WIN32) + add_definitions(-DZ_PREFIX) +endif() + # Add common library add_library(MultiMC_logic STATIC ${LOGIC_SOURCES}) +# Use system zlib on unix and Qt ZLIB on Windows +if(UNIX) + find_package(ZLIB REQUIRED) +else(UNIX) + get_filename_component(ZLIB_FOUND_DIR "${Qt5Core_DIR}/../../../include/QtZlib" ABSOLUTE) + set(ZLIB_INCLUDE_DIRS ${ZLIB_FOUND_DIR} CACHE PATH "Path to ZLIB headers of Qt") + set(ZLIB_LIBRARIES "") + if(NOT EXISTS "${ZLIB_INCLUDE_DIRS}/zlib.h") + message("Please specify a valid zlib include dir") + endif(NOT EXISTS "${ZLIB_INCLUDE_DIRS}/zlib.h") +endif(UNIX) + # Link target_link_libraries(MultiMC_logic xz-embedded unpack200 iconfix libUtil LogicalGui ${QUAZIP_LIBRARIES} Qt5::Core Qt5::Xml Qt5::Widgets Qt5::Network Qt5::Concurrent - ${MultiMC_LINK_ADDITIONAL_LIBS}) + ${ZLIB_LIBRARIES} ${MultiMC_LINK_ADDITIONAL_LIBS}) add_dependencies(MultiMC_logic QuaZIP) diff --git a/logic/GZip.cpp b/logic/GZip.cpp new file mode 100644 index 00000000..96d58ea8 --- /dev/null +++ b/logic/GZip.cpp @@ -0,0 +1,71 @@ +#include "GZip.h" +#include +#include + +// HACK: workaround for terrible macro crap on Windows +int wrap_inflate (z_streamp strm, int flush) +{ + return inflate(strm, flush); +} + +#ifdef inflate + #undef inflate +#endif + +bool GZip::inflate(const QByteArray &compressedBytes, QByteArray &uncompressedBytes) +{ + if (compressedBytes.size() == 0) + { + uncompressedBytes = compressedBytes; + return true; + } + + unsigned uncompLength = compressedBytes.size(); + unsigned half_length = compressedBytes.size() / 2; + uncompressedBytes.clear(); + uncompressedBytes.resize(uncompLength); + + z_stream strm; + strm.next_in = (Bytef *)compressedBytes.data(); + strm.avail_in = compressedBytes.size(); + strm.total_out = 0; + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + + bool done = false; + + if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK) + { + return false; + } + + while (!done) + { + // If our output buffer is too small + if (strm.total_out >= uncompLength) + { + uncompressedBytes.resize(uncompLength + half_length); + uncompLength += half_length; + } + + strm.next_out = (Bytef *)(uncompressedBytes.data() + strm.total_out); + strm.avail_out = uncompLength - strm.total_out; + + // Inflate another chunk. + int err = wrap_inflate(&strm, Z_SYNC_FLUSH); + if (err == Z_STREAM_END) + done = true; + else if (err != Z_OK) + { + break; + } + } + + if (inflateEnd(&strm) != Z_OK) + { + return false; + } + + uncompressedBytes.resize(strm.total_out); + return true; +} diff --git a/logic/GZip.h b/logic/GZip.h new file mode 100644 index 00000000..cd9f53d8 --- /dev/null +++ b/logic/GZip.h @@ -0,0 +1,9 @@ +#pragma once +#include + +class GZip +{ +public: + static bool inflate(const QByteArray &compressedBytes, QByteArray &uncompressedBytes); +}; + diff --git a/logic/NullInstance.h b/logic/NullInstance.h index aba3e484..9ddbc2b2 100644 --- a/logic/NullInstance.h +++ b/logic/NullInstance.h @@ -70,4 +70,8 @@ public: { return QMap(); } + virtual IPathMatcher::Ptr getLogFileMatcher() + { + return nullptr; + } }; diff --git a/logic/RecursiveFileSystemWatcher.cpp b/logic/RecursiveFileSystemWatcher.cpp index 39985699..1253870b 100644 --- a/logic/RecursiveFileSystemWatcher.cpp +++ b/logic/RecursiveFileSystemWatcher.cpp @@ -4,7 +4,7 @@ #include RecursiveFileSystemWatcher::RecursiveFileSystemWatcher(QObject *parent) - : QObject(parent), m_exp(".*"), m_watcher(new QFileSystemWatcher(this)) + : QObject(parent), m_watcher(new QFileSystemWatcher(this)) { connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &RecursiveFileSystemWatcher::fileChange); @@ -82,16 +82,20 @@ void RecursiveFileSystemWatcher::addFilesToWatcherRecursive(const QDir &dir) QStringList RecursiveFileSystemWatcher::scanRecursive(const QDir &directory) { QStringList ret; - QRegularExpression exp(m_exp); + if(!m_matcher) + { + return {}; + } for (const QString &dir : directory.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { ret.append(scanRecursive(directory.absoluteFilePath(dir))); } for (const QString &file : directory.entryList(QDir::Files)) { - if (exp.match(file).hasMatch()) + auto relPath = m_root.relativeFilePath(directory.absoluteFilePath(file)); + if (m_matcher->matches(relPath)) { - ret.append(m_root.relativeFilePath(directory.absoluteFilePath(file))); + ret.append(relPath); } } return ret; diff --git a/logic/RecursiveFileSystemWatcher.h b/logic/RecursiveFileSystemWatcher.h index 339172bb..a2a9e7e3 100644 --- a/logic/RecursiveFileSystemWatcher.h +++ b/logic/RecursiveFileSystemWatcher.h @@ -2,6 +2,7 @@ #include #include +#include "pathmatcher/IPathMatcher.h" class RecursiveFileSystemWatcher : public QObject { @@ -10,16 +11,27 @@ public: RecursiveFileSystemWatcher(QObject *parent); void setRootDir(const QDir &root); - QDir rootDir() const { return m_root; } + QDir rootDir() const + { + return m_root; + } // WARNING: setting this to true may be bad for performance void setWatchFiles(const bool watchFiles); - bool watchFiles() const { return m_watchFiles; } + bool watchFiles() const + { + return m_watchFiles; + } - void setFileExpression(const QString &exp) { m_exp = exp; } - QString fileExpression() const { return m_exp; } + void setMatcher(IPathMatcher::Ptr matcher) + { + m_matcher = matcher; + } - QStringList files() const { return m_files; } + QStringList files() const + { + return m_files; + } signals: void filesChanged(); @@ -33,7 +45,7 @@ private: QDir m_root; bool m_watchFiles = false; bool m_isEnabled = false; - QString m_exp; + IPathMatcher::Ptr m_matcher; QFileSystemWatcher *m_watcher; diff --git a/logic/minecraft/MinecraftInstance.cpp b/logic/minecraft/MinecraftInstance.cpp index d576ef4f..50962452 100644 --- a/logic/minecraft/MinecraftInstance.cpp +++ b/logic/minecraft/MinecraftInstance.cpp @@ -5,6 +5,8 @@ #include "Env.h" #include "minecraft/MinecraftVersionList.h" #include +#include +#include #define IBUS "@im=ibus" @@ -277,4 +279,12 @@ MessageLevel::Enum MinecraftInstance::guessLevel(const QString &line, MessageLev return level; } +IPathMatcher::Ptr MinecraftInstance::getLogFileMatcher() +{ + auto combined = std::make_shared(); + combined->add(std::make_shared(".*\\.log(\\.[0-9]*)?(\\.gz)?$")); + combined->add(std::make_shared("crash-.*\\.txt")); + return combined; +} + #include "MinecraftInstance.moc" diff --git a/logic/minecraft/MinecraftInstance.h b/logic/minecraft/MinecraftInstance.h index 14a0f097..a09e44db 100644 --- a/logic/minecraft/MinecraftInstance.h +++ b/logic/minecraft/MinecraftInstance.h @@ -44,6 +44,8 @@ public: /// guess log level from a line of minecraft log virtual MessageLevel::Enum guessLevel(const QString &line, MessageLevel::Enum level); + virtual IPathMatcher::Ptr getLogFileMatcher() override; + protected: QMap createCensorFilterFromSession(AuthSessionPtr session); }; diff --git a/logic/pathmatcher/FSTreeMatcher.h b/logic/pathmatcher/FSTreeMatcher.h new file mode 100644 index 00000000..f99e45c9 --- /dev/null +++ b/logic/pathmatcher/FSTreeMatcher.h @@ -0,0 +1,19 @@ +#include "IPathMatcher.h" +#include +#include + +class FSTreeMatcher : public IPathMatcher +{ +public: + virtual ~FSTreeMatcher() {}; + FSTreeMatcher(SeparatorPrefixTree<'/'> & tree) : m_fsTree(tree) + { + } + + virtual bool matches(const QString &string) override + { + return m_fsTree.covers(string); + } + + SeparatorPrefixTree<'/'> & m_fsTree; +}; diff --git a/logic/pathmatcher/IPathMatcher.h b/logic/pathmatcher/IPathMatcher.h new file mode 100644 index 00000000..806a750a --- /dev/null +++ b/logic/pathmatcher/IPathMatcher.h @@ -0,0 +1,12 @@ +#pragma once +#include + +class IPathMatcher +{ +public: + typedef std::shared_ptr Ptr; + +public: + virtual ~IPathMatcher(){}; + virtual bool matches(const QString &string) = 0; +}; diff --git a/logic/pathmatcher/MultiMatcher.h b/logic/pathmatcher/MultiMatcher.h new file mode 100644 index 00000000..e018967c --- /dev/null +++ b/logic/pathmatcher/MultiMatcher.h @@ -0,0 +1,31 @@ +#include "IPathMatcher.h" +#include +#include + +class MultiMatcher : public IPathMatcher +{ +public: + virtual ~MultiMatcher() {}; + MultiMatcher() + { + } + MultiMatcher &add(Ptr add) + { + m_matchers.append(add); + return *this; + } + + virtual bool matches(const QString &string) override + { + for(auto iter: m_matchers) + { + if(iter->matches(string)) + { + return true; + } + } + return false; + } + + QList m_matchers; +}; diff --git a/logic/pathmatcher/RegexpMatcher.h b/logic/pathmatcher/RegexpMatcher.h new file mode 100644 index 00000000..f3cf90b1 --- /dev/null +++ b/logic/pathmatcher/RegexpMatcher.h @@ -0,0 +1,29 @@ +#include "IPathMatcher.h" +#include + +class RegexpMatcher : public IPathMatcher +{ +public: + virtual ~RegexpMatcher() {}; + RegexpMatcher(QString regexp) + { + m_regexp.setPattern(regexp); + m_onlyFilenamePart = !regexp.contains('/'); + } + + virtual bool matches(const QString &string) override + { + if(m_onlyFilenamePart) + { + auto slash = string.lastIndexOf('/'); + if(slash != -1) + { + auto part = string.mid(slash + 1); + return m_regexp.match(part).hasMatch(); + } + } + return m_regexp.match(string).hasMatch(); + } + QRegularExpression m_regexp; + bool m_onlyFilenamePart = false; +}; -- cgit v1.2.3