[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    KCrash + kapp changes
From:       Carsten Pfeiffer <carpdjih () cetus ! zrz ! TU-Berlin ! DE>
Date:       2000-03-30 23:36:00
[Download RAW message or body]

Hi,

Tom Braun had a look at the kcrash stuff and hacked a little bit on it.
Changes:

- now a C++ class
- less variables
- easier interface
- documentation
- less bugs ;)

While doing this, some problem in kapp was discovered.
parseCommandLine() was overloaded, one using KCmdLineArgs, the other one
just parsing argv. This led to unnecessary and buggy code (one was being
updated, the other not). Creating a KCmdLineArgs object and then using the
first parseCommandLine() fixes this.

Anyone having objections to me committing?

Cheers,
Carsten Pfeiffer
-- 
http://www.geocities.com/SiliconValley/1632/

["kcrash.patch" (text/plain)]

Index: kapp.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kapp.cpp,v
retrieving revision 1.290
diff -u -u -r1.290 kapp.cpp
--- kapp.cpp	2000/03/28 03:17:46	1.290
+++ kapp.cpp	2000/03/30 23:37:42
@@ -194,7 +194,8 @@
     pAppData = new KApplicationPrivate;
 
     init(GUIenabled);
-    parseCommandLine( argc, argv );
+    KCmdLineArgs::init(argc, argv, argv[0], "A KDE-Application", "-");
+    parseCommandLine();
 }
 
 KApplication::KApplication( bool allowStyles, bool GUIenabled ) :
@@ -598,74 +599,7 @@
 			msgStr, i18n("OK"));
 }
 
-void KApplication::parseCommandLine( int& argc, char** argv )
-{
-    enum parameter_code { unknown = 0, caption, icon, miniicon, dcopserver };
-    const char* parameter_strings[] = { "-caption", "-icon", "-miniicon", \
                "-dcopserver", 0 };
-
-    aDummyString2 = " ";
-    int i = 1;
-    parameter_code parameter;
-    while( i < argc ) {
-	parameter = unknown;
-
-	for ( int p = 0 ; parameter_strings[p]; p++)
-	    if ( !strcmp( argv[i], parameter_strings[p]) ) {
-		parameter = static_cast<parameter_code>(p + 1);
-		break;
-	    }
-
-	if ( parameter != unknown && argc < i +2 ) { // last argument without parameters
-	    argc -= 1;
-	    break; // jump out of the while loop
-	}
-
-	switch (parameter) {
-	case caption:
-	    aCaption = argv[i+1];
-	    aDummyString2 += parameter_strings[caption-1];
-	    aDummyString2 += " \"";
-	    aDummyString2 += argv[i+1];
-	    aDummyString2 += "\" ";
-	    break;
-	case icon:
-	    if (argv[i+1][0] == '/')
-		aIconPixmap = QPixmap(argv[i+1]);
-	    else
-		aIconPixmap = DesktopIcon(argv[i+1]);
-	    if (aMiniIconPixmap.isNull())
-		aMiniIconPixmap = SmallIcon(argv[i+1]);
-	    aDummyString2 += parameter_strings[icon-1];
-	    aDummyString2 += " ";
-	    aDummyString2 += argv[i+1];
-	    aDummyString2 += " ";
-	    break;
-	case miniicon:
-	    aMiniIconPixmap = SmallIcon(argv[i+1]);
-	    aDummyString2 += parameter_strings[miniicon-1];
-	    aDummyString2 += " ";
-	    aDummyString2 += argv[i+1];
-	    aDummyString2 += " ";
-	    break;
-	case dcopserver:
-	    dcopClient()->setServerAddress(argv[i+1]);
-	    break;
-	case unknown:
-	    i++;
-	}
-
-	if ( parameter != unknown ) { // remove arguments
-
-	    for( int j = i;  j < argc-2; j++ )
-		argv[j] = argv[j+2];
 
-	    argc -=2 ;
-	}
-
-    }
-    pArgc = argc;
-}
-
 static const KCmdLineOptions qt_options[] =
 {
    { "display <displayname>", I18N_NOOP("Use the X-server display 'displayname'"), \
0}, @@ -684,8 +618,7 @@
    { 0, 0, 0 }
 };
 
-void
-KApplication::addCmdLineOptions()
+void KApplication::addCmdLineOptions()
 {
    KCmdLineArgs::addCmdLineOptions(qt_options, "Qt", "qt");
    KCmdLineArgs::addCmdLineOptions(kde_options, "KDE", "kde");
@@ -721,14 +654,8 @@
 
     if (args->isSet("crashhandler"))
     {
-#if 0
-        // reset the crash handler recursive counter
-        resetCrashRecursion();
-
-        // set default crash handler / set emergency save function to nothing
-        setCrashHandler(KDE_CRASH_INTERNAL);
-        setEmergencySaveFunction(KDE_SAVE_NONE);
-#endif
+       // install the internal crash-handler (KCrash::defaultCrashHandler())
+       KCrash::setCrashHandler();
     }
 
     delete args; // Throw away
Index: kapp.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kapp.h,v
retrieving revision 1.141
diff -u -u -r1.141 kapp.h
--- kapp.h	2000/03/25 20:08:00	1.141
+++ kapp.h	2000/03/30 23:37:42
@@ -116,6 +116,7 @@
 
   /**
     * Add Qt and KDE command line options to KCmdLineArgs.
+    * Usually called from KCmdLineArgs::init.
     */
   static void addCmdLineOptions();
 
@@ -495,7 +496,6 @@
 
   void init( bool GUIenabled );
 
-  void parseCommandLine( int&, char** ); // search for special KDE arguments
   void parseCommandLine( ); // Handle KDE arguments (Using KCmdLineArgs)
 
   virtual void kdisplaySetPalette();
Index: kcrash.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kcrash.cpp,v
retrieving revision 1.8
diff -u -u -r1.8 kcrash.cpp
--- kcrash.cpp	2000/03/29 09:11:59	1.8
+++ kcrash.cpp	2000/03/30 23:37:42
@@ -1,6 +1,7 @@
 /*
  * This file is part of the KDE Libraries
- * Copyright (C) 2000 Timo Hummel (timo.hummel@sap.com)
+ * Copyright (C) 2000 Timo Hummel <timo.hummel@sap.com>
+ *                    Tom Braun <braunt@fh-konstanz.de>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -29,87 +30,67 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <kdebug.h>
 
 #include "kcrash.h"
 
-static int CrashRecursionCounter;	// If a crash occurs in our crash handler \
                procedure, we can handle it :)
-kdesignal_t emergencySaveFunction;
 
-// This function sets the function which should be called when the application \
crashes and the +KCrash::HandlerType KCrash::_emergencySaveFunction = 0;
+KCrash::HandlerType KCrash::_crashHandler = 0;
+
+
+// This function sets the function which should be called when the 
+// application crashes and the
 // application is asked to try to save its data.
 void
-setEmergencySaveFunction (kdesignal_t saveFunction)
+KCrash::setEmergencySaveFunction (HandlerType saveFunction)
 {
-  if (saveFunction == KDE_SAVE_NONE)
-    {
-      emergencySaveFunction = KDE_SAVE_NONE;
-    }
-
-  emergencySaveFunction = saveFunction;
+  _emergencySaveFunction = saveFunction;
+  
+  /* 
+   * We need at least the default crash handler for 
+   * emergencySaveFunction to be called
+   */
+  if (_emergencySaveFunction && !_crashHandler)
+    _crashHandler = defaultCrashHandler;
 }
 
 
-// This function sets the function which should be responsible for the application \
                crash handling.
-// Usually, this should be KDE_CRASH_INTERNAL.
+// This function sets the function which should be responsible for 
+// the application crash handling.
 
 void
-setCrashHandler (kdesignal_t crashHandler)
+KCrash::setCrashHandler (HandlerType handler)
 {
-  printf ("Trying to install kcrash handler...\n");
+  if (!handler)
+    signal (SIGSEGV, SIG_DFL);
 
-  if (crashHandler == KDE_CRASH_DEFAULT)
-    {
-      printf ("Installing the system's default bug handler...");
-      signal (SIGSEGV, SIG_DFL);
-      printf ("done.\n");
-      return;
-    }
-
-  if (crashHandler == KDE_CRASH_INTERNAL)
-    {
-      printf ("Installing the KDE internal crash handler...");
-      signal (SIGSEGV, defaultCrashHandler);
-      printf ("done\n");
-      return;
-    }
+  kdDebug(0) << "KCrash: Installing the application's crash handler...";
+  signal (SIGSEGV, handler);
+  kdDebug(0) << "done" << endl;
 
-  printf ("Installing the application's crash handler...");
-  signal (SIGSEGV, crashHandler);
-  printf ("done.\n");
+  _crashHandler = handler;
 }
 
-void
-resetCrashRecursion (void)
-{
-  printf ("Crash recursion set to zero.\n");
-  CrashRecursionCounter = 0;
-}
 
 void
-defaultCrashHandler (int signal)
+KCrash::defaultCrashHandler (int signal)
 {
   // Handle possible recursions
-
-  CrashRecursionCounter++;
-
-  if (CrashRecursionCounter < 2)
-    {
-     
-      // Check if 
-      if (emergencySaveFunction != KDE_SAVE_NONE)
-	{
-	  emergencySaveFunction (signal);
-	}
-    }
-
-  if (CrashRecursionCounter < 3)
-    {
-      printf ("Would start dr. konqi here\n");
+  static int crashRecursionCounter = 0;
+  crashRecursionCounter++;
 
-      execlp ("xmessage", "xmessage", "An application crashed!",
-	      NULL);
+  if (crashRecursionCounter < 2) {
+    if (_emergencySaveFunction) {
+      _emergencySaveFunction (signal);
     }
+  }
+  
+  if (crashRecursionCounter < 3) {
+    kdDebug(0) << "Would start dr. konqi here\n";  
+    execlp ("xmessage", "xmessage", "An application crashed!", NULL);
+  }
 
-  printf ("Unable to start dr. konqi\n");
+  kdDebug(0) << "Unable to start dr. konqi\n";
   exit (1);
 }
Index: kcrash.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kcrash.h,v
retrieving revision 1.5
diff -u -u -r1.5 kcrash.h
--- kcrash.h	2000/03/29 09:11:59	1.5
+++ kcrash.h	2000/03/30 23:37:42
@@ -1,7 +1,7 @@
 /*
  * This file is part of the KDE Libraries
- * Copyright (C) 2000 Timo Hummel (timo.hummel@sap.com)
- *
+ * Copyright (C) 2000 Timo Hummel <timo.hummel@sap.com>
+ *                    Tom Braun <braunt@fh-konstanz.de>
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License as published by the Free Software Foundation; either
@@ -19,39 +19,70 @@
  *
  */
 
-/*
- * This file is used to catch signals which would normally
- * crash the application (like segmentation fault, floating
- * point exception and such).
- */
-
 #ifndef __KCRASH_H
 #define __KCRASH_H
 
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <qstring.h>
-
-extern "C" {
- // Taken from signal.h
- typedef void(*kdesignal_t) (int);  
-
- void defaultCrashHandler (int signal);
- void setCrashHandler (kdesignal_t);
- void setEmergencySaveFunction (kdesignal_t);
- void resetCrashRecursion (void);
-
- const kdesignal_t KDE_CRASH_DEFAULT  = ((kdesignal_t)0x1);
- const kdesignal_t KDE_CRASH_INTERNAL = ((kdesignal_t)0x2);
- const kdesignal_t KDE_SAVE_NONE      = ((kdesignal_t)0x3);
-
- struct kcrashargs {
- 	char *crashObjName;
- 	char *crashObjValue;
- };
 
-}
+/**
+ * This class handles segmentation-faults.
+ * By default it displays a  message-box saying the application crashed.
+ * This default can be overridden by setting a custom crash handler with
+ * @ref setCrashHandler().
+ * If a function is specified with @ref setEmergencySaveFunction() it will
+ * be called by the default crash handler, giving the application a chance
+ * to save its data.
+ */ 
+class KCrash 
+{
+ private: // ;o)
+  static void defaultCrashHandler (int signal);
+ public:
+  typedef void (*HandlerType)(int);
+  
+
+  /**
+   * Install a function to be called in case a SIGSEGV is caught.
+   * @param HandlerType handler can be one of 
+   * @li null in wich case signal-catching is disabled 
+   *  (by calling signal(SIGSEGV, SIG_DFL))
+   * @li if handler is omitted the default crash handler is installed.
+   * @li a pointer to a void function taking an int (the signal´s number)
+   *   where HandlerType looks like 
+   *   <pre>typedef void (*HandlerType)(int)</pre>
+   */
+  static void setCrashHandler (HandlerType handler = defaultCrashHandler);
+
+  /**
+   * Returns the intalled crash handler
+   */
+  static HandlerType crashHandler() { return _crashHandler; }
+
+  /**
+   * Installs a function which should try to save the applications data.
+   * It is the crash handler´s responsibility to call this function.
+   * Therefore, if no crash handler is set, the default crash handler 
+   * is installed to ensure the save function is called.
+   */
+  static void setEmergencySaveFunction (HandlerType saveFunction = (HandlerType)0);
+  /**
+   * Return the currently set emergency save function.
+   */
+  static HandlerType emergencySaveFunction() { return _emergencySaveFunction; }
+  
+  /**
+   * not used yet
+   */
+  struct KCrashArgs {
+    char *crashObjName;
+    char *crashObjValue;
+  };
+ protected:
+  static HandlerType _crashHandler;
+  static HandlerType _emergencySaveFunction; 
+};
 
 #endif
 



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic