From 5e0136d737ebaa6f2547c20c72dc24edb8e3d3d4 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Fri, 15 May 2020 12:47:27 +0100 Subject: [PATCH] Add concept of verboseDebug --- code/Common/Assimp.cpp | 4 ++-- code/Common/DefaultLogger.cpp | 29 +++++++++++++++++++++++++++-- include/assimp/DefaultLogger.hpp | 11 +++++++---- include/assimp/Logger.hpp | 32 ++++++++++++++++++++++++++++++-- include/assimp/NullLogger.hpp | 7 ++++++- 5 files changed, 72 insertions(+), 11 deletions(-) diff --git a/code/Common/Assimp.cpp b/code/Common/Assimp.cpp index 67f1c3af2..86ddb92c1 100644 --- a/code/Common/Assimp.cpp +++ b/code/Common/Assimp.cpp @@ -392,7 +392,7 @@ ASSIMP_API aiReturn aiDetachLogStream(const aiLogStream *stream) { if (it == gActiveLogStreams.end()) { return AI_FAILURE; } - DefaultLogger::get()->detatchStream(it->second); + DefaultLogger::get()->detachStream( it->second ); delete it->second; gActiveLogStreams.erase(it); @@ -416,7 +416,7 @@ ASSIMP_API void aiDetachAllLogStreams(void) { } for (LogStreamMap::iterator it = gActiveLogStreams.begin(); it != gActiveLogStreams.end(); ++it) { - logger->detatchStream(it->second); + logger->detachStream( it->second ); delete it->second; } gActiveLogStreams.clear(); diff --git a/code/Common/DefaultLogger.cpp b/code/Common/DefaultLogger.cpp index adb9ba160..6b2f9252b 100644 --- a/code/Common/DefaultLogger.cpp +++ b/code/Common/DefaultLogger.cpp @@ -177,6 +177,18 @@ void Logger::debug(const char* message) { return OnDebug(message); } +// ---------------------------------------------------------------------------------- +void Logger::verboseDebug(const char *message) { + + // SECURITY FIX: otherwise it's easy to produce overruns since + // sometimes importers will include data from the input file + // (i.e. node names) in their messages. + if (strlen(message) > MAX_LOG_MESSAGE_LENGTH) { + return; + } + return OnVerboseDebug(message); +} + // ---------------------------------------------------------------------------------- void Logger::info(const char* message) { @@ -251,7 +263,7 @@ void DefaultLogger::kill() { // ---------------------------------------------------------------------------------- // Debug message void DefaultLogger::OnDebug( const char* message ) { - if ( m_Severity == Logger::NORMAL ) { + if ( m_Severity < Logger::DEBUG ) { return; } @@ -262,6 +274,19 @@ void DefaultLogger::OnDebug( const char* message ) { WriteToStreams( msg, Logger::Debugging ); } +// Verbose debug message +void DefaultLogger::OnVerboseDebug(const char *message) { + if (m_Severity < Logger::VERBOSE) { + return; + } + + static const size_t Size = MAX_LOG_MESSAGE_LENGTH + 16; + char msg[Size]; + ai_snprintf(msg, Size, "Debug, T%u: %s", GetThreadID(), message); + + WriteToStreams(msg, Logger::Debugging); +} + // ---------------------------------------------------------------------------------- // Logs an info void DefaultLogger::OnInfo( const char* message ){ @@ -320,7 +345,7 @@ bool DefaultLogger::attachStream( LogStream *pStream, unsigned int severity ) { // ---------------------------------------------------------------------------------- // Detach a stream -bool DefaultLogger::detatchStream( LogStream *pStream, unsigned int severity ) { +bool DefaultLogger::detachStream( LogStream *pStream, unsigned int severity ) { if ( nullptr == pStream ) { return false; } diff --git a/include/assimp/DefaultLogger.hpp b/include/assimp/DefaultLogger.hpp index 789072a7c..229ace87b 100644 --- a/include/assimp/DefaultLogger.hpp +++ b/include/assimp/DefaultLogger.hpp @@ -80,7 +80,7 @@ public: /** @brief Creates a logging instance. * @param name Name for log file. Only valid in combination * with the aiDefaultLogStream_FILE flag. - * @param severity Log severity, VERBOSE turns on debug messages + * @param severity Log severity, DEBUG turns on debug messages and VERBOSE turns on all messages. * @param defStreams Default log streams to be attached. Any bitwise * combination of the aiDefaultLogStream enumerated values. * If #aiDefaultLogStream_FILE is specified but an empty string is @@ -127,8 +127,8 @@ public: unsigned int severity); // ---------------------------------------------------------------------- - /** @copydoc Logger::detatchStream */ - bool detatchStream(LogStream *pStream, + /** @copydoc Logger::detachStream */ + bool detachStream(LogStream *pStream, unsigned int severity); private: @@ -141,9 +141,12 @@ private: /** @briefDestructor */ ~DefaultLogger(); - /** @brief Logs debug infos, only been written when severity level VERBOSE is set */ + /** @brief Logs debug infos, only been written when severity level DEBUG is set */ void OnDebug(const char* message); + /** @brief Logs debug infos, only been written when severity level VERBOSE is set */ + void OnVerboseDebug(const char *message); + /** @brief Logs an info message */ void OnInfo(const char* message); diff --git a/include/assimp/Logger.hpp b/include/assimp/Logger.hpp index f32c3edc2..1e5a61eee 100644 --- a/include/assimp/Logger.hpp +++ b/include/assimp/Logger.hpp @@ -74,7 +74,8 @@ public: */ enum LogSeverity { NORMAL, //!< Normal granularity of logging - VERBOSE //!< Debug infos will be logged, too + DEBUG, //!< Debug messages will be logged, but not verbose debug messages. + VERBOSE //!< All messages will be logged }; // ---------------------------------------------------------------------- @@ -103,6 +104,12 @@ public: void debug(const char* message); void debug(const std::string &message); + // ---------------------------------------------------------------------- + /** @brief Writes a debug message + * @param message Debug message*/ + void verboseDebug(const char *message); + void verboseDebug(const std::string &message); + // ---------------------------------------------------------------------- /** @brief Writes a info message * @param message Info message*/ @@ -154,7 +161,7 @@ public: * if the result is 0 the stream is detached from the Logger and * the caller retakes the possession of the stream. * @return true if the stream has been detached, false otherwise.*/ - virtual bool detatchStream(LogStream *pStream, + virtual bool detachStream(LogStream *pStream, unsigned int severity = Debugging | Err | Warn | Info) = 0; protected: @@ -178,6 +185,16 @@ protected: */ virtual void OnDebug(const char* message)= 0; + // ---------------------------------------------------------------------- + /** + * @brief Called as a request to write a specific verbose debug message + * @param message Debug message. Never longer than + * MAX_LOG_MESSAGE_LENGTH characters (excluding the '0'). + * @note The message string is only valid until the scope of + * the function is left. + */ + virtual void OnVerboseDebug(const char *message) = 0; + // ---------------------------------------------------------------------- /** * @brief Called as a request to write a specific info message @@ -255,6 +272,11 @@ void Logger::debug(const std::string &message) { return debug(message.c_str()); } +// ---------------------------------------------------------------------------------- +inline void Logger::verboseDebug(const std::string &message) { + return verboseDebug(message.c_str()); +} + // ---------------------------------------------------------------------------------- inline void Logger::error(const std::string &message) { @@ -285,6 +307,9 @@ void Logger::info(const std::string &message) { #define ASSIMP_LOG_DEBUG_F(string, ...) \ Assimp::DefaultLogger::get()->debug((Assimp::Formatter::format(string), __VA_ARGS__)) +#define ASSIMP_LOG_VERBOSE_DEBUG_F(string, ...) \ + Assimp::DefaultLogger::get()->verboseDebug((Assimp::Formatter::format(string), __VA_ARGS__)) + #define ASSIMP_LOG_INFO_F(string, ...) \ Assimp::DefaultLogger::get()->info((Assimp::Formatter::format(string), __VA_ARGS__)) @@ -297,6 +322,9 @@ void Logger::info(const std::string &message) { #define ASSIMP_LOG_DEBUG(string) \ Assimp::DefaultLogger::get()->debug(string) +#define ASSIMP_LOG_VERBOSE_DEBUG(string) \ + Assimp::DefaultLogger::get()->verboseDebug(string) + #define ASSIMP_LOG_INFO(string) \ Assimp::DefaultLogger::get()->info(string) diff --git a/include/assimp/NullLogger.hpp b/include/assimp/NullLogger.hpp index 7effca83a..e7a01ed78 100644 --- a/include/assimp/NullLogger.hpp +++ b/include/assimp/NullLogger.hpp @@ -66,6 +66,11 @@ public: (void)message; //this avoids compiler warnings } + /** @brief Logs a verbose debug message */ + void OnVerboseDebug(const char *message) { + (void)message; //this avoids compiler warnings + } + /** @brief Logs an info message */ void OnInfo(const char* message) { (void)message; //this avoids compiler warnings @@ -88,7 +93,7 @@ public: } /** @brief Detach a still attached stream from logger */ - bool detatchStream(LogStream *pStream, unsigned int severity) { + bool detachStream(LogStream *pStream, unsigned int severity) { (void)pStream; (void)severity; //this avoids compiler warnings return false; }