///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2017 Statoil ASA // // ResInsight is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. // // See the GNU General Public License at // for more details. // ///////////////////////////////////////////////////////////////////////////////// #pragma once #include #include #include #include #include #include enum class RILogLevel { RI_LL_DISABLED = 0, RI_LL_ERROR = 1, RI_LL_WARNING = 2, RI_LL_INFO = 3, RI_LL_DEBUG = 4 }; //================================================================================================== // // Logger interface for the application // //================================================================================================== class RiaLogger { public: virtual ~RiaLogger() {} virtual int level() const = 0; virtual void setLevel( int logLevel ) = 0; virtual void error( const char* message ) = 0; virtual void warning( const char* message ) = 0; virtual void info( const char* message ) = 0; virtual void debug( const char* message ) = 0; // Deliver messages a logger has accepted but not yet written out. A logger that has to hand messages from // a worker thread over to another thread can otherwise let them arrive after messages logged later. virtual void flushPendingMessages() {} }; //================================================================================================== // // // //================================================================================================== class RiaLogging { public: static std::vector loggerInstances(); static void appendLoggerInstance( std::unique_ptr loggerInstance ); static RILogLevel logLevelBasedOnPreferences(); static std::optional parseLogLevelString( const QString& logLevelString ); static void error( std::string_view message, std::string_view logKeyword = "" ); static void warning( std::string_view message, std::string_view logKeyword = "" ); static void info( std::string_view message, std::string_view logKeyword = "" ); static void debug( std::string_view message, std::string_view logKeyword = "" ); // Write out anything the loggers are holding, so messages logged from a worker thread appear before the // messages the waiting thread logs once the worker is done. static void flushPendingMessages(); static std::chrono::time_point currentTime(); static void logElapsedTime( std::string_view message, const std::chrono::time_point& startTime ); private: static void setLastMessage( std::string_view message ); static bool isSameMessage( std::string_view message ); static bool isKeywordEnabled( std::string_view keyword ); private: static std::vector> sm_logger; static std::string sm_lastMessage; static std::chrono::time_point sm_lastMessageTime; }; //================================================================================================== // //================================================================================================== class RiuMessageLoggerBase : public RiaLogger { public: explicit RiuMessageLoggerBase(); int level() const override; void setLevel( int logLevel ) override; void error( const char* message ) override; void warning( const char* message ) override; void info( const char* message ) override; void debug( const char* message ) override; protected: virtual void writeMessageToLogger( const std::string& str ) = 0; private: void writeMessageWithPrefixToLogger( const char* prefix, const char* message ); private: int m_logLevel; }; //================================================================================================== // //================================================================================================== class RiaStdOutLogger : public RiuMessageLoggerBase { public: void writeMessageToLogger( const std::string& str ) override; }; //================================================================================================== // //================================================================================================== class RiaThreadSafeLogger { public: void error( const QString& message ); void warning( const QString& message ); void info( const QString& message ); void debug( const QString& message ); std::vector messages() const; private: std::vector m_messages; };