//===========================================================================//
// File:	NetLog.cpp											
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 04/05/01  JSE  Inital coding
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Microsoft Corp.										 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "MW4Headers.hpp"

#include "NetLog.hpp"

#include "MWApplication.hpp"

#include <GameOS\\ToolOs.hpp>

#include <stdio.h>


#include <Windows.h>
#include <fstream>


NetLog gNetworkLog;
std::ofstream netLogStream;
		


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//

void NetLog::Init()
{

	if (!open)
	{
		open = true;

		WORD day, month, year;
		gos_GetUnformattedDate(&year, &month, &day);


		SYSTEMTIME sysTime;

		GetLocalTime( &sysTime);

		MString file_name;
		

		if (!gos_DoesFileExist("NETLOG"))
			gos_CreateDirectory("NETLOG");


		if (sysTime.wHour > 12)
			sysTime.wHour -= 12;


		char buffer[50];
		sprintf(&buffer[0], "NETLOG\\NETLOG_%d-%d-%d_%d_%d_%d.txt", month, day, year, sysTime.wHour, sysTime.wMinute, sysTime.wMilliseconds);

		file_name = buffer;
		
		netLogStream.open(file_name);

		MWApplication *app = MWApplication::GetInstance();

		if (app->serverFlag)
			netLogStream << "---------SERVER-----------\n";
		else
			netLogStream << "---------CLIENT-----------\n";
		

		


		sprintf(&buffer[0], "DATE : %d-%d-%d %d:%d:%d\n", month, day, year, sysTime.wHour, sysTime.wMinute, sysTime.wMilliseconds);
		MString output = buffer;
		netLogStream << buffer;

		Stuff::Scalar time = (float)gos_GetElapsedTime(1);
		int client = Connection::Local->GetID();
		netLogStream << time << " : " << client << ": '" << app->servedConnectionData[client].pilotName << "' START GAME\nn\n\n";



		netLogStream << "------------------------------------------------------\n";


		netLogStream.flush();

	}

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//

void NetLog::Log(MString data, int tab_level, int client, bool incoming)
{
	
	if (open)
	{
		MWApplication *app = MWApplication::GetInstance();


		Scalar time = (float)gos_GetElapsedTime(1);
		netLogStream << time << " : ";
		
		if (incoming)
			netLogStream << "from : ";
		else
			netLogStream << "to : ";

		netLogStream << client << ": '" << app->servedConnectionData[client].pilotName << "'";

		for (int i = 0; i < tab_level+1; ++i)
		{
			netLogStream << "\t";
		}

		netLogStream << data;
		netLogStream << MString("\n");
		netLogStream.flush();
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//

void NetLog::Close()
{
	if (open)
	{
		open = false;
		Stuff::Scalar time = (float)gos_GetElapsedTime(1);

		MWApplication *app = MWApplication::GetInstance();
		int client = Connection::Local->GetID();
		netLogStream << time << " : " << client << ": '" << app->servedConnectionData[client].pilotName << "' STOP GAME\nn\n\n";
		netLogStream << "------------------------------------------------------\n";


		netLogStream.close();
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
