Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdio.h>
00033
00034 #include <QDebug>
00035 #include <QByteArray>
00036 #include <QDateTime>
00037 #include <QApplication>
00038 #include <QMessageBox>
00039
00040 #include <klfdefs.h>
00041 #include <klfdebug.h>
00042
00043
00044
00045
00046
00047
00048
00049
00050 static FILE *klf_qt_msg_fp = NULL;
00051
00052
00053 static FILE *klf_fp_tty = NULL;
00054 static bool klf_fp_tty_failed = false;
00055
00056
00057
00058 static QByteArray klf_qt_msg_warnings_buffer;
00059
00060
00061
00062 static void ensure_tty_fp()
00063 {
00064 #ifdef Q_OS_UNIX
00065 if (klf_fp_tty == NULL && !klf_fp_tty_failed) {
00066 if ( !(klf_fp_tty = fopen("/dev/tty", "w")) ) {
00067 klf_fp_tty_failed = true;
00068 }
00069 }
00070 #else
00071 Q_UNUSED(klf_fp_tty_failed) ;
00072 #endif
00073 }
00074
00075
00083 KLF_EXPORT FILE * klf_qt_msg_get_tty_fp()
00084 {
00085 ensure_tty_fp();
00086 return klf_fp_tty;
00087 }
00088
00094 KLF_EXPORT bool klf_qt_msg_tty_fp_failed()
00095 {
00096 ensure_tty_fp();
00097 return klf_fp_tty_failed;
00098 }
00099
00101 KLF_EXPORT void klf_qt_msg_set_fp(FILE * fp)
00102 {
00103 klf_qt_msg_fp = fp;
00104
00105
00106 FILE *fout = klf_qt_msg_fp;
00107 if (fout == NULL)
00108 fout = stderr;
00109 fprintf(fout, "\n\n"
00110 "-------------------------------------------------\n"
00111 " KLATEXFORMULA DEBUG OUTPUT\n"
00112 "-------------------------------------------------\n"
00113 "Redirected on %s\n\n",
00114 qPrintable(QDateTime::currentDateTime().toString(Qt::DefaultLocaleLongDate)));
00115 }
00116
00118 KLF_EXPORT QByteArray klf_qt_msg_get_warnings_buffer()
00119 {
00120 return klf_qt_msg_warnings_buffer;
00121 }
00122
00123 KLF_EXPORT void klf_qt_msg_clear_warnings_buffer()
00124 {
00125 klf_qt_msg_warnings_buffer = QByteArray();
00126 }
00127
00132 KLF_EXPORT void klf_qt_msg_handle(QtMsgType type, const QMessageLogContext &, const QString &msgstr)
00133 {
00134 FILE *fout = stderr;
00135 if (klf_qt_msg_fp != NULL) {
00136 fout = klf_qt_msg_fp;
00137 }
00138 ensure_tty_fp();
00139
00140 QByteArray msgbytes(msgstr.toLatin1());
00141 const char * msg = msgbytes.constData();
00142
00143 switch (type) {
00144 case QtDebugMsg:
00145
00146 #ifdef KLF_DEBUG
00147 fprintf(fout, "D: %s\n", msg);
00148 fflush(fout);
00149 #endif
00150 break;
00151 case QtWarningMsg:
00152 fprintf(fout, "Warning: %s\n", msg);
00153 fflush(fout);
00154
00155 klf_qt_msg_warnings_buffer += QByteArray("Warning: ") + msg + "\n";
00156 #ifdef KLF_DEBUG
00157
00158 if (klf_fp_tty)
00159 fprintf(klf_fp_tty, "Warning: %s\n", msg);
00160 #endif
00161
00162 #if defined KLF_WS_WIN && defined KLF_DEBUG
00163 # define SAFECOUNTER_NUM 10
00164
00165 if (qApp != NULL && qApp->inherits("QApplication")) {
00166 static int safecounter = SAFECOUNTER_NUM;
00167 if (!QString::fromLocal8Bit(msg).startsWith("MNG error")) {
00168 if (safecounter-- >= 0) {
00169 QMessageBox::warning(0, "Warning",
00170 QString("KLatexFormula System Warning:\n%1")
00171 .arg(QString::fromLocal8Bit(msg)));
00172 }
00173 }
00174 if (safecounter == -1) {
00175 QMessageBox::information(0, "Information",
00176 QString("Shown %1 system warnings. Will stop displaying them.").arg(SAFECOUNTER_NUM));
00177 safecounter = -2;
00178 }
00179 if (safecounter < -2) safecounter = -2;
00180 }
00181 #endif
00182 break;
00183 case QtCriticalMsg:
00184 fprintf(fout, "Error: %s\n", msg);
00185 fflush(fout);
00186
00187 klf_qt_msg_warnings_buffer += QByteArray("Error: ") + msg + "\n";
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 break;
00201 case QtFatalMsg:
00202 fprintf(fout, "Fatal: %s\n", msg);
00203 fflush(fout);
00204 #ifdef KLF_WS_WIN
00205 if (qApp != NULL && qApp->inherits("QApplication")) {
00206 QMessageBox::critical(0, QObject::tr("FATAL ERROR",
00207 "[[KLF's Qt Message Handler: dialog title]]"),
00208 QObject::tr("KLatexFormula System FATAL ERROR:\n%1",
00209 "[[KLF's Qt Message Handler: dialog text]]")
00210 .arg(QString::fromLocal8Bit(msg)));
00211 }
00212 #endif
00213 ::exit(255);
00214 default:
00215 fprintf(fout, "?????: %s\n", msg);
00216 fflush(fout);
00217 break;
00218 }
00219 }
00220
00221
00222
00223