[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfdebug.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * file klfdebug.cpp
3 * This file is part of the KLatexFormula Project.
4 * Copyright (C) 2011 by Philippe Faist
5 * philippe.faist at bluewin.ch
6 * *
7 * This program 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 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program 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 this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22/* $Id$ */
23
24
25// All definitions are in klfdefs.cpp. I may choose to move them here in the future.
26// ### Aug 2012: adding a template qt msg handler tool here.
27
28// this file is here to fool automoc to create a klfdebug.moc.cpp for klfdebug.h
29// in klfkateplugin.
30
31
32#include <stdio.h>
33
34#include <QDebug>
35#include <QByteArray>
36#include <QDateTime>
37#include <QApplication>
38#include <QMessageBox>
39
40#include <klfdefs.h>
41#include <klfdebug.h>
42
43
44
45// define a template qt message handler:
46
47// DEBUG, WARNING AND FATAL MESSAGES HANDLER
48
49// redirect debug output to this file (if non-NULL) instead of stderr
50static FILE *klf_qt_msg_fp = NULL;
51
52// in case we want to print messages directly into terminal
53static FILE *klf_fp_tty = NULL;
54static bool klf_fp_tty_failed = false;
55
56
57// a buffer where we store all warning messages
58static QByteArray klf_qt_msg_warnings_buffer;
59
60
61// internal
62static void ensure_tty_fp()
63{
64#ifdef Q_OS_UNIX
65 if (klf_fp_tty == NULL && !klf_fp_tty_failed) {
66 if ( !(klf_fp_tty = fopen("/dev/tty", "w")) ) {
67 klf_fp_tty_failed = true;
68 }
69 }
70#else
71 Q_UNUSED(klf_fp_tty_failed) ;
72#endif
73}
74
75
84{
85 ensure_tty_fp();
86 return klf_fp_tty;
87}
88
95{
96 ensure_tty_fp();
97 return klf_fp_tty_failed;
98}
99
102{
103 klf_qt_msg_fp = fp;
104
105 // display small message to indicate the redirection
106 FILE *fout = klf_qt_msg_fp;
107 if (fout == NULL)
108 fout = stderr;
109 fprintf(fout, "\n\n"
110 "-------------------------------------------------\n"
111 " KLATEXFORMULA DEBUG OUTPUT\n"
112 "-------------------------------------------------\n"
113 "Redirected on %s\n\n",
114 qPrintable(QDateTime::currentDateTime().toString(Qt::DefaultLocaleLongDate)));
115}
116
119{
120 return klf_qt_msg_warnings_buffer;
121}
122
124{
125 klf_qt_msg_warnings_buffer = QByteArray();
126}
127
132KLF_EXPORT void klf_qt_msg_handle(QtMsgType type, const QMessageLogContext &/*context*/, const QString &msgstr)
133{
134 FILE *fout = stderr;
135 if (klf_qt_msg_fp != NULL) {
136 fout = klf_qt_msg_fp;
137 }
138 ensure_tty_fp();
139
140 QByteArray msgbytes(msgstr.toLatin1());
141 const char * msg = msgbytes.constData();
142
143 switch (type) {
144 case QtDebugMsg:
145 // only with debugging enabled
146#ifdef KLF_DEBUG
147 fprintf(fout, "D: %s\n", msg);
148 fflush(fout);
149#endif
150 break;
151 case QtWarningMsg:
152 fprintf(fout, "Warning: %s\n", msg);
153 fflush(fout);
154 // add the warning also to the warnings buffer.
155 klf_qt_msg_warnings_buffer += QByteArray("Warning: ") + msg + "\n";
156#ifdef KLF_DEBUG
157 // in debug mode, also print warning messages to TTY (because they get lost in the debug messages!)
158 if (klf_fp_tty)
159 fprintf(klf_fp_tty, "Warning: %s\n", msg);
160#endif
161
162#if defined KLF_WS_WIN && defined KLF_DEBUG
163# define SAFECOUNTER_NUM 10
164 // only show dialog after having created a QApplication
165 if (qApp != NULL && qApp->inherits("QApplication")) {
166 static int safecounter = SAFECOUNTER_NUM;
167 if (!QString::fromLocal8Bit(msg).startsWith("MNG error")) { // ignore these "MNG" errors...
168 if (safecounter-- >= 0) {
169 QMessageBox::warning(0, "Warning",
170 QString("KLatexFormula System Warning:\n%1")
171 .arg(QString::fromLocal8Bit(msg)));
172 }
173 }
174 if (safecounter == -1) {
175 QMessageBox::information(0, "Information",
176 QString("Shown %1 system warnings. Will stop displaying them.").arg(SAFECOUNTER_NUM));
177 safecounter = -2;
178 }
179 if (safecounter < -2) safecounter = -2;
180 }
181#endif
182 break;
183 case QtCriticalMsg:
184 fprintf(fout, "Error: %s\n", msg);
185 fflush(fout);
186 // add the message also to the warnings buffer.
187 klf_qt_msg_warnings_buffer += QByteArray("Error: ") + msg + "\n";
188 //
189 // These messages can be seen in the "system messages" (Settings -> Advanced
190 // -> System Messages); no need for error dialog
191 //
192 // #ifdef KLF_WS_WIN
193 // if (qApp != NULL && qApp->inherits("QApplication")) {
194 // QMessageBox::critical(0, QObject::tr("Error", "[[KLF's Qt Message Handler: dialog title]]"),
195 // QObject::tr("KLatexFormula System Error:\n%1",
196 // "[[KLF's Qt Message Handler: dialog text]]")
197 // .arg(QString::fromLocal8Bit(msg)));
198 // }
199 // #endif
200 break;
201 case QtFatalMsg:
202 fprintf(fout, "Fatal: %s\n", msg);
203 fflush(fout);
204#ifdef KLF_WS_WIN
205 if (qApp != NULL && qApp->inherits("QApplication")) {
206 QMessageBox::critical(0, QObject::tr("FATAL ERROR",
207 "[[KLF's Qt Message Handler: dialog title]]"),
208 QObject::tr("KLatexFormula System FATAL ERROR:\n%1",
209 "[[KLF's Qt Message Handler: dialog text]]")
210 .arg(QString::fromLocal8Bit(msg)));
211 }
212#endif
213 ::exit(255);
214 default:
215 fprintf(fout, "?????: %s\n", msg);
216 fflush(fout);
217 break;
218 }
219}
220
221
222
223
const char * type
KLF_EXPORT bool klf_qt_msg_tty_fp_failed()
returns true if an attempt to open the tty fp failed.
Definition klfdebug.cpp:94
KLF_EXPORT void klf_qt_msg_set_fp(FILE *fp)
redirect all debug/warning messages to a given file pointer
Definition klfdebug.cpp:101
KLF_EXPORT QByteArray klf_qt_msg_get_warnings_buffer()
returns the contents of the warnings buffer.
Definition klfdebug.cpp:118
KLF_EXPORT void klf_qt_msg_clear_warnings_buffer()
Definition klfdebug.cpp:123
KLF_EXPORT void klf_qt_msg_handle(QtMsgType type, const QMessageLogContext &, const QString &msgstr)
a template handler function for Qt debugging/warning/critical messages etc.
Definition klfdebug.cpp:132
KLF_EXPORT FILE * klf_qt_msg_get_tty_fp()
get the TTY file pointer.
Definition klfdebug.cpp:83
Debugging utilities.
Base declarations for klatexformula and some utilities.
#define KLF_EXPORT
Definition klfdefs.h:41
const char * constData() const
QDateTime currentDateTime()
QString tr(const char *sourceText, const char *disambiguation, int n)
QString fromLocal8Bit(const char *str, int size)
QByteArray toLatin1() const

Generated by doxygen 1.9.8