diff options
-rw-r--r-- | reporthandler.cpp | 126 | ||||
-rw-r--r-- | reporthandler.h | 103 |
2 files changed, 118 insertions, 111 deletions
diff --git a/reporthandler.cpp b/reporthandler.cpp index 18a9a9c1e..a311b53cd 100644 --- a/reporthandler.cpp +++ b/reporthandler.cpp @@ -23,7 +23,10 @@ #include "reporthandler.h" #include "typesystem.h" +#include <QtCore/QSet> #include <cstring> +#include <cstdarg> +#include <cstdio> #ifndef NOCOLOR #define COLOR_END "\033[0m" @@ -37,16 +40,108 @@ #define COLOR_GREEN "" #endif +class ProgressAnimation +{ + public: + ProgressAnimation() + { + anim_data = "|/-\\"; + anim_frame = anim_data; + std::strcpy(anim_string, "[ ]"); + m_current = m_max = 0; + } + const char* toString() + { + step(); + return anim_string; + } + + void reset(int max) + { + m_current = 1; + m_max = max; + } + + int current() const + { + return m_current; + } + int max() const + { + return m_max; + } + + private: + const char* anim_data; + char anim_string[4]; + const char* anim_frame; + int m_max; + int m_current; + + void step() + { + if (!*(++anim_frame)) + anim_frame = anim_data; + anim_string[1] = *anim_frame; + m_current++; + } +}; + + +static bool m_silent = false; +static int m_warningCount = 0; +static int m_suppressedCount = 0; +static QString m_context; +static ReportHandler::DebugLevel m_debugLevel = ReportHandler::NoDebug; +static QSet<QString> m_reportedWarnings; +static char m_progressBuffer[1024] = {0}; +static ProgressAnimation m_anim; + +static void printProgress() +{ + std::printf("%s", m_progressBuffer); + std::fflush(stdout); +} + +ReportHandler::DebugLevel ReportHandler::debugLevel() +{ + return m_debugLevel; +} + +void ReportHandler::setDebugLevel(ReportHandler::DebugLevel level) +{ + m_debugLevel = level; +} + +void ReportHandler::setContext(const QString& context) +{ + m_context = context; +} + +int ReportHandler::suppressedCount() +{ + return m_suppressedCount; +} + +int ReportHandler::warningCount() +{ + return m_warningCount; +} + +void ReportHandler::setProgressReference(int max) +{ + m_anim.reset(max); +} -bool ReportHandler::m_silent = false; -int ReportHandler::m_warningCount = 0; -int ReportHandler::m_suppressedCount = 0; -QString ReportHandler::m_context; -ReportHandler::DebugLevel ReportHandler::m_debugLevel = NoDebug; -QSet<QString> ReportHandler::m_reportedWarnings; -char ReportHandler::m_progressBuffer[1024] = {0}; -ProgressAnimation ReportHandler::m_anim; +bool ReportHandler::isSilent() +{ + return m_silent; +} +void ReportHandler::setSilent(bool silent) +{ + m_silent = silent; +} void ReportHandler::warning(const QString &text) { @@ -61,7 +156,7 @@ void ReportHandler::warning(const QString &text) if (db && db->isSuppressedWarning(text)) { ++m_suppressedCount; } else if (!m_reportedWarnings.contains(text)) { - puts(qPrintable(warningText)); + std::puts(qPrintable(warningText)); printProgress(); ++m_warningCount; @@ -74,27 +169,20 @@ void ReportHandler::progress(const QString& str, ...) if (m_silent) return; QString msg = QString("\033[1K\r" COLOR_WHITE "%1 (%2/%3) " COLOR_END).arg(m_anim.toString()).arg(m_anim.current()).arg(m_anim.max()) + str; - va_list argp; + std::va_list argp; va_start(argp, str); - vsnprintf(m_progressBuffer, sizeof(m_progressBuffer), msg.toLocal8Bit().constData(), argp); + std::vsnprintf(m_progressBuffer, sizeof(m_progressBuffer), msg.toLocal8Bit().constData(), argp); va_end(argp); printProgress(); } -void ReportHandler::printProgress() -{ - printf("%s", m_progressBuffer); - fflush(stdout); -} - - void ReportHandler::debug(DebugLevel level, const QString &text) { if (m_debugLevel == NoDebug) return; if (level <= m_debugLevel) { - printf("\r" COLOR_GREEN "DEBUG" COLOR_END " :: %-70s\n", qPrintable(text)); + std::printf("\r" COLOR_GREEN "DEBUG" COLOR_END " :: %-70s\n", qPrintable(text)); printProgress(); } } diff --git a/reporthandler.h b/reporthandler.h index 1f70046ce..7c8b1c57d 100644 --- a/reporthandler.h +++ b/reporthandler.h @@ -24,95 +24,33 @@ #ifndef REPORTHANDLER_H #define REPORTHANDLER_H -#include <QtCore/QString> -#include <QtCore/QSet> -#include <cstring> +class QString; #include "apiextractormacros.h" -class ProgressAnimation -{ -public: - ProgressAnimation() - { - anim_data = "|/-\\"; - anim_frame = anim_data; - std::strcpy(anim_string, "[ ]"); - m_current = m_max = 0; - } - const char* toString() - { - step(); - return anim_string; - } - template<typename T> - void setCollection(T collection) - { - m_current = 1; - m_max = collection.count(); - } - - int current() const - { - return m_current; - } - int max() const - { - return m_max; - } - -private: - const char* anim_data; - char anim_string[4]; - const char* anim_frame; - int m_max; - int m_current; - - void step() - { - if (!*(++anim_frame)) - anim_frame = anim_data; - anim_string[1] = *anim_frame; - m_current++; - } -}; - class APIEXTRACTOR_API ReportHandler { public: enum DebugLevel { NoDebug, SparseDebug, MediumDebug, FullDebug }; - static void setContext(const QString &context) - { - m_context = context; - } + static void setContext(const QString &context); - static DebugLevel debugLevel() - { - return m_debugLevel; - } - static void setDebugLevel(DebugLevel level) - { - m_debugLevel = level; - } + static DebugLevel debugLevel(); + static void setDebugLevel(DebugLevel level); - static int warningCount() - { - return m_warningCount; - } + static int warningCount(); - static int suppressedCount() - { - return m_suppressedCount; - } + static int suppressedCount(); static void warning(const QString &str); template <typename T> static void setProgressReference(T collection) { - m_anim.setCollection(collection); + setProgressReference(collection.count()); } + static void setProgressReference(int max); + static void progress(const QString &str, ...); static void debugSparse(const QString &str) @@ -129,27 +67,8 @@ public: } static void debug(DebugLevel level, const QString &str); - static bool isSilent() - { - return m_silent; - } - static void setSilent(bool silent) - { - m_silent = silent; - } - -private: - static bool m_silent; - static int m_warningCount; - static int m_suppressedCount; - static DebugLevel m_debugLevel; - static QString m_context; - static QSet<QString> m_reportedWarnings; - - static ProgressAnimation m_anim; - static char m_progressBuffer[1024]; - - static void printProgress(); + static bool isSilent(); + static void setSilent(bool silent); }; #endif // REPORTHANDLER_H |