C   9

error.c

Guest on 20th July 2021 03:06:44 PM

  1. /* error.c
  2.    Copyright (C)  Eugene K. Ressler, Jr.
  3.  
  4. This file is part of Sketch, a small, simple system for making
  5. 3d drawings with LaTeX and the PSTricks or TikZ package.
  6.  
  7. Sketch is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11.  
  12. Sketch is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Sketch; see the file COPYING.txt.  If not, see
  19. http://www.gnu.org/copyleft */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include "error.h"
  25.  
  26. SRC_LINE no_line = { NULL, 0 };
  27.  
  28. typedef enum error_type_t
  29. {
  30.   E_REMARK,
  31.   E_WARNING,
  32.   E_ERROR,
  33.   E_FATAL_ERROR,
  34.   E_N                           // keep last; this is the number of error types
  35. }
  36. ERROR_TYPE;
  37.  
  38. char *error_type_str[] = {
  39.   "remark",
  40.   "warning",
  41.   "error",
  42.   "fatal",
  43. };
  44.  
  45. static int n_errors[E_N];       // assumes C initialization to zero
  46.  
  47. static void
  48. print_error (ERROR_TYPE type, SRC_LINE line, char *fmt, va_list arg_list)
  49. {
  50.   n_errors[type]++;
  51.   if (line.file_name)
  52.     fprintf (stderr, "%s", line.file_name);
  53.   if (line.number > 0)
  54.     fprintf (stderr, "(%u) : ", line.number);
  55.   fprintf (stderr, "%s, ", error_type_str[type]);
  56.   vfprintf (stderr, fmt, arg_list);
  57.   fprintf (stderr, "\n");
  58. }
  59.  
  60. void
  61. remark (SRC_LINE line, char *fmt, ...)
  62. {
  63.   va_list arg_list;
  64.  
  65.   va_start (arg_list, fmt);
  66.   print_error (E_REMARK, line, fmt, arg_list);
  67.   va_end (arg_list);
  68. }
  69.  
  70. void
  71. warn (SRC_LINE line, char *fmt, ...)
  72. {
  73.   va_list arg_list;
  74.  
  75.   va_start (arg_list, fmt);
  76.   print_error (E_WARNING, line, fmt, arg_list);
  77.   va_end (arg_list);
  78. }
  79.  
  80. void
  81. err (SRC_LINE line, char *fmt, ...)
  82. {
  83.   va_list arg_list;
  84.  
  85.   va_start (arg_list, fmt);
  86.   print_error (E_ERROR, line, fmt, arg_list);
  87.   va_end (arg_list);
  88. }
  89.  
  90. void
  91. die (SRC_LINE line, char *fmt, ...)
  92. {
  93.   va_list arg_list;
  94.  
  95.   va_start (arg_list, fmt);
  96.   print_error (E_FATAL_ERROR, line, fmt, arg_list);
  97.   va_end (arg_list);
  98.   report_errors ();
  99. }
  100.  
  101. int
  102. trouble_p (void)
  103. {
  104.   return n_errors[E_ERROR] > 0 || n_errors[E_FATAL_ERROR] > 0;
  105. }
  106.  
  107. void
  108. report_errors (void)
  109. {
  110.   int i, n_total = 0;
  111.  
  112.   // start printing summary
  113.   fprintf (stderr, "summary: ");
  114.   for (i = 0; i < E_N; i++)
  115.     {
  116.       if (n_errors[i] > 0)
  117.         {
  118.           if (n_total > 0)
  119.             fprintf (stderr, ", ");
  120.           fprintf (stderr, "%d %s", n_errors[i], error_type_str[i]);
  121.           n_total++;
  122.         }
  123.     }
  124.  
  125.   // see if we need to abort
  126.   if (trouble_p ())
  127.     {
  128.       fprintf (stderr, "\nquitting...\n");
  129.       exit (1);
  130.     }
  131.   // not aborting, so finish up the summary
  132.   if (n_total == 0)
  133.     {
  134.       fprintf (stderr, "no errors");
  135.     }
  136.   fprintf (stderr, "\n");
  137.  
  138.   // zero the error counters
  139.   for (i = 0; i < E_N; i++)
  140.     n_errors[i] = 0;
  141. }

Raw Paste


Login or Register to edit or fork this paste. It's free.