source: calamares/trunk/fuentes/src/libcalamares/utils/Logger.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 4.8 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
4 *   Copyright 2014,      Teo Mrnjavac <teo@kde.org>
5 *   Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
6 *
7 *   Calamares is free software: you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation, either version 3 of the License, or
10 *   (at your option) any later version.
11 *
12 *   Calamares is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "Logger.h"
22
23#include <iostream>
24#include <fstream>
25
26#include <QCoreApplication>
27#include <QDir>
28#include <QFileInfo>
29#include <QMutex>
30#include <QTime>
31#include <QVariant>
32
33#include "utils/CalamaresUtils.h"
34#include "CalamaresVersion.h"
35
36#define LOGFILE_SIZE 1024 * 256
37
38static std::ofstream logfile;
39static unsigned int s_threshold =
40#ifdef QT_NO_DEBUG
41            Logger::LOG_DISABLE;
42#else
43            Logger::LOGEXTRA + 1;  // Comparison is < in log() function
44#endif
45static QMutex s_mutex;
46
47namespace Logger
48{
49
50void
51setupLogLevel(unsigned int level)
52{
53    if ( level > LOGVERBOSE )
54        level = LOGVERBOSE;
55    s_threshold = level + 1;  // Comparison is < in log() function
56}
57
58static void
59log( const char* msg, unsigned int debugLevel, bool toDisk = true )
60{
61    if ( toDisk || debugLevel < s_threshold )
62    {
63        QMutexLocker lock( &s_mutex );
64
65        // If we don't format the date as a Qt::ISODate then we get a crash when
66        // logging at exit as Qt tries to use QLocale to format, but QLocale is
67        // on its way out.
68        logfile << QDate::currentDate().toString( Qt::ISODate ).toUtf8().data()
69                << " - "
70                << QTime::currentTime().toString().toUtf8().data()
71                << " [" << QString::number( debugLevel ).toUtf8().data() << "]: "
72                << msg << std::endl;
73
74        logfile.flush();
75    }
76
77    if ( debugLevel <= LOGEXTRA || debugLevel < s_threshold )
78    {
79        QMutexLocker lock( &s_mutex );
80
81        std::cout << QTime::currentTime().toString().toUtf8().data()
82             << " [" << QString::number( debugLevel ).toUtf8().data() << "]: "
83             << msg << std::endl;
84        std::cout.flush();
85    }
86}
87
88
89static void
90CalamaresLogHandler( QtMsgType type, const QMessageLogContext& context, const QString& msg )
91{
92    static QMutex s_mutex;
93
94    Q_UNUSED( context );
95
96    QByteArray ba = msg.toUtf8();
97    const char* message = ba.constData();
98
99    QMutexLocker locker( &s_mutex );
100    switch( type )
101    {
102        case QtDebugMsg:
103            log( message, LOGVERBOSE );
104            break;
105
106        case QtInfoMsg:
107            log( message, 1 );
108            break;
109
110        case QtCriticalMsg:
111        case QtWarningMsg:
112        case QtFatalMsg:
113            log( message, 0 );
114            break;
115    }
116}
117
118
119QString
120logFile()
121{
122    return CalamaresUtils::appLogDir().filePath( "session.log" );
123}
124
125
126void
127setupLogfile()
128{
129    if ( QFileInfo( logFile().toLocal8Bit() ).size() > LOGFILE_SIZE )
130    {
131        QByteArray lc;
132        {
133            QFile f( logFile().toLocal8Bit() );
134            f.open( QIODevice::ReadOnly | QIODevice::Text );
135            lc = f.readAll();
136            f.close();
137        }
138
139        QFile::remove( logFile().toLocal8Bit() );
140
141        {
142            QFile f( logFile().toLocal8Bit() );
143            f.open( QIODevice::WriteOnly | QIODevice::Text );
144            f.write( lc.right( LOGFILE_SIZE - ( LOGFILE_SIZE / 4 ) ) );
145            f.close();
146        }
147    }
148
149    // Since the log isn't open yet, this probably only goes to stdout
150    cDebug() << "Using log file:" << logFile();
151
152    // Lock while (re-)opening the logfile
153    {
154    QMutexLocker lock( &s_mutex );
155    logfile.open( logFile().toLocal8Bit(), std::ios::app );
156    if ( logfile.tellp() )
157        logfile << "\n\n" << std::endl;
158    logfile << "=== START CALAMARES " << CALAMARES_VERSION << std::endl;
159    }
160
161    qInstallMessageHandler( CalamaresLogHandler );
162}
163
164CLog::CLog( unsigned int debugLevel )
165    : QDebug( &m_msg )
166    , m_debugLevel( debugLevel )
167{
168}
169
170
171CLog::~CLog()
172{
173    log( m_msg.toUtf8().data(), m_debugLevel );
174}
175
176CDebug::~CDebug()
177{
178}
179
180const char* continuation = "\n    ";
181
182QString toString( const QVariant& v )
183{
184    auto t = v.type();
185
186    if ( t == QVariant::List )
187    {
188        QStringList s;
189        auto l = v.toList();
190        for ( auto lit = l.constBegin(); lit != l.constEnd(); ++lit )
191            s << lit->toString();
192        return s.join(", ");
193    }
194    else
195        return v.toString();
196}
197
198}  // namespace
Note: See TracBrowser for help on using the repository browser.