Merge branch 'improve-logging'

main
Adriaan de Groot 5 years ago
commit 1f34460d39

@ -44,6 +44,10 @@ static unsigned int s_threshold =
#endif #endif
static QMutex s_mutex; static QMutex s_mutex;
static const char s_Continuation[] = "\n ";
static const char s_SubEntry[] = " .. ";
namespace Logger namespace Logger
{ {
@ -172,22 +176,39 @@ setupLogfile()
qInstallMessageHandler( CalamaresLogHandler ); qInstallMessageHandler( CalamaresLogHandler );
} }
CLog::CLog( unsigned int debugLevel ) CDebug::CDebug( unsigned int debugLevel, const char* func )
: QDebug( &m_msg ) : QDebug( &m_msg )
, m_debugLevel( debugLevel ) , 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 ); log( m_msg.toUtf8().data(), m_debugLevel );
} }
CDebug::~CDebug() {} constexpr FuncSuppressor::FuncSuppressor( const char s[] )
: m_s( s )
{
}
const char Continuation[] = "\n "; const constexpr FuncSuppressor Continuation( s_Continuation );
const char SubEntry[] = " .. "; const constexpr FuncSuppressor SubEntry( s_SubEntry );
QString QString
toString( const QVariant& v ) toString( const QVariant& v )

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

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

Loading…
Cancel
Save