SmaugWizard/Backup/
SmaugWizard/Backup/L/
SmaugWizard/Boards/
SmaugWizard/Building/
SmaugWizard/Corpses/
SmaugWizard/Councils/
SmaugWizard/Deity/
SmaugWizard/Gods/
SmaugWizard/MudProgs/
SmaugWizard/Player/L/
SmaugWizard/Src/
SmaugWizard/Src/res/
/****************************************************************************
 * [S]imulated [M]edieval [A]dventure multi[U]ser [G]ame      |				*
 * -----------------------------------------------------------|   \\._.//	*
 * SmaugWiz (C) 1998 by Russ Pillsbury (Windows NT version)   |   (0...0)	*
 * -----------------------------------------------------------|    ).:.(	*
 * SMAUG (C) 1994, 1995, 1996 by Derek Snider                 |    {o o}	*
 * -----------------------------------------------------------|   / ' ' \	*
 * SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus,      |~'~.VxvxV.~'~*
 * Scryn, Swordbearer, Rennard, Tricops, and Gorog.           |				*
 * ------------------------------------------------------------------------ *
 * Merc 2.1 Diku Mud improvments copyright (C) 1992, 1993 by Michael        *
 * Chastain, Michael Quan, and Mitchell Tse.                                *
 * Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,          *
 * Michael Seifert, Hans Henrik Staerfeldt, Tom Madsen, and Katja Nyboe.    *
 * ------------------------------------------------------------------------ *
 * 			Logging Interface												*
 ****************************************************************************/

#include	"stdafx.h"
#include	"smaug.h"
#include	"Smaugx.h"
#include	"SysData.h"
#include	"log.h"
#include	"SmaugWizDoc.h"
#include	"SmaugFiles.h"


CLog::CLog (const char* name)
{
	m_Name = name;
	m_fp = NULL;
	m_bLeaveOpen = FALSE;
	memset (m_Types, 0, sizeof (m_Types));
}


CLog::~CLog ()
{
	Close ();
}


void CLog::Open ()
{
	if (! m_fp) {
		m_fp = fopen (m_Name, "a");
		if (! m_fp) {
			// if open fails, log it if possible, and continue.
			if (m_Name != STDERR_FILE)
				bug ("CLog::Open: Cannot open %s", NCCP m_Name);
			return;
		}
	}
}


void CLog::Close ()
{
	if (m_fp) {
		fclose (m_fp);
		m_fp = NULL;
	}
}


void CLog::SetFromSysData ()
{
	if (SysData.IsLogBootToFile ()) then SetType (LOG_BOOT);
	if (SysData.IsLogResetsToFile ()) then SetType (LOG_RESET);
	if (SysData.IsLogBuildToFile ()) then SetType (LOG_BUILD);
	if (SysData.IsLogBugsToFile ()) then SetType (LOG_BUG);
	if (SysData.IsLogPlayersToFile ()) then SetType (LOG_PLAYER);
	if (SysData.IsLogCommToFile ()) then SetType (LOG_COMM);
	if (! SysData.IsCloseStdErr ()) then SetLeaveOpen (TRUE);
}


void CLog::SetType (LogTypes t, BOOL bState /* = TRUE */)
{
	if (t == LOG_ALL) {
		for (int i=0; i < LOG_END; ++i)
			m_Types [i] = bState;
	}
	else m_Types [t] = bState;
}


void CLog::Print (LogTypes t, const char* s)
{
	if (IsLogged (t)) {
		Open ();
		if (m_fp) {
			fprintf (m_fp, s);
			if (! m_bLeaveOpen)
				Close ();
		}
	}
}


void CLog::Printf (LogTypes t, const char* fmt, ...)
{
	va_list	args;

	if (IsLogged (t)) {
		char	*buf = new char [MAX_STRING_LENGTH*2];

		va_start (args, fmt);
		vsprintf (buf, fmt, args);
		va_end (args);

		Print (t, buf);
		delete buf;
	}
}


long CLog::GetSize ()
{
	CFileStatus		St;

	return CFile::GetStatus (m_Name, St) ? St.m_size : 0;
}


void CLog::Archive ()
{
	Close ();
	CString	Bname = FileTable.MakeBackupName (m_Name);
	FileTable.Remove (Bname);
	rename (m_Name, Bname);
}