Merge branch 'improve-logging'

This commit is contained in:
Adriaan de Groot 2020-03-03 17:01:04 +01:00
commit 1f34460d39
3 changed files with 55 additions and 30 deletions

View File

@ -44,6 +44,10 @@ static unsigned int s_threshold =
#endif
static QMutex s_mutex;
static const char s_Continuation[] = "\n ";
static const char s_SubEntry[] = " .. ";
namespace Logger
{
@ -172,22 +176,39 @@ setupLogfile()
qInstallMessageHandler( CalamaresLogHandler );
}
CLog::CLog( unsigned int debugLevel )
CDebug::CDebug( unsigned int debugLevel, const char* func )
: QDebug( &m_msg )
, m_debugLevel( debugLevel )
, m_funcinfo( func )
{
if ( debugLevel <= LOGERROR )
{
m_msg = QStringLiteral( "ERROR:" );
}
else if ( debugLevel <= LOGWARNING )
{
m_msg = QStringLiteral( "WARNING:" );
}
}
CLog::~CLog()
CDebug::~CDebug()
{
if ( m_funcinfo )
{
m_msg.prepend( s_Continuation ); // Prepending, so back-to-front
m_msg.prepend( m_funcinfo );
}
log( m_msg.toUtf8().data(), m_debugLevel );
}
CDebug::~CDebug() {}
constexpr FuncSuppressor::FuncSuppressor( const char s[] )
: m_s( s )
{
}
const char Continuation[] = "\n ";
const char SubEntry[] = " .. ";
const constexpr FuncSuppressor Continuation( s_Continuation );
const constexpr FuncSuppressor SubEntry( s_SubEntry );
QString
toString( const QVariant& v )

View File

@ -27,8 +27,14 @@
namespace Logger
{
DLLEXPORT extern const char Continuation[];
DLLEXPORT extern const char SubEntry[];
struct FuncSuppressor
{
explicit constexpr FuncSuppressor( const char[] );
const char* m_s;
};
DLLEXPORT extern const FuncSuppressor Continuation;
DLLEXPORT extern const FuncSuppressor SubEntry;
enum
{
@ -41,34 +47,32 @@ enum
LOGVERBOSE = 8
};
class DLLEXPORT CLog : public QDebug
class DLLEXPORT CDebug : public QDebug
{
public:
explicit CLog( unsigned int debugLevel );
virtual ~CLog();
explicit CDebug( unsigned int debugLevel = LOGDEBUG, const char* func = nullptr );
virtual ~CDebug();
friend QDebug& operator<<( CDebug&&, const FuncSuppressor& );
private:
QString m_msg;
unsigned int m_debugLevel;
const char* m_funcinfo = nullptr;
};
class DLLEXPORT CDebug : public CLog
inline QDebug&
operator<<( CDebug&& s, const FuncSuppressor& f )
{
public:
CDebug( unsigned int debugLevel = LOGDEBUG )
: CLog( debugLevel )
{
if ( debugLevel <= LOGERROR )
{
*this << "ERROR:";
}
else if ( debugLevel <= LOGWARNING )
{
*this << "WARNING:";
}
}
virtual ~CDebug();
};
s.m_funcinfo = nullptr;
return s << f.m_s;
}
inline QDebug&
operator<<( QDebug& s, const FuncSuppressor& f )
{
return s << f.m_s;
}
/**
* @brief The full path of the log file.
@ -219,8 +223,8 @@ operator<<( QDebug& s, const DebugMap& t )
}
} // namespace Logger
#define cDebug() (Logger::CDebug( Logger::LOGDEBUG ) << Q_FUNC_INFO << Logger::Continuation)
#define cWarning() Logger::CDebug( Logger::LOGWARNING )
#define cError() Logger::CDebug( Logger::LOGERROR )
#define cDebug() Logger::CDebug( Logger::LOGDEBUG, Q_FUNC_INFO )
#define cWarning() Logger::CDebug( Logger::LOGWARNING, Q_FUNC_INFO )
#define cError() Logger::CDebug( Logger::LOGERROR, Q_FUNC_INFO )
#endif

View File

@ -69,7 +69,7 @@ CreatePartitionTableJob::prettyStatusMessage() const
static inline QDebug&
operator <<( QDebug& s, PartitionIterator& it )
operator <<( QDebug&& s, PartitionIterator& it )
{
s << ( ( *it ) ? ( *it )->deviceNode() : QString( "<null device>" ) );
return s;