Using glog's design for Phobos?

Robert M. Münch robert.muench at robertmuench.de
Sat Aug 28 02:33:45 PDT 2010


On 2010-08-27 03:50:43 +0200, Yao G. said:

> Using printf / writeln is perfectly fine, until you start to program in 
> Windows, where there's no command line to display the text. Here's 
> where I think a small logging library comes handy. I did a small one 
> that logs to a file, and a variant that display the log messages to a 
> independent window.

void odprintf(const char *format, ...){
	char    buf[4096], *p = buf;
	va_list args;
	int     n;

  va_start(args, format);
  #ifdef WIN32
  n = _vsnprintf(p, sizeof buf - 3, format, args); // buf-3 is room for 
CR/LF/NUL
  #else
  n = vsnprintf(p, sizeof buf - 3, format, args); // buf-3 is room for 
CR/LF/NUL
  #endif
  va_end(args);

  p += (n < 0) ? sizeof buf - 3 : n;

  while ( p > buf  &&  isspace(p[-1]) )
          *--p = '\0';

  *p++ = '\r';
  *p++ = '\n';
  *p   = '\0';

  #ifdef WIN32
  OutputDebugString(buf);
  #else
  printf("%s", buf);
  #endif
}

Should do the trick and than sue something like debugview from 
ex-sysinternals. Works very well.

-- 
Robert M. Münch
http://www.robertmuench.de



More information about the Digitalmars-d mailing list