PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/include/error.h

https://github.com/ayourtch/zf
C Header | 113 lines | 37 code | 21 blank | 55 comment | 1 complexity | ccee1aa4fa5c94bffd601c4bf03b0016 MD5 | raw file
  1. /* error.h declares macros and other facilities for error logging
  2. *
  3. * written wew 2004-06-07
  4. *
  5. */
  6. #ifndef ERROR_H
  7. #define ERROR_H
  8. #include <stdio.h>
  9. /* Variadic macros from C99 */
  10. /*
  11. #define ERROR(format, ...) \
  12. error_loc(-1, __func__, __FILE__, __LINE__, format, ##__VA_ARGS__)
  13. #define ERROR_CODE(code, format, ...) \
  14. error_loc(code, __func__, __FILE__, __LINE__, format, ##__VA_ARGS__)
  15. */
  16. /* C99 provides __func__ as the name of the current function.
  17. Before this, GCC had the extension __FUNCTION__. Otherwise,
  18. we don't attempt to determine it. */
  19. #if __STDC_VERSION__ >= 199901L
  20. #define FUNC_NAME __func__
  21. #elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
  22. #define FUNC_NAME __FUNCTION__
  23. #else
  24. #define FUNC_NAME "(unknown function)"
  25. #endif
  26. /* What C89 provides */
  27. #define ERROR(format) \
  28. error_loc(-1, FUNC_NAME, __FILE__, __LINE__, format)
  29. #define ERROR_CODE(code, format) \
  30. error_loc(code, FUNC_NAME, __FILE__, __LINE__, format)
  31. #define ERROR1(format, arg1) \
  32. error_loc(-1, FUNC_NAME, __FILE__, __LINE__, format, arg1)
  33. #define ERROR_CODE1(code, format, arg1) \
  34. error_loc(code, FUNC_NAME, __FILE__, __LINE__, format, arg1)
  35. #define ERROR2(format, arg1, arg2) \
  36. error_loc(-1, FUNC_NAME, __FILE__, __LINE__, format, arg1, arg2)
  37. #define ERROR_CODE2(code, format, arg1, arg2) \
  38. error_loc(code, FUNC_NAME, __FILE__, __LINE__, format, arg1, arg2)
  39. #define ERROR3(format, arg1, arg2, arg3) \
  40. error_loc(-1, FUNC_NAME, __FILE__, __LINE__, format, arg1, arg2, arg3)
  41. #define ERROR_CODE3(code, format, arg1, arg2, arg3) \
  42. error_loc(code, FUNC_NAME, __FILE__, __LINE__, format, arg1, arg2, arg3)
  43. #define ERROR4(format, arg1, arg2, arg3, arg4) \
  44. error_loc(-1, FUNC_NAME, __FILE__, __LINE__, format, arg1, arg2, arg3, arg4)
  45. #define ERROR_CODE4(code, format, arg1, arg2, arg3) \
  46. error_loc(code, FUNC_NAME, __FILE__, __LINE__, format, arg1, arg2, arg3, arg4)
  47. /*
  48. * Is there an error to report?.
  49. */
  50. int error_has_msg();
  51. /*
  52. * Get the last error message as set by error_loc()/ERROR().
  53. */
  54. const char * error_last_msg();
  55. /*
  56. * Get the last error code as set by error_loc()/ERROR().
  57. */
  58. int error_last_code();
  59. /*
  60. * Set the stream that errors are logged to.
  61. *
  62. * Setting to NULL stops errors being logged.
  63. *
  64. * By default, errors are not logged, unless the LOGERRORS
  65. * macros is defined at compile time, in which case they go
  66. * to stderr.
  67. */
  68. void error_set_log_stream(FILE * stream);
  69. /*
  70. * Record an error message.
  71. *
  72. * The error message will have the form
  73. * "ERROR: <func> (<file>::<line>): <msg> [(system error is "<syserr>")].
  74. * This message is retrievable by error_last_msg(). It will also be
  75. * logged to the stream set by error_set_stream() (unless this is
  76. * NULL).
  77. *
  78. * This function is most conveniently called via the ERROR macro.
  79. *
  80. * @param code the code for this error.
  81. * @param func the name of the function this error occurs in.
  82. * @param file the name of the file this error occurs in.
  83. * @param line the line of this file for this error message.
  84. * @param fmt user-defined format. It is not necessary to precede
  85. * it with "ERROR", or put a trailing newline.
  86. *
  87. * @return the error code, for convenience.
  88. */
  89. int error_loc(int code, const char * func, const char * file, int line,
  90. const char * fmt, ...);
  91. #endif