File indexing completed on 2024-04-21 04:50:09

0001 /*
0002     SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3bdebuggingoutputcache.h"
0007 
0008 #include "k3bdevicemanager.h"
0009 #include "k3bdevice.h"
0010 #include "k3bdeviceglobals.h"
0011 #include "k3bcore.h"
0012 #include "k3bversion.h"
0013 #include "k3bglobals.h"
0014 
0015 #include <KLocalizedString>
0016 #include <kcoreaddons_version.h>
0017 
0018 
0019 static const int s_maxCache = 10*1024*1024; // 10 MB max cache size
0020 
0021 class K3b::DebuggingOutputCache::Private
0022 {
0023 public:
0024     Private()
0025         : stderrEnabled( false ),
0026           cacheSize( 0 ) {
0027     }
0028 
0029     bool stderrEnabled;
0030     QMap<QString, QString> groups;
0031     QMap<QString, QString> lastMessages;
0032     QMap<QString, int> lastMessageCount;
0033     int cacheSize;
0034 };
0035 
0036 
0037 K3b::DebuggingOutputCache::DebuggingOutputCache()
0038     : d( new Private() )
0039 {
0040 }
0041 
0042 
0043 K3b::DebuggingOutputCache::~DebuggingOutputCache()
0044 {
0045     delete d;
0046 }
0047 
0048 
0049 void K3b::DebuggingOutputCache::clear()
0050 {
0051     d->groups.clear();
0052     d->lastMessages.clear();
0053     d->lastMessageCount.clear();
0054     d->cacheSize = 0;
0055 
0056     if (k3bcore == Q_NULLPTR)
0057        return; 
0058     addOutput( QLatin1String( "System" ), QString::fromLatin1( "K3b Version: %1" ).arg(k3bcore->version()) );
0059     addOutput( QLatin1String( "System" ), QString::fromLatin1( "KDE Version: %1" ).arg(KCOREADDONS_VERSION_STRING) );
0060     addOutput( QLatin1String( "System" ), QString::fromLatin1( "Qt Version:  %1" ).arg(qVersion()) );
0061     addOutput( QLatin1String( "System" ), QString::fromLatin1( "Kernel:      %1" ).arg(K3b::kernelVersion()) );
0062 
0063     // devices in the logfile
0064     QList<K3b::Device::Device *> items(k3bcore->deviceManager()->allDevices());
0065     for( QList<K3b::Device::Device *>::const_iterator it = items.constBegin();
0066        it != items.constEnd(); ++it ) {
0067 
0068         K3b::Device::Device* dev = *it;
0069         addOutput( "Devices",
0070                    QString( "%1 (%2, %3) [%5] [%6] [%7]" )
0071                    .arg( dev->vendor() + ' ' + dev->description() + ' ' + dev->version() )
0072                    .arg( dev->blockDeviceName() )
0073                    .arg( K3b::Device::deviceTypeString( dev->type() ) )
0074                    .arg( K3b::Device::mediaTypeString( dev->supportedProfiles() ) )
0075                    .arg( K3b::Device::writingModeString( dev->writingModes() ) ) );
0076     }
0077 }
0078 
0079 
0080 void K3b::DebuggingOutputCache::addOutput( const QString& group, const QString& line )
0081 {
0082     if ( d->lastMessages[group] == line ) {
0083         d->lastMessageCount[group]++;
0084     }
0085     else {
0086         if ( d->lastMessageCount.contains( group ) &&
0087              d->lastMessageCount[group] > 1 ) {
0088             d->groups[group].append( QString( "=== last message repeated %1 times. ===\n" ).arg( d->lastMessageCount[group]   ) );
0089         }
0090         d->lastMessageCount[group] = 1;
0091         d->lastMessages[group] = line;
0092 
0093         d->cacheSize += line.length();
0094         if ( d->cacheSize > s_maxCache &&
0095             d->cacheSize - ( int )line.length() <= s_maxCache ) {
0096             d->groups[defaultGroup()].append( "=== K3b debugging output cache overflow ===\n" );
0097         }
0098         else if ( d->cacheSize <= s_maxCache ) {
0099             d->groups[group].append( line + '\n' );
0100         }
0101     }
0102 }
0103 
0104 
0105 K3b::DebuggingOutputCache& K3b::DebuggingOutputCache::operator<<( const QString& line )
0106 {
0107     addOutput( defaultGroup(), line );
0108     return *this;
0109 }
0110 
0111 
0112 QString K3b::DebuggingOutputCache::toString() const
0113 {
0114     QString s;
0115     for ( QMap<QString, QString>::const_iterator it = d->groups.constBegin();
0116           it != d->groups.constEnd(); ++it ) {
0117         if ( !s.isEmpty() )
0118             s.append( '\n' );
0119         s.append( it.key() + '\n' );
0120         s.append( "-----------------------\n" );
0121         s.append( *it );
0122     }
0123     return s;
0124 }
0125 
0126 
0127 QMap<QString, QString> K3b::DebuggingOutputCache::toGroups() const
0128 {
0129     return d->groups;
0130 }
0131 
0132 
0133 bool K3b::DebuggingOutputCache::stderrEnabled() const
0134 {
0135     return d->stderrEnabled;
0136 }
0137 
0138 
0139 void K3b::DebuggingOutputCache::enableStderr( bool b )
0140 {
0141     d->stderrEnabled = b;
0142 }
0143 
0144 
0145 QString K3b::DebuggingOutputCache::defaultGroup()
0146 {
0147     return "Misc";
0148 }