[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;
}
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
@ -62,33 +69,40 @@ logLevelEnabled( unsigned int level )
return level < s_threshold;
}
unsigned int
logLevel()
/** @brief Should we call the log_implementation() function with this level?
*
* 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
log( const char* msg, unsigned int debugLevel, bool withTime = true )
{
if ( logLevelEnabled( debugLevel ) )
log_implementation( const char* msg, unsigned int debugLevel, const bool withTime )
{
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
// logging at exit as Qt tries to use QLocale to format, but QLocale is
// on its way out.
logfile << QDate::currentDate().toString( Qt::ISODate ).toUtf8().data() << " - "
<< QTime::currentTime().toString().toUtf8().data() << " ["
<< QString::number( debugLevel ).toUtf8().data() << "]: " << msg << std::endl;
logfile << date.toUtf8().data() << " - " << time.toUtf8().data() << " [" << debugLevel << "]: " << msg << std::endl;
logfile.flush();
if ( logLevelEnabled( debugLevel ) )
{
if ( withTime )
{
std::cout << QTime::currentTime().toString().toUtf8().data() << " ["
<< QString::number( debugLevel ).toUtf8().data() << "]: ";
std::cout << time.toUtf8().data() << " [" << debugLevel << "]: ";
}
// The endl is desired, since it also flushes (like the logfile, above)
std::cout << msg << std::endl;
}
}
@ -97,29 +111,30 @@ log( const char* msg, unsigned int debugLevel, bool withTime = true )
static void
CalamaresLogHandler( QtMsgType type, const QMessageLogContext&, const QString& msg )
{
static QMutex s_mutex;
QByteArray ba = msg.toUtf8();
const char* message = ba.constData();
QMutexLocker locker( &s_mutex );
unsigned int level = LOGVERBOSE;
switch ( type )
{
case QtInfoMsg:
log( message, LOGVERBOSE );
level = LOGVERBOSE;
break;
case QtDebugMsg:
log( message, LOGDEBUG );
level = LOGDEBUG;
break;
case QtWarningMsg:
log( message, LOGWARNING );
level = LOGWARNING;
break;
case QtCriticalMsg:
case QtFatalMsg:
log( message, LOGERROR );
level = LOGERROR;
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()
{
if ( logLevelEnabled( m_debugLevel ) )
if ( log_enabled( m_debugLevel ) )
{
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, m_funcinfo );
log_implementation( m_msg.toUtf8().data(), m_debugLevel, bool( m_funcinfo ) );
}
}