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