home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / sos3-2.lha / src / err / err.c next >
Encoding:
C/C++ Source or Header  |  1992-01-23  |  3.9 KB  |  163 lines

  1. /* --------------------------------------------------------------------------
  2.  * Copyright 1992 by Forschungszentrum Informatik (FZI)
  3.  *
  4.  * You can use and distribute this software under the terms of the licence
  5.  * you should have received along with this program.
  6.  * If not or if you want additional information, write to
  7.  * Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
  8.  * D-7500 Karlsruhe 1, Germany.
  9.  * --------------------------------------------------------------------------
  10.  */
  11. // **************************************************************************
  12. // Module err                                         Bernhard Schiefer (bs)
  13. //                                                           Juergen Uhl (ju
  14. //                                                      Dietmar Theobald (dt)
  15. // **************************************************************************
  16. // SOS error handler               
  17. // **************************************************************************
  18.  
  19. #include <stream.h>
  20. #include <libc.h>
  21. #include <string.h>
  22. #include "trc_err.h"
  23. #include "sys.h"
  24. #include "err.h"
  25.  
  26. void (*err_output_handler)() = NULL;
  27.  
  28. const err_msg err_NOT_IMPLEMENTED="attempt to execute not implemented function";
  29. const err_msg err_ABSTRACT_METHOD="attempt to execute an abstract method";
  30. const err_msg err_ASSERTION_FAILED="assertion failed";
  31.  
  32. static int err_sys_sum = 0;
  33. static int err_use_sum = 0;
  34. static int err_wng_sum = 0;
  35.  
  36. static err_class last_class   = err_SYS;
  37. static err_msg   last_raised  = NULL;
  38. static char      *last_origin = NULL;
  39.  
  40. class err_buffer
  41. {  char *buf;
  42.    int  size;
  43.  
  44.  public:
  45.    err_buffer() { buf = NULL; size = 0; }
  46.  
  47.    char *buffer_string (char *str);
  48. };
  49.  
  50. static err_buffer err_raised_buffer;
  51. static err_buffer err_origin_buffer;
  52.  
  53. err_env_stc *err_env_stack;
  54.  
  55. // --------------------------------------------------------------------------
  56.  
  57. char *err_buffer::buffer_string (char *str)
  58. {  if (str)
  59.    {  int strsize = strlen (str) + 1;
  60.  
  61.       if (strsize > size)
  62.       {  if (buf)
  63.         delete buf;
  64.      buf  = new char[strsize];
  65.      size = strsize;
  66.       }
  67.       return strcpy (buf, str);
  68.    }
  69.    else
  70.       return NULL;
  71. }
  72.  
  73. void err_raise (err_class ec, err_msg msg, char* where, int copy)
  74. {
  75.    T_PROC ("err_raise");
  76.    TT (err_H, T_ENTER; TI (ec));
  77.  
  78.    if (copy)
  79.    {  last_raised = err_raised_buffer.buffer_string (msg);
  80.       last_origin = err_origin_buffer.buffer_string (where);
  81.    }
  82.    else
  83.    {  last_raised = msg;
  84.       last_origin = where;
  85.    }
  86.    last_class = ec;
  87.  
  88.    if (ec != err_SYS  OR  NOT err_env_stack)
  89.       if (err_output_handler)
  90.      (*err_output_handler)();
  91.       else
  92.       {  
  93.      cerr << ((ec == err_WNG) ? " Warning "
  94.                   : " ERROR ");
  95.      if (where)
  96.      {
  97.         cerr << ((ec == err_WNG) ? "for "
  98.                      : "in ")
  99.              << where << " ";
  100.      }
  101.      cerr << ": " << msg << "\n";
  102.      cerr.flush();
  103.       }
  104.  
  105.    switch (ec)
  106.    {
  107.       case err_SYS : err_sys_sum++;
  108.              if (err_env_stack)
  109.             siglongjmp (err_env_stack->env, 1);
  110.              else
  111.             abort();
  112.       case err_USE : err_use_sum++; break;
  113.       case err_WNG : err_wng_sum++; break;
  114.    }  // switch
  115.  
  116.    TT (err_H, T_LEAVE);
  117. }
  118.  
  119. err_class err_last_class ()
  120. {  return last_class;
  121. }
  122.  
  123. err_msg err_last_raised ()
  124. {  return last_raised;
  125. }
  126.  
  127. err_msg err_last_origin ()
  128. {  return last_origin;
  129. }
  130.  
  131. int err_occurred (err_class ec)
  132. {
  133.    T_PROC ("err_occurred");
  134.    TT (err_H, T_ENTER; TI (ec));
  135.  
  136.    switch (ec)
  137.    {
  138.       case err_SYS : TT (err_H, T_LEAVE; TI (err_sys_sum)); return err_sys_sum;
  139.       case err_USE : TT (err_H, T_LEAVE; TI (err_use_sum)); return err_use_sum;
  140.       case err_WNG : TT (err_H, T_LEAVE; TI (err_wng_sum)); return err_wng_sum;
  141.    }  // switch
  142. }
  143.  
  144. void err_reset ()
  145. {  err_sys_sum = 0;
  146.    err_use_sum = 0;
  147.    err_wng_sum = 0;
  148. }
  149.  
  150. void err_push_env()
  151. {  err_env_stc *es = new err_env_stc;
  152.  
  153.    es->next      = err_env_stack;
  154.    err_env_stack = es;
  155. }
  156.  
  157. void err_pop_env()
  158. {  err_env_stc *es = err_env_stack;
  159.  
  160.    err_env_stack = err_env_stack->next;
  161.    delete es;
  162. }
  163.