Sort bulgarian.lang into english.lang order
[maemo-rb.git] / rbutil / rbutilqt / systrace.cpp
blob943f34f7b9d53dea974d44566c7e7911ef736d6e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
19 #include <QFileDialog>
20 #include <QScrollBar>
21 #include "systrace.h"
22 #include "ui_systracefrm.h"
24 #include "rbsettings.h"
26 QString SysTrace::debugbuffer;
27 QString SysTrace::lastmessage;
28 unsigned int SysTrace::repeat = 0;
30 SysTrace::SysTrace(QWidget *parent) : QDialog(parent)
32 ui.setupUi(this);
33 ui.textTrace->setReadOnly(true);
34 ui.textTrace->setLayoutDirection(Qt::LeftToRight);
35 refresh();
37 connect(ui.buttonClose, SIGNAL(clicked()), this, SLOT(close()));
38 connect(ui.buttonSave, SIGNAL(clicked()), this, SLOT(saveCurrentTrace()));
39 connect(ui.buttonSavePrevious, SIGNAL(clicked()), this, SLOT(savePreviousTrace()));
40 connect(ui.buttonRefresh, SIGNAL(clicked()), this, SLOT(refresh()));
43 void SysTrace::refresh(void)
45 int pos = ui.textTrace->verticalScrollBar()->value();
46 flush();
47 ui.textTrace->setHtml("<pre>" + debugbuffer + "</pre>");
48 ui.textTrace->verticalScrollBar()->setValue(pos);
49 QString oldlog = RbSettings::value(RbSettings::CachePath).toString()
50 + "/rbutil-trace.log";
51 ui.buttonSavePrevious->setEnabled(QFileInfo(oldlog).isFile());
55 void SysTrace::save(QString filename)
57 if(filename.isEmpty())
58 filename = RbSettings::value(RbSettings::CachePath).toString()
59 + "/rbutil-trace.log";
60 // make sure any repeat detection is flushed
61 flush();
62 // append save date to the trace. Append it directly instead of using
63 // qDebug() as the handler might have been unregistered.
64 debugbuffer.append("[SysTrace] saving trace at ");
65 debugbuffer.append(QDateTime::currentDateTime().toString(Qt::ISODate));
66 debugbuffer.append("\n");
67 QFile fh(filename);
68 if(!fh.open(QIODevice::WriteOnly))
69 return;
70 fh.write(debugbuffer.toUtf8(), debugbuffer.size());
71 fh.close();
74 void SysTrace::saveCurrentTrace(void)
76 QString fp = QFileDialog::getSaveFileName(this, tr("Save system trace log"),
77 QDir::homePath(), "*.log");
78 if(!fp.isEmpty())
79 save(fp);
83 void SysTrace::savePreviousTrace(void)
85 QString fp = QFileDialog::getSaveFileName(this, tr("Save system trace log"),
86 QDir::homePath(), "*.log");
87 if(fp.isEmpty())
88 return;
90 QString oldlog = RbSettings::value(RbSettings::CachePath).toString()
91 + "/rbutil-trace.log";
92 QFile::copy(oldlog, fp);
93 return;
96 #if QT_VERSION < 0x050000
97 void SysTrace::debug(QtMsgType type, const char* msg)
99 (void)type;
100 if(lastmessage != msg) {
101 lastmessage = msg;
102 flush();
103 debugbuffer.append(QString::fromLocal8Bit(msg) + "\n");
104 #if !defined(NODEBUG)
105 fprintf(stderr, "%s\n", msg);
106 #endif
107 repeat = 1;
109 else {
110 repeat++;
113 #else
114 void SysTrace::debug(QtMsgType type, const QMessageLogContext &context, const QString &msg)
116 (void)type;
117 QByteArray localMsg = msg.toLocal8Bit();
118 if(lastmessage != msg) {
119 lastmessage = msg;
120 flush();
121 debugbuffer.append(msg + "\n");
122 #if !defined(NODEBUG)
123 fprintf(stderr, "%s\n", localMsg.constData());
124 #endif
125 repeat = 1;
127 else {
128 repeat++;
131 #endif
133 void SysTrace::flush(void)
135 if(repeat > 1) {
136 debugbuffer.append(
137 QString(" (Last message repeated %1 times.)\n").arg(repeat));
138 #if !defined(NODEBUG)
139 fprintf(stderr, " (Last message repeated %i times.)\n", repeat);
140 #endif
145 void SysTrace::changeEvent(QEvent *e)
147 if(e->type() == QEvent::LanguageChange) {
148 ui.retranslateUi(this);
149 } else {
150 QWidget::changeEvent(e);