home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / krcls012.zip / KrClass / source / krtrace.cpp < prev    next >
Text File  |  1997-02-14  |  5KB  |  201 lines

  1. // Kroni's Classes: objects for exception handling and tracing
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: krtrace.cpp
  4.  
  5. #define INCL_DOSFILEMGR
  6. #include <os2.h>
  7.  
  8. #include "krtrace.hpp"
  9.  
  10. #include <itrace.hpp>
  11. #include <iapp.hpp>
  12. #include <terminat.h>
  13. #include <imsgbox.hpp>
  14.  
  15.  
  16. IString KrTrace::fileName = "";
  17. ofstream * KrTrace::lFile = 0;
  18. KrTrace::traceTarget KrTrace::target = KrTrace::queue;
  19. KrTrace::vvf KrTrace::oldFunction = 0;
  20. _KrException * KrTrace::lastException = 0;
  21. KrTrace::userInfo KrTrace::info = noMsg;
  22.  
  23.  
  24. void KrTrace::traceTo (const IString & s)
  25. {
  26.   if (target==logFile && fileName==s)
  27.      return;                                     // Nothing to do!
  28.  
  29.   try {
  30.      if (lFile) delete lFile;
  31.      lFile = new ofstream (s, ios::out|ios::app);
  32.      }
  33.   catch (...) {
  34.      _KRSYSTHROW (IAccessError("Could not open the file " + s + " for writing."));
  35.      }
  36.  
  37.   HFILE handle = 1;
  38.   APIRET rc = DosDupHandle (lFile->rdbuf()->fd(), &handle);
  39.   if (rc) {
  40.      _KRSYSTHROW (IAccessError("Can't log to the file " + s + " ."));
  41.      }
  42.  
  43.   ITrace::enableTrace ();
  44.   ITrace::writeToStandardOutput ();
  45.  
  46.   target = file;
  47.   fileName = s;
  48. };
  49.  
  50.  
  51. void KrTrace::traceTo (traceTarget aTarget)
  52. {
  53.  
  54.   switch (aTarget) {
  55.  
  56.      case stdout:
  57.         if (file)
  58.            lFile->close();
  59.         ITrace::enableTrace ();
  60.         ITrace::writeToStandardOutput ();
  61.         break;
  62.  
  63.      case stderr:
  64.         if (file)
  65.            lFile->close();
  66.         ITrace::enableTrace ();
  67.         ITrace::writeToStandardError ();
  68.         break;
  69.  
  70.      case file:
  71.         if (fileName=="")
  72.            traceTo ();
  73.         else
  74.            traceTo (fileName);
  75.         return;
  76.  
  77.      case queue:
  78.         if (file)
  79.            lFile->close();
  80.         ITrace::enableTrace ();
  81.         ITrace::writeToQueue ();
  82.         break;
  83.  
  84.      case none:
  85.         ITrace::disableTrace ();
  86.         break;
  87.      }
  88.  
  89.   target = aTarget;
  90.  
  91. };
  92.  
  93.  
  94. void KrTrace::traceTo ()
  95. {
  96.   IString filename = IApplication::current().argv(0);
  97.                                                  // Complete filename of the program
  98.   int i = filename.lastIndexOf ('\\');           // Beyond i is the filename without the path
  99.   int j = filename.lastIndexOf ('.');            // i is the position of the last '.' in the filename
  100.  
  101.   if (j>i)                                       // the filename itself does contain a '.'
  102.      filename = filename.subString (1,j-1);
  103.  
  104.   filename += ".log";
  105.  
  106.   if (filename==".log")                          // Just in case that ICurrentApplication.setArgs
  107.      filename = "errors.log";                    //   has not been called
  108.  
  109.   traceTo (filename);
  110. };
  111.  
  112.  
  113. void KrTrace::tellUser (userInfo aInfo)
  114. {
  115.   if (!oldFunction) {
  116.      oldFunction = set_terminate (KrTrace::_terminate);
  117.      };
  118.   info = aInfo;
  119. };
  120.  
  121.  
  122. void KrTrace::_throw (const IException & ex, const char* fileName, const char* functionName,
  123.                 unsigned long lineNumber)
  124. {
  125.   if (lastException) delete lastException;
  126.   lastException = new _KrException (ex,ex.name());
  127.   lastException->addLocation (IExceptionLocation (fileName, functionName, lineNumber));
  128.   lastException->logExceptionData();
  129.   throw (*lastException);
  130. };
  131.  
  132.  
  133. void KrTrace::_throw (const IException & ex, const char* fileName, const char* functionName,
  134.                 unsigned long lineNumber, const IString & group)
  135. {
  136.   if (lastException) delete lastException;
  137.   lastException = new _KrException (ex,ex.name());
  138.   lastException->addLocation (IExceptionLocation (fileName, functionName, lineNumber));
  139.   lastException->setErrorCodeGroup(group);
  140.   lastException->logExceptionData();
  141.   throw (*lastException);
  142. };
  143.  
  144.  
  145. void KrTrace::_terminate ()
  146. {
  147.   IMessageBox box (0);
  148.   const IExceptionLocation * loc;
  149.   box.setTitle ("Internal Error!");
  150.  
  151.   IString s = "Sorry, an internal error occurred. I can't continue.\n";
  152.  
  153.   switch (info)
  154.      {
  155.      case noMsg:
  156.         abort ();
  157.         break;
  158.      case full:
  159.         s+= "Detailed error information:\n";
  160.         if (lastException->textCount())
  161.            s += "  " + IString (lastException->text(0)) + "\n";
  162.         s+= "  Error code: " + IString (lastException->errorCodeGroup()) + " #"
  163.            + IString(lastException->errorId()) +"\n";
  164.         s+= "  Exception class: " + IString(lastException->name()) + "\n";
  165.         loc = lastException->locationAtIndex(0);
  166.         if (loc)
  167.           s+= "  Error location:\n    function: " + IString (loc->functionName()) + "\n    file: "
  168.             + loc->fileName() + ", line" + IString (loc->lineNumber()) + "\n";
  169.      case logFile:
  170.         if (target==file)
  171.            s += "For further information, have a look at the file\n   " + fileName + ".\n";
  172.      case simple:
  173.         s += "Please send a problem report to the author.";
  174.         break;
  175.      };
  176.  
  177.   box.show (s, IMessageBox::catastrophic);
  178. };
  179.  
  180.  
  181.  
  182. _KrException::_KrException (const IException & ex, const IString & aTypeName)
  183.    : IException (ex)
  184. {
  185.   typeName = aTypeName;
  186. };
  187.  
  188.  
  189. const char * _KrException::name () const
  190. {
  191.   return typeName;
  192. };
  193.  
  194.  
  195. IException & _KrException::setErrorCodeGroup (ErrorCodeGroup aErrorGroup)
  196. {
  197.   errorGroup = aErrorGroup;                      // this one will be guaranteed to survive.
  198.   return IException::setErrorCodeGroup (errorGroup);
  199. };
  200.  
  201.