[libcalamares] Always log to file, sometimes to terminal

This commit is contained in:
Adriaan de Groot 2022-03-22 12:54:54 +01:00
parent aedb55ea36
commit 99b19b9539

View File

@ -53,7 +53,14 @@ setupLogLevel( unsigned int level )
{ {
level = LOGVERBOSE; level = LOGVERBOSE;
} }
s_threshold = level + 1; // Comparison is < in log() function s_threshold = level + 1; // Comparison is < in logLevelEnabled() function
}
unsigned int
logLevel()
{
// Undo the +1 in setupLogLevel()
return s_threshold > 0 ? s_threshold - 1 : 0;
} }
bool bool
@ -62,33 +69,40 @@ logLevelEnabled( unsigned int level )
return level < s_threshold; return level < s_threshold;
} }
unsigned int /** @brief Should we call the log_implementation() function with this level?
logLevel() *
* The implementation logs everything for which logLevelEnabled() is
* true to the file **and** to stdout; it logs everything at debug-level
* or below to the file regardless.
*/
static inline bool
log_enabled( unsigned int level )
{ {
return s_threshold > 0 ? s_threshold - 1 : 0; return level <= LOGDEBUG || logLevelEnabled( level );
} }
static void static void
log( const char* msg, unsigned int debugLevel, bool withTime = true ) log_implementation( const char* msg, unsigned int debugLevel, const bool withTime )
{
if ( logLevelEnabled( debugLevel ) )
{ {
QMutexLocker lock( &s_mutex ); QMutexLocker lock( &s_mutex );
const auto date = QDate::currentDate().toString( Qt::ISODate );
const auto time = QTime::currentTime().toString();
// If we don't format the date as a Qt::ISODate then we get a crash when // If we don't format the date as a Qt::ISODate then we get a crash when
// logging at exit as Qt tries to use QLocale to format, but QLocale is // logging at exit as Qt tries to use QLocale to format, but QLocale is
// on its way out. // on its way out.
logfile << QDate::currentDate().toString( Qt::ISODate ).toUtf8().data() << " - " logfile << date.toUtf8().data() << " - " << time.toUtf8().data() << " [" << debugLevel << "]: " << msg << std::endl;
<< QTime::currentTime().toString().toUtf8().data() << " ["
<< QString::number( debugLevel ).toUtf8().data() << "]: " << msg << std::endl;
logfile.flush(); logfile.flush();
if ( logLevelEnabled( debugLevel ) )
{
if ( withTime ) if ( withTime )
{ {
std::cout << QTime::currentTime().toString().toUtf8().data() << " [" std::cout << time.toUtf8().data() << " [" << debugLevel << "]: ";
<< QString::number( debugLevel ).toUtf8().data() << "]: ";
} }
// The endl is desired, since it also flushes (like the logfile, above)
std::cout << msg << std::endl; std::cout << msg << std::endl;
} }
} }
@ -97,29 +111,30 @@ log( const char* msg, unsigned int debugLevel, bool withTime = true )
static void static void
CalamaresLogHandler( QtMsgType type, const QMessageLogContext&, const QString& msg ) CalamaresLogHandler( QtMsgType type, const QMessageLogContext&, const QString& msg )
{ {
static QMutex s_mutex; unsigned int level = LOGVERBOSE;
QByteArray ba = msg.toUtf8();
const char* message = ba.constData();
QMutexLocker locker( &s_mutex );
switch ( type ) switch ( type )
{ {
case QtInfoMsg: case QtInfoMsg:
log( message, LOGVERBOSE ); level = LOGVERBOSE;
break; break;
case QtDebugMsg: case QtDebugMsg:
log( message, LOGDEBUG ); level = LOGDEBUG;
break; break;
case QtWarningMsg: case QtWarningMsg:
log( message, LOGWARNING ); level = LOGWARNING;
break; break;
case QtCriticalMsg: case QtCriticalMsg:
case QtFatalMsg: case QtFatalMsg:
log( message, LOGERROR ); level = LOGERROR;
break; break;
} }
if ( !log_enabled( level ) )
{
return;
}
log_implementation( msg.toUtf8().constData(), level, true );
} }
@ -188,14 +203,14 @@ CDebug::CDebug( unsigned int debugLevel, const char* func )
CDebug::~CDebug() CDebug::~CDebug()
{ {
if ( logLevelEnabled( m_debugLevel ) ) if ( log_enabled( m_debugLevel ) )
{ {
if ( m_funcinfo ) if ( m_funcinfo )
{ {
m_msg.prepend( s_Continuation ); // Prepending, so back-to-front m_msg.prepend( s_Continuation ); // Prepending, so back-to-front
m_msg.prepend( m_funcinfo ); m_msg.prepend( m_funcinfo );
} }
log( m_msg.toUtf8().data(), m_debugLevel, m_funcinfo ); log_implementation( m_msg.toUtf8().data(), m_debugLevel, bool( m_funcinfo ) );
} }
} }