
#include "V.h"
#include "sys_varargs.h"
	extern int  dbn_write;
	extern Flag VshowLvl;
	extern Flag Vshowtim;

/*
* Print a debug message, add a newline, and do a fflush.  This function is
* mostly invoked via the D* functions defined in V.h, for example:
*   V3 "bar(%d) called with x=%d y=%d",k,x,y V;
* will produce the message if Vlvl >= 3.  Note that we have written this
* routine so that it will work with or without the varargs package,  which
* still isn't quite universal in all C libraries.  And even if it's there,
* it is one of the major sources of portability problems, so you might not
* want to use it.
*/
/*VARARGS*/
#if USE_varargs
Vdmsg(dp,l,fmt,va_alist)
	int  l;
	V_f*dp;
	char*fmt;
	va_dcl
{	va_list argp;
#else			/* Don't use varags package */
Vdmsg(dp,l,fmt,ARGLIST)
	int  l;
	V_f*dp;
	char*fmt;
	long ARGLIST;
{
#endif			/* USE_varargs */
	int r=0, savl, savlvl;
#if defined(USE_pthreads) && (USE_pthreads > 0)
	if (V_locking && use_pthreads) {
		pthread_lock_global_np();

	}
#endif /*USE_pthreads*/
	savlvl = Vlvl;
	if (!Vout) {
		if (savlvl>4) FP stderr,"### Vdmsg called with Vout null Vlvl=%d ###\n",Vlvl P;
		return r;
	}
	Vlvl = 0;		/* Try not to debug the debug messages! */










	if (!stdiofl && Fileno(Vout)<3)
		return r;		/* If not allowed to use stdio files */
	if (VshowLvl) fprintf(Vout,"%d ",l);
	if (Vshowtim) fprintf(Vout,"%s ",Vtime());
	V_df(((Vfshow<0)?savlvl:Vfshow),dp,':');
	fputc('\t',Vout);
#if USE_varargs
    va_start(argp);
    vfprintf(Vout,fmt,argp);
    va_end(argp);
#else					/* Don't use varags package */
	fprintf(Vout,fmt,ARGLIST);
#endif					/* USE_varargs */
	fprintf(Vout,"\n");
	fflush(Vout);		/* This shouldn't be necessary, but ... */
#ifndef USE_syscall		/* If defined, write() does the increment */
	++dbn_write;
#endif
	if (Vsleep>0) sleep(Vsleep);
	Vlvl = savlvl;
#if defined(USE_pthreads) && (USE_pthreads > 0)
	if (V_locking && use_pthreads) {

		pthread_unlock_global_np();
	}
#endif /*USE_pthreads*/
	return r;
}

/*
* Here is the routine to generate up to  l  names  from  the  function
* stack. Note that they are written to Vout one at a time. Thus it's
* not a real good idea to make Vout unbuffered; it's better  to  use
* the above fflush() to force the write at the end of a line.
*/
void V_df(l,dp,c)
	int  l;
	V_f*dp;
	char c;
{
extern V_f Vfct;
	if (dp) {
		if (dp->list && (--l > 0))
			V_df(l,dp->list,':');
		if (dp->name)
			fputs(dp->name,Vout);
#ifdef USE__LINE__
		if (dp->line)
			fprintf(Vout,".%d",dp->line);
#endif
	} elsif (Vfct.name) {
		fputs(Vfct.name,Vout);
	}
	fputc(c,Vout);
}

