/*
** SellaNMS
** $Id: trace.h,v 1.10 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_COMMON_TRACE_H
#define __DGS_COMMON_TRACE_H

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include "defaults.h"
#include <unistd.h>
#include <pthread.h>
#include <syslog.h>
#include <assert.h>

#include <stl/string>
#include <stl/vector>
#include <stl/hash_map>

#define	TRACE_POLICY		0x0000000f
#define	TRACE_POLICY_IN	0x00000001
#define	TRACE_POLICY_OUT	0x00000002
#define	TRACE_POLICY_SQL	0x00000004
#define	TRACE_POLICY_MAP	0x00000008
#define	TRACE_CONFIG		0x00000010
#define	TRACE_SQL			0x00000100
#define	TRACE_THREAD		0x00001000
#define	TRACE_TIMER			0x00010000
#define	TRACE_INTERNAL		0x10000000
#define	TRACE_DEBUG			0x20000000
#define	TRACE_DUMP			0x40000000
#define	TRACE_ALL			0xffffffff

#define	DEBUGLEVEL_0		0x00000000
#define	DEBUGLEVEL_1		(TRACE_DEBUG)
#define	DEBUGLEVEL_2		(TRACE_DEBUG | TRACE_CONFIG)
#define	DEBUGLEVEL_3		(TRACE_DEBUG | TRACE_CONFIG | TRACE_THREAD)
#define	DEBUGLEVEL_4		(TRACE_DEBUG | TRACE_CONFIG | TRACE_THREAD | TRACE_INTERNAL)
#define	DEBUGLEVEL_5		(TRACE_DEBUG | TRACE_CONFIG | TRACE_THREAD | TRACE_INTERNAL | TRACE_TIMER)
#define	DEBUGLEVEL_6		(TRACE_DEBUG | TRACE_CONFIG | TRACE_THREAD | TRACE_INTERNAL | TRACE_TIMER | TRACE_POLICY)
#define	DEBUGLEVEL_7		(TRACE_DEBUG | TRACE_CONFIG | TRACE_THREAD | TRACE_INTERNAL | TRACE_TIMER | TRACE_POLICY | TRACE_SQL)
#define	DEBUGLEVEL_8		(TRACE_DEBUG | TRACE_CONFIG | TRACE_THREAD | TRACE_INTERNAL | TRACE_TIMER | TRACE_POLICY | TRACE_SQL | TRACE_DUMP)
#define	DEBUGLEVEL_9		(TRACE_ALL)

#define	TRACE_INIT __trace_init()
#define	TRACE_FREE __trace_free()
#define	TRACE_SET(x) __set_traceoption(x)
#define	TRACE_UNSET(x) __unset_traceoption(x)
#define	TRACE_ISSET(x) __isset_traceoption(x)
#define	TRACE_SET_SEV(x) __set_severity(x)
#define	TRACE_GET_SEV() __get_severity()

#define	TIME(x) __time(x)
#define	TIME_START(x) __time_start(x)
#define	TIME_STOP(x) __time_stop(x)

#define	LOADWATCH(x) __get_procload(x)

#ifdef USE_DEBUG
	#ifndef DEBUG
		#define DEBUG
	#endif
	#undef NDEBUG
#else
	#define NDEBUG
#endif

#ifdef DEBUG
	#define	TRACE __set_file_line(__FILE__, __LINE__); __trace_debug
#else
	#define	TRACE __trace
#endif

#ifdef DEBUG
	#undef	LMSG
	#define	LMSG __set_file_line(__FILE__, __LINE__); __lmsg_debug
#else
	#undef	LMSG
	#define	LMSG __lmsg
#endif

typedef struct _time_clock_t {
	timeval			start;
	timeval			stop;
} time_clock_t;

bool is_initialized();
void __trace_init();
void __trace_free();
void __trace(unsigned long flags, const char *format, ...);
void __trace_debug(unsigned long flags, const char *format, ...);
void __lmsg(const char *format, ...);
void __lmsg_debug(const char *format, ...);
void __set_traceoption(unsigned long flags);
void __unset_traceoption(unsigned long flags);
bool __isset_traceoption(unsigned long flags);
void __time_start(const char *name);
void __time_stop(const char *name);
void __time(const char *name);
void __set_severity(unsigned long);
unsigned long __get_severity();
unsigned long __get_traceoptions();
void __set_file_line(const char* file, unsigned long line);
double __get_procload(double trigger);

#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/
