[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfdebug.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfdebug.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 
00025 // All definitions are in klfdefs.cpp. I may choose to move them here in the future.
00026 // ### Aug 2012: adding a template qt msg handler tool here.
00027 
00028 // this file is here to fool automoc to create a klfdebug.moc.cpp for klfdebug.h
00029 // in klfkateplugin.
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 // define a template qt message handler:
00046 
00047 // DEBUG, WARNING AND FATAL MESSAGES HANDLER
00048 
00049 // redirect debug output to this file (if non-NULL) instead of stderr
00050 static FILE *klf_qt_msg_fp = NULL;
00051 
00052 // in case we want to print messages directly into terminal
00053 static FILE *klf_fp_tty = NULL;
00054 static bool klf_fp_tty_failed = false;
00055 
00056 
00057 // a buffer where we store all warning messages
00058 static QByteArray klf_qt_msg_warnings_buffer;
00059 
00060 
00061 // internal
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   // display small message to indicate the redirection
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 &/*context*/, 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     // only with debugging enabled
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     // add the warning also to the warnings buffer.
00155     klf_qt_msg_warnings_buffer += QByteArray("Warning: ") + msg + "\n";
00156 #ifdef KLF_DEBUG
00157     // in debug mode, also print warning messages to TTY (because they get lost in the debug messages!)
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     // only show dialog after having created a QApplication
00165     if (qApp != NULL && qApp->inherits("QApplication")) {
00166       static int safecounter = SAFECOUNTER_NUM;
00167       if (!QString::fromLocal8Bit(msg).startsWith("MNG error")) { // ignore these "MNG" errors...
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     // add the message also to the warnings buffer.
00187     klf_qt_msg_warnings_buffer += QByteArray("Error: ") + msg + "\n";
00188     //
00189     // These messages can be seen in the "system messages" (Settings -> Advanced
00190     // -> System Messages); no need for error dialog
00191     //
00192     // #ifdef KLF_WS_WIN
00193     //     if (qApp != NULL && qApp->inherits("QApplication")) {
00194     //       QMessageBox::critical(0, QObject::tr("Error", "[[KLF's Qt Message Handler: dialog title]]"),
00195     //                      QObject::tr("KLatexFormula System Error:\n%1",
00196     //                                  "[[KLF's Qt Message Handler: dialog text]]")
00197     //                      .arg(QString::fromLocal8Bit(msg)));
00198     //     }
00199     // #endif
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 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net