diff -cpr head/src/backend/utils/error/elog.c eventlog/src/backend/utils/error/elog.c
*** head/src/backend/utils/error/elog.c	Tue Jul  7 16:06:35 2009
--- eventlog/src/backend/utils/error/elog.c	Mon Aug 17 10:49:42 2009
*************** static int	syslog_facility = LOG_LOCAL0;
*** 111,116 ****
--- 111,118 ----
  static void write_syslog(int level, const char *line);
  #endif
  
+ static void write_console(const char *line, int len);
+ 
  #ifdef WIN32
  static void write_eventlog(int level, const char *line);
  #endif
*************** write_eventlog(int level, const char *li
*** 1606,1613 ****
  			break;
  	}
  
! 
! 	ReportEvent(evtHandle,
  				eventlevel,
  				0,
  				0,				/* All events are Id 0 */
--- 1608,1616 ----
  			break;
  	}
  
! 	if (GetDatabaseEncoding() == GetPlatformEncoding())
! 	{
! 		ReportEventA(evtHandle,
  				eventlevel,
  				0,
  				0,				/* All events are Id 0 */
*************** write_eventlog(int level, const char *li
*** 1616,1624 ****
--- 1619,1696 ----
  				0,
  				&line,
  				NULL);
+ 	}
+ 	else
+ 	{
+ 		char	   *utf8;
+ 		WCHAR	   *utf16;
+ 		int			len;
+ 
+ 		len = strlen(line);
+ 		utf8 = (char *) pg_do_encoding_conversion((unsigned char *) line,
+ 										len, GetDatabaseEncoding(), PG_UTF8);
+ 		if (utf8 != line)
+ 			len = strlen(utf8);
+ 
+ 		utf16 = (WCHAR *) palloc(sizeof(WCHAR) * (len + 1));
+ 		len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, len);
+ 		utf16[len] = L'\0';
+ 
+ 		ReportEventW(evtHandle,
+ 				eventlevel,
+ 				0,
+ 				0,				/* All events are Id 0 */
+ 				NULL,
+ 				1,
+ 				0,
+ 				(LPCWSTR *) &utf16,
+ 				NULL);
+ 
+ 		if (utf8 != line)
+ 			pfree(utf8);
+ 		pfree(utf16);
+ 	}
  }
  #endif   /* WIN32 */
  
+ static void
+ write_console(const char *line, int len)
+ {
+ 	if (GetDatabaseEncoding() == GetPlatformEncoding())
+ 		write(fileno(stderr), line, len);
+ 	else
+ 	{
+ #ifdef WIN32
+ 		char	   *utf8;
+ 		WCHAR	   *utf16;
+ 		HANDLE		stderrHandle;
+ 		DWORD		written;
+ 
+ 		utf8 = (char *) pg_do_encoding_conversion((unsigned char *) line,
+ 										len, GetDatabaseEncoding(), PG_UTF8);
+ 
+ 		if (utf8 != line)
+ 			len = strlen(utf8);
+ 		utf16 = (WCHAR *) palloc(sizeof(WCHAR) * len);
+ 		len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, len);
+ 
+ 		stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
+ 		WriteConsoleW(stderrHandle, utf16, len, &written, NULL);
+ 
+ 		if (utf8 != line)
+ 			pfree(utf8);
+ 		pfree(utf16);
+ #else
+ 		/*
+ 		 * Conversion on non-win32 platform is not implemented yet.
+ 		 * It requires non-throw version of pg_do_encoding_conversion(),
+ 		 * that converts unconvertable characters to '?' without errors.
+ 		 */
+ 		write(fileno(stderr), line, len);
+ #endif
+ 	}
+ }
+ 
  /*
   * setup formatted_log_time, for consistent times between CSV and regular logs
   */
*************** send_message_to_server_log(ErrorData *ed
*** 2233,2239 ****
  			write_eventlog(edata->elevel, buf.data);
  #endif
  		else
! 			write(fileno(stderr), buf.data, buf.len);
  	}
  
  	/* If in the syslogger process, try to write messages direct to file */
--- 2305,2311 ----
  			write_eventlog(edata->elevel, buf.data);
  #endif
  		else
! 			write_console(buf.data, buf.len);
  	}
  
  	/* If in the syslogger process, try to write messages direct to file */
*************** send_message_to_server_log(ErrorData *ed
*** 2256,2267 ****
  		{
  			const char *msg = _("Not safe to send CSV data\n");
  
! 			write(fileno(stderr), msg, strlen(msg));
  			if (!(Log_destination & LOG_DESTINATION_STDERR) &&
  				whereToSendOutput != DestDebug)
  			{
  				/* write message to stderr unless we just sent it above */
! 				write(fileno(stderr), buf.data, buf.len);
  			}
  			pfree(buf.data);
  		}
--- 2328,2339 ----
  		{
  			const char *msg = _("Not safe to send CSV data\n");
  
! 			write_console(msg, strlen(msg));
  			if (!(Log_destination & LOG_DESTINATION_STDERR) &&
  				whereToSendOutput != DestDebug)
  			{
  				/* write message to stderr unless we just sent it above */
! 				write_console(buf.data, buf.len);
  			}
  			pfree(buf.data);
  		}
*************** void
*** 2642,2647 ****
--- 2714,2722 ----
  write_stderr(const char *fmt,...)
  {
  	va_list		ap;
+ #ifdef WIN32
+ 	char		errbuf[2048];		/* Arbitrary size? */
+ #endif
  
  	fmt = _(fmt);
  
*************** write_stderr(const char *fmt,...)
*** 2651,2656 ****
--- 2726,2732 ----
  	vfprintf(stderr, fmt, ap);
  	fflush(stderr);
  #else
+ 	vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
  
  	/*
  	 * On Win32, we print to stderr if running on a console, or write to
*************** write_stderr(const char *fmt,...)
*** 2658,2673 ****
  	 */
  	if (pgwin32_is_service())	/* Running as a service */
  	{
- 		char		errbuf[2048];		/* Arbitrary size? */
- 
- 		vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
- 
  		write_eventlog(ERROR, errbuf);
  	}
  	else
  	{
  		/* Not running as service, write to stderr */
! 		vfprintf(stderr, fmt, ap);
  		fflush(stderr);
  	}
  #endif
--- 2734,2745 ----
  	 */
  	if (pgwin32_is_service())	/* Running as a service */
  	{
  		write_eventlog(ERROR, errbuf);
  	}
  	else
  	{
  		/* Not running as service, write to stderr */
! 		write_console(errbuf, strlen(errbuf));
  		fflush(stderr);
  	}
  #endif
diff -cpr head/src/backend/utils/mb/mbutils.c eventlog/src/backend/utils/mb/mbutils.c
*** head/src/backend/utils/mb/mbutils.c	Thu Jul  9 11:16:13 2009
--- eventlog/src/backend/utils/mb/mbutils.c	Mon Aug 17 10:49:42 2009
*************** static FmgrInfo *ToClientConvProc = NULL
*** 58,63 ****
--- 58,64 ----
   */
  static pg_enc2name *ClientEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
  static pg_enc2name *DatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
+ static pg_enc2name *PlatformEncoding = NULL;
  
  /*
   * During backend startup we can't set client encoding because we (a)
*************** pg_client_encoding(PG_FUNCTION_ARGS)
*** 977,980 ****
--- 978,989 ----
  {
  	Assert(ClientEncoding);
  	return DirectFunctionCall1(namein, CStringGetDatum(ClientEncoding->name));
+ }
+ 
+ int
+ GetPlatformEncoding(void)
+ {
+ 	if (PlatformEncoding == NULL)
+ 		PlatformEncoding = &pg_enc2name_tbl[pg_get_encoding_from_locale("")];
+ 	return PlatformEncoding->encoding;
  }
diff -cpr head/src/include/mb/pg_wchar.h eventlog/src/include/mb/pg_wchar.h
*** head/src/include/mb/pg_wchar.h	Fri Jun 12 09:52:43 2009
--- eventlog/src/include/mb/pg_wchar.h	Mon Aug 17 10:49:42 2009
*************** extern const char *pg_get_client_encodin
*** 402,407 ****
--- 402,408 ----
  extern void SetDatabaseEncoding(int encoding);
  extern int	GetDatabaseEncoding(void);
  extern const char *GetDatabaseEncodingName(void);
+ extern int	GetPlatformEncoding(void);
  extern void pg_bind_textdomain_codeset(const char *domainname);
  
  extern int	pg_valid_client_encoding(const char *name);