diff --git a/code/Common/Logger.cpp b/code/Common/Logger.cpp index 369cfcc..3be7b48 100644 --- a/code/Common/Logger.cpp +++ b/code/Common/Logger.cpp @@ -30,40 +30,41 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include namespace cppcore { - -static void appendDomain(const String &domain, String &logMsg) { - if (!domain.isEmpty()) { - logMsg += '('; - logMsg += domain; - logMsg += ')'; - } -} - -static ::std::string stripFilename(const ::std::string &filename) { - if (filename.empty()) { - return filename; +namespace { + void appendDomain(const String &domain, String &logMsg) { + if (!domain.isEmpty()) { + logMsg += '('; + logMsg += domain; + logMsg += ')'; + } } - - ::std::string::size_type pos = filename.find_last_of("/"); - if (pos == ::std::string::npos) { - return filename; + + ::std::string stripFilename(const ::std::string &filename) { + if (filename.empty()) { + return filename; + } + + ::std::string::size_type pos = filename.find_last_of("/"); + if (pos == ::std::string::npos) { + return filename; + } + const ::std::string strippedName = filename.substr(pos + 1, filename.size() - pos - 1); + + return strippedName; } - const ::std::string strippedName = filename.substr(pos + 1, filename.size() - pos - 1); - - return strippedName; -} - -static void addTraceInfo(const String &file, int line, String &msg) { - if (Logger::getInstance()->getVerboseMode() == Logger::VerboseMode::Trace) { - msg += String(" (", 2); - std::string stripped = stripFilename(file.c_str()); - msg += String(stripped.c_str(), stripped.size()); - msg += String(", ", 2); - std::stringstream ss; - ss << line; - std::string lineno = ss.str(); - msg += String(lineno.c_str(), lineno.size()); - msg += ')'; + + void addTraceInfo(const String &file, int line, String &msg) { + if (Logger::getInstance()->getVerboseMode() == Logger::VerboseMode::Trace) { + msg += String(" (", 2); + std::string stripped = stripFilename(file.c_str()); + msg += String(stripped.c_str(), stripped.size()); + msg += String(", ", 2); + std::stringstream ss; + ss << line; + std::string lineno = ss.str(); + msg += String(lineno.c_str(), lineno.size()); + msg += ')'; + } } } @@ -82,7 +83,7 @@ bool AbstractLogStream::isActive() const { Logger *Logger::sLogger = nullptr; Logger *Logger::create() { - if (nullptr == sLogger) { + if (sLogger == nullptr) { sLogger = new Logger; } @@ -98,7 +99,7 @@ void Logger::set(Logger *logger) { } Logger *Logger::getInstance() { - if (nullptr == sLogger) { + if (sLogger == nullptr) { static_cast(create()); } @@ -189,8 +190,7 @@ void Logger::print(const String &msg, PrintMode mode) { } } - if (msg.size() > 8) { - if (msg[6] == '=' && msg[7] == '>') { + if (msg.size() > 8 && msg[6] == '=' && msg[7] == '>') { mIntention += 2; } } @@ -231,7 +231,7 @@ void Logger::registerLogStream(AbstractLogStream *pLogStream) { mLogStreams.add(pLogStream); } -void Logger::unregisterLogStream(AbstractLogStream *logStream) { +void Logger::unregisterLogStream(const AbstractLogStream *logStream) { if (nullptr == logStream) { return; } @@ -254,8 +254,8 @@ Logger::~Logger() { } } -String Logger::getDateTime() { - static const uint32_t Space = 2; +String Logger::getDateTime() const { + static constexpr uint32_t Space = 2; DateTime currentDateTime; std::stringstream stream; stream.fill('0'); diff --git a/include/cppcore/Common/Logger.h b/include/cppcore/Common/Logger.h index 8416077..94dd919 100644 --- a/include/cppcore/Common/Logger.h +++ b/include/cppcore/Common/Logger.h @@ -72,13 +72,11 @@ class DLL_CPPCORE_EXPORT AbstractLogStream { //------------------------------------------------------------------------------------------------- /// -/// @brief This class implements a simple logger. -/// +/// @brief This class implements a simple logger. /// The logger is implemented as a singleton. You can attach several log streams to it, which can -/// be used to log the messages to several output channels like a window or a log file or something -/// else. -/// The granularity of the logged messages can be controlled by the severity of the logger. The -/// supported modes are normal ( no debug and info messages ), verbose ( all messages will be +/// be used to log the messages to several output channels like a window or a log file or something +/// else. The granularity of the logged messages can be controlled by the severity of the logger. +/// The supported modes are normal ( no debug and info messages ), verbose ( all messages will be /// logged ) and debug ( the debug messages will be logged as well, be careful with this option ). //------------------------------------------------------------------------------------------------- class DLL_CPPCORE_EXPORT Logger final { @@ -87,8 +85,8 @@ class DLL_CPPCORE_EXPORT Logger final { enum class VerboseMode { Invalid = -1, ///< Invalid marker Normal, ///< Only warnings and errors will be logged. - Verbose, ///< Normal messages will be logged as well. - Debug, ///< All debug messages will be logged as well. + Verbose, ///< Normal messages will be logged as well. + Debug, ///< All debug messages will be logged as well. Trace, ///< Will enable the tracing. Count ///< Number of enums }; @@ -166,12 +164,12 @@ class DLL_CPPCORE_EXPORT Logger final { /// @brief Unregisters a registered log stream. /// @param[in] pLogStream A pointer showing to the log stream. - void unregisterLogStream(AbstractLogStream *pLogStream); + void unregisterLogStream(const AbstractLogStream *pLogStream); private: Logger(); ~Logger(); - String getDateTime(); + String getDateTime() const; private: // @brief The Standard log stream.