/*
 * $Header: /home/gene/library/website/docsrc/sca/src/RCS/debug.c,v 395.1 2008/04/20 17:25:48 gene Exp $
 *
 *  Copyright (c) 2006 Gene Michael Stover.  All rights reserved.
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 *  USA
 */

#include "this.h"

Boolean
DEBUG_IsVerbose () {
  return TRUE;
}

void
DEBUG_Print (char file[], int line, char fmt[], ...)
{
  va_list ap;

  if (DEBUG_IsVerbose ()) {
    fprintf (stderr, "\n%s:%d:",
	     strrchr (file, '\\') ? strrchr (file, '\\') : file,
	     line);
    va_start (ap, fmt);
    vfprintf (stderr, fmt, ap);
    va_end (ap);
  }
}

void
DEBUG_PrintLastError (char file[], int line, char moo[])
{
#if IS_UNIX
  DEBUG_Print (file, line, " %s: %s", moo, strerror (errno));
#endif

#if IS_WINDERS
  DWORD           flags;
  LPVOID          lpMsgBuf;

  if (DEBUG_IsVerbose ()) {
    flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS;
    if (FormatMessage (flags, NULL, GetLastError (),
		       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		       (LPTSTR) & lpMsgBuf, 0, NULL)) {
      DEBUG_Print (file, line, " %s: %s", moo, lpMsgBuf);
      LocalFree (lpMsgBuf);
    }
  }
#endif
}

/* --- end of file --- */
