error.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. /*
  11. * standalone mini error implementation
  12. */
  13. #include <ast/ast.h>
  14. #include <ast/error.h>
  15. #include <stddef.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <util/exit.h>
  20. Error_info_t error_info;
  21. void setErrorLine (int line) { error_info.line = line; }
  22. void setErrorFileLine (char* src, int line) {
  23. error_info.file = src;
  24. error_info.line = line;
  25. }
  26. void setErrorId (char* id) { error_info.id = id; }
  27. void setErrorErrors (int errors) { error_info.errors = errors; }
  28. int getErrorErrors (void) { return error_info.errors; }
  29. void setTraceLevel (int i) { error_info.trace = i; }
  30. void errorv(const char *id, int level, const char *s, va_list ap)
  31. {
  32. int flags;
  33. if (level < error_info.trace) return;
  34. if (level < 0)
  35. flags = 0;
  36. else {
  37. flags = level & ~ERROR_LEVEL;
  38. level &= ERROR_LEVEL;
  39. }
  40. const char *prefix;
  41. if (level && ((prefix = error_info.id) || (prefix = id))) {
  42. if (flags & ERROR_USAGE)
  43. fprintf(stderr, "Usage: %s ", prefix);
  44. else
  45. fprintf(stderr, "%s: ", prefix);
  46. }
  47. if (flags & ERROR_USAGE)
  48. /*nop */ ;
  49. else if (level < 0) {
  50. int i;
  51. for (i = 0; i < error_info.indent; i++)
  52. fprintf(stderr, " ");
  53. fprintf(stderr, "debug%d: ", level);
  54. } else if (level) {
  55. if (level == ERROR_WARNING) {
  56. fprintf(stderr, "warning: ");
  57. error_info.warnings++;
  58. } else {
  59. error_info.errors++;
  60. if (level == ERROR_PANIC)
  61. fprintf(stderr, "panic: ");
  62. }
  63. if (error_info.line) {
  64. if (error_info.file && *error_info.file)
  65. fprintf(stderr, "\"%s\", ", error_info.file);
  66. fprintf(stderr, "line %d: ", error_info.line);
  67. }
  68. }
  69. vfprintf(stderr, s, ap);
  70. if (flags & ERROR_SYSTEM)
  71. fprintf(stderr, "\n%s", strerror(errno));
  72. fprintf(stderr, "\n");
  73. if (level >= ERROR_FATAL)
  74. graphviz_exit(level - ERROR_FATAL + 1);
  75. }
  76. void error(int level, const char *s, ...)
  77. {
  78. va_list ap;
  79. va_start(ap, s);
  80. errorv(NULL, level, s, ap);
  81. va_end(ap);
  82. }
  83. void errorf(void *handle, void *discipline, int level, const char *s, ...)
  84. {
  85. va_list ap;
  86. va_start(ap, s);
  87. errorv((discipline
  88. && handle) ? *((char **) handle) : (char *) handle, level, s, ap);
  89. va_end(ap);
  90. }