/* 
   Netdrive Samba client plugin
   logging functions
   Copyright (C) netlabs.org 2003-2008

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU 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 General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
*/

#define INCL_DOSERRORS
#define INCL_DOS
#include <os2emx.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <process.h>
#include <sys/time.h>

#include "smbwrp.h"
//#include "smbcd.h"
//#include "config.h"

#define SMBCD_MAX_THREADS 250

int debuglevel = 0;
HMTX logmutex = 0;
char *logfile = NULL;


#if 0
int debug_parse_levels(const char *params_str)
{
	return 1;
}

void dbgflush( void )
{
//	if (!logfile) fflush(stdout);
}
#endif

int debuglvl(int level)
{
	return (level <= debuglevel) ? 1 : 0;
}

int debugheader(int level, char * file, char * func, int line)
{
	int rc;
	if (level > debuglevel)
	{
		return 0;
	}
	rc = DosRequestMutexSem(logmutex, -1L);
	if (rc)
	{
		return 0;
	}
	rc = 1;
	do
	{
		FILE * f;
		struct timeval tv;
		time_t t;
		char buf[80] = {0};
		if (logfile)
		{
			f = fopen(logfile, "a");
			if (!f)
			{
				rc = 0;
				break;
			}
		}
		else
		{
			f = stdout;
		}
		gettimeofday(&tv, NULL);
		t = time(NULL);
//		strftime(buf,sizeof(buf)-1,"%Y/%m/%d %H:%M:%S", localtime((time_t *)&tv.tv_sec));
		strftime(buf,sizeof(buf)-1,"%Y/%m/%d %H:%M:%S", localtime(&t));
		fprintf(f, "%s.%d: %d %d: %s:%s(%d) :", buf, tv.tv_usec / 10000, level, (long)_gettid(), file, func, line);
		if (logfile)
		{
			fclose(f);
		}
	}
	while (0);
	DosReleaseMutexSem(logmutex);
	return rc;
}

int debugmessage(char * fmt, ...)
{
	int rc;
	rc = DosRequestMutexSem(logmutex, -1L);
	if (rc)
	{
		return 0;
	}
	do
	{
		FILE * f;
		va_list args;
		if (logfile)
		{
			f = fopen(logfile, "a");
			if (!f)
			{
				break;
			}
		}
		else
		{
			f = stdout;
		}
		va_start(args, fmt);
		vfprintf(f, fmt, args);
		va_end(args);
		if (logfile)
		{
			fclose(f);
		}
	}
	while (0);
	DosReleaseMutexSem(logmutex);
	return 0;
}

void debuglocal(int level, const char * fmt, ...)
{
	int rc;
	if (level > debuglevel)
	{
		return;
	}
	rc = DosRequestMutexSem(logmutex, -1L);
	if (rc)
	{
		return;
	}
	do
	{
		FILE * f;
		struct timeval tv;
		char buf[80] = {0};
		va_list args;
		if (logfile)
		{
			f = fopen(logfile, "a");
			if (!f)
			{
				break;
			}
		}
		else
		{
			f = stdout;
		}
		gettimeofday(&tv, NULL);
		strftime(buf,sizeof(buf)-1,"%Y/%m/%d %H:%M:%S", localtime((time_t *)&tv.tv_sec));
		fprintf(f, "%s.%d: %d %d: ", buf, tv.tv_usec / 10000, level, (long)_gettid());
		va_start(args, fmt);
		vfprintf(f, fmt, args);
		va_end(args);
		if (logfile)
		{
			fclose(f);
		}
	}
	while (0);
	DosReleaseMutexSem(logmutex);
}


