Files

142 lines
4.8 KiB
C++
Raw Permalink Normal View History

/////////////////////////////////////////////////////////////////////////////////
//
// 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 <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
2024-10-29 14:06:58 +01:00
#include <chrono>
2020-10-22 13:57:48 +02:00
#include <memory>
#include <string>
#include <vector>
2025-04-08 13:17:38 +02:00
#include <QString>
class QWidget;
2020-04-24 08:10:48 +02:00
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;
};
//==================================================================================================
//
//
//
//==================================================================================================
class RiaLogging
{
public:
2024-01-15 17:51:03 +01:00
static std::vector<RiaLogger*> loggerInstances();
static void appendLoggerInstance( std::unique_ptr<RiaLogger> loggerInstance );
static RILogLevel logLevelBasedOnPreferences();
static std::optional<RILogLevel> parseLogLevelString( const QString& logLevelString );
2022-12-19 15:24:53 +01:00
2025-04-08 13:17:38 +02:00
static void error( const QString& message, const QString logKeyword = "" );
static void warning( const QString& message, const QString logKeyword = "" );
static void info( const QString& message, const QString logKeyword = "" );
static void debug( const QString& message, const QString logKeyword = "" );
2017-02-27 13:17:32 +01:00
static void errorInMessageBox( QWidget* parent, const QString& title, const QString& text );
2025-07-23 14:53:52 +02:00
static std::chrono::time_point<std::chrono::high_resolution_clock> currentTime();
static void logElapsedTime( const QString& message, const std::chrono::time_point<std::chrono::high_resolution_clock>& startTime );
2024-10-29 14:06:58 +01:00
private:
static void setLastMessage( const QString& message );
static bool isSameMessage( const QString& message );
private:
2024-10-29 14:06:58 +01:00
static std::vector<std::unique_ptr<RiaLogger>> sm_logger;
static QString sm_lastMessage;
static std::chrono::time_point<std::chrono::high_resolution_clock> 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<QString> messages() const;
private:
std::vector<QString> m_messages;
};