/contrib/fgtracker/fgt_error.c

https://gitlab.com/fgear-fgms/wlbraggs-f-jjths-fgms-0-x · C · 212 lines · 164 code · 21 blank · 27 comment · 15 complexity · 536b2b3e6c837a013a4a286552ef464c MD5 · raw file

  1. /*
  2. * fgt_error.c - error handling functions
  3. *
  4. * Author: Gabor Toth <tgbp@freemail.hu>
  5. * License: GPL
  6. *
  7. * $Log: error.c,v $
  8. * Revision 1.2 2006/05/10 21:22:34 koversrac
  9. * Comment with author and license has been added.
  10. *
  11. */
  12. #include "fgt_common.h"
  13. #include "fgt_error.h"
  14. #include <stdarg.h> /* ANSI C header file */
  15. #ifdef _MSC_VER
  16. #define LOG_INFO 1
  17. #define LOG_ERR 2
  18. #define snprintf _snprintf
  19. #else // !_MSC_VER
  20. #include <syslog.h> /* for syslog() */
  21. #endif // _MSC_VER y/n
  22. int daemon_proc = 0; /* set nonzero by server daemon_init() */
  23. void pgm_exit(int val)
  24. {
  25. #if defined(_MSC_VER) && !defined(NDEBUG)
  26. int c;
  27. printf("Enter a key to exit...: ");
  28. c = getchar();
  29. #endif // _MSC_VER an !NDEBUG
  30. exit(val);
  31. }
  32. #ifdef _MSC_VER
  33. int syslog(int lev, char *fmt, ...)
  34. {
  35. char buf[MAXLINE + 1];
  36. va_list ap;
  37. va_start(ap, fmt);
  38. #ifdef HAVE_VSNPRINTF
  39. vsnprintf(buf, MAXLINE, fmt, ap); * safe */
  40. #else
  41. vsprintf(buf, fmt, ap); /* not safe */
  42. #endif
  43. strcat(buf, "\n");
  44. fflush(stdout); /* in case stdout and stderr are the same */
  45. fputs(buf, stderr);
  46. fflush(stderr);
  47. va_end(ap);
  48. return lev;
  49. }
  50. // get a message from the system for this error value
  51. char *get_message_text( int err )
  52. {
  53. LPSTR ptr = 0;
  54. DWORD fm = FormatMessage(
  55. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  56. FORMAT_MESSAGE_FROM_SYSTEM |
  57. FORMAT_MESSAGE_IGNORE_INSERTS,
  58. NULL,
  59. err,
  60. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  61. (LPSTR)&ptr,
  62. 0,
  63. NULL );
  64. if (ptr) {
  65. size_t len = strlen(ptr);
  66. while(len--) {
  67. if (ptr[len] > ' ')
  68. break;
  69. ptr[len] = 0;
  70. }
  71. if (len)
  72. return ptr;
  73. LocalFree(ptr);
  74. }
  75. return NULL;
  76. }
  77. #endif // _MSC_VER
  78. /* Nonfatal error related to system call
  79. * Print message and return */
  80. void
  81. err_ret(const char *fmt, ...)
  82. {
  83. va_list ap;
  84. va_start(ap, fmt);
  85. err_doit(1, LOG_INFO, fmt, ap);
  86. va_end(ap);
  87. return;
  88. }
  89. /* Fatal error related to system call
  90. * Print message and terminate */
  91. void
  92. err_sys(const char *fmt, ...)
  93. {
  94. va_list ap;
  95. va_start(ap, fmt);
  96. err_doit(1, LOG_ERR, fmt, ap);
  97. va_end(ap);
  98. pgm_exit(1);
  99. }
  100. /* Fatal error related to system call
  101. * Print message, dump core, and terminate */
  102. void
  103. err_dump(const char *fmt, ...)
  104. {
  105. va_list ap;
  106. va_start(ap, fmt);
  107. err_doit(1, LOG_ERR, fmt, ap);
  108. va_end(ap);
  109. abort(); /* dump core and terminate */
  110. exit(1); /* shouldn't get here */
  111. }
  112. /* Nonfatal error unrelated to system call
  113. * Print message and return */
  114. void
  115. err_msg(const char *fmt, ...)
  116. {
  117. va_list ap;
  118. va_start(ap, fmt);
  119. err_doit(0, LOG_INFO, fmt, ap);
  120. va_end(ap);
  121. return;
  122. }
  123. /* Fatal error unrelated to system call
  124. * Print message and terminate */
  125. void
  126. err_quit(const char *fmt, ...)
  127. {
  128. va_list ap;
  129. va_start(ap, fmt);
  130. err_doit(0, LOG_ERR, fmt, ap);
  131. va_end(ap);
  132. pgm_exit(1);
  133. }
  134. /* Print message and return to caller
  135. * Caller specifies "errnoflag" and "level" */
  136. void
  137. err_doit(int errnoflag, int level, const char *fmt, va_list ap)
  138. {
  139. int errno_save, n;
  140. char buf[MAXLINE + 1];
  141. errno_save = errno; /* value caller might want printed */
  142. #ifdef HAVE_VSNPRINTF
  143. vsnprintf(buf, MAXLINE, fmt, ap); * safe */
  144. #else
  145. vsprintf(buf, fmt, ap); /* not safe */
  146. #endif
  147. n = strlen(buf);
  148. if (errnoflag) {
  149. #ifdef _MSC_VER
  150. // most likely came from a socket function error
  151. int err = WSAGetLastError();
  152. if (err) { // got a socket error
  153. char *ptr = get_message_text(err);
  154. if (ptr) {
  155. snprintf(buf + n, MAXLINE - n, ": %s", ptr);
  156. LocalFree(ptr);
  157. } else {
  158. snprintf(buf + n, MAXLINE - n, ": Got socket err %d", err);
  159. }
  160. } else if (errno_save) {
  161. snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
  162. }
  163. #else // _MSC_VER
  164. snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
  165. #endif // _MSC_VER y/n
  166. }
  167. strcat(buf, "\n");
  168. if (daemon_proc) {
  169. syslog(level, "%s", buf);
  170. } else {
  171. fflush(stdout); /* in case stdout and stderr are the same */
  172. fputs(buf, stderr);
  173. fflush(stderr);
  174. }
  175. return;
  176. }
  177. void
  178. debug(int level,char *str)
  179. {
  180. char buf[MAXLINE+1];
  181. if (level<=DEBUG_LEVEL)
  182. {
  183. //printf("%s\n",str);
  184. snprintf(buf,MAXLINE,"%s",str);
  185. if (daemon_proc)
  186. syslog(level, "%s", buf);
  187. else
  188. printf("%s\n",str);
  189. }
  190. }
  191. /* eof - fgt_error.c */