httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Sutton <p...@eu.c2.net>
Subject [PATCH] Proper reporting of Win32 errors
Date Mon, 01 Dec 1997 10:47:00 GMT
The current aplog_error() function cannot report on errors returned by
Win32 functions. These functions do not bother setting errno - instead,
you have to make a call to GetLastError() to get the error code, then call
FormatMessage() to get the corresponding string. I already added code to
do this in os/win32/service.c, but this was specific to reporting errors
to standard error during apache -i or -u calls.

The patch below updates aplog_error() to enable generic logging of 
Win32 errors to the same place as other errors. It adds a new flag,
APLOG_WIN32ERROR which if given in the _second_ argument to aplog_error()
causes the Win32 error code and error string to be logged. Here is an
example call (this is from worker_main()):

	if (SetEvent(ev[i]) == 0)
	    aplog_error(APLOG_MARK,APLOG_WIN32ERROR, server_conf,
		"SetEvent for child process in slot #%d", i);

With this we'll be able to remove the asserts that litter the MT code for
Win32 and do proper error reporting.  

Paul

diff -c ../../../apachen/src/main/http_log.c ./http_log.c
*** ../../../apachen/src/main/http_log.c	Mon Nov 10 11:00:08 1997
--- ./http_log.c	Mon Dec  1 10:32:10 1997
***************
*** 319,324 ****
--- 319,367 ----
  	len += ap_snprintf(errstr + len, sizeof(errstr) - len,
  		"(%d)%s: ", save_errno, strerror(save_errno));
      }
+ #ifdef WIN32
+     if (level & APLOG_WIN32ERROR) {
+ 	int nChars;
+ 	int nErrorCode;
+ 
+ 	nErrorCode = GetLastError();
+ 	len += ap_snprintf(errstr + len, sizeof(errstr) - len,
+ 	    "(%d)", nErrorCode);
+ 
+ 	nChars = FormatMessage( 
+ 	    FORMAT_MESSAGE_FROM_SYSTEM,
+ 	    NULL,
+ 	    nErrorCode,
+ 	    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ 	    (LPTSTR) errstr + len,
+ 	    sizeof(errstr) - len,
+ 	    NULL 
+ 	);
+ 	len += nChars;
+ 	if (nChars == 0) {
+ 	    /* Um, error occurred, but we can't recurse to log it again
+ 	     * (and it would probably only fail anyway), so lets just
+ 	     * log the numeric value.
+ 	     */
+ 	    nErrorCode = GetLastError();
+ 	    len += ap_snprintf(errstr + len, sizeof(errstr) - len,
+ 		"(FormatMessage failed with code %d): ", nErrorCode);
+ 	}
+ 	else {
+ 	    /* FormatMessage put the message in the buffer, but it may
+ 	     * have appended a newline (\r\n). So remove it and use
+ 	     * ": " instead like the Unix errors. The error may also
+ 	     * end with a . before the return - if so, trash it.
+ 	     */
+ 	    if (len > 1 && errstr[len-2] == '\r' && errstr[len-1] == '\n') {
+ 		if (len > 2 && errstr[len-3] == '.')
+ 		    len--;
+ 		errstr[len-2] = ':';
+ 		errstr[len-1] = ' ';
+ 	    }
+ 	}
+     }
+ #endif
  
      va_start(args, fmt);
      len += ap_vsnprintf(errstr + len, sizeof(errstr) - len, fmt, args);
diff -c ../../../apachen/src/main/http_log.h ./http_log.h
*** ../../../apachen/src/main/http_log.h	Sat Nov  1 16:30:33 1997
--- ./http_log.h	Mon Dec  1 10:30:51 1997
***************
*** 81,86 ****
--- 81,91 ----
  #endif
  
  #define APLOG_NOERRNO		(APLOG_LEVELMASK + 1)
+ #ifdef WIN32
+ /* Set to indicate that error msg should come from Win32's GetLastError(),
+  * not errno. */
+ #define APLOG_WIN32ERROR	((APLOG_LEVELMASK+1) * 2)
+ #endif
  
  #ifndef DEFAULT_LOGLEVEL
  #define DEFAULT_LOGLEVEL	APLOG_ERR




Mime
View raw message