1
0
forked from 0ad/0ad
0ad/source/ps/Network/NetLog.cpp
olsner 7bcc12373b - Created JSMap
- Changed player colour JS interface: setColour(...) instead of a colour
property
- Introduced a network log and replaced most network LOG() calls with
NET_LOG()
- Moved to a slot-based system for Pre-Game and extended a lot of the JS
APIs to networking stuff
- A bit of cleanup in the low-level network code (Unix parts)
- Clients now keep track of all other connected clients on the server
(And exposes this info to JS)
- Split out GameAttributes to its own file
- Removed unused class AttributeMap
- Changed CJSObject to use T* pointers in JS_SetPrivate (needed to make
ToScript work with multiple inheritance)

This was SVN commit r1929.
2005-02-21 17:13:31 +00:00

91 lines
1.6 KiB
C++

#include "precompiled.h"
#include "NetLog.h"
#include "ThreadUtil.h"
#include <stdio.h>
#include <stdarg.h>
#include "lib/timer.h"
CNetLog g_NetLog;
#define LOG_FORMAT "[%3u.%03u] %s\n"
#define LOG_ARGS_PREFIX (uint)get_time(), ((uint)(get_time()*1000)) % 1000,
#define LOG_ARGS_SUFFIX
CNetLog::CNetLog():
m_Initialized(false),
m_Flush(true),
m_pFile(NULL)
{}
CNetLog::~CNetLog()
{
if (m_Initialized)
{
fclose(m_pFile);
}
}
void CNetLog::Initialize()
{
CScopeLock scopeLock(m_Mutex);
if (m_Initialized)
return;
char filename[256];
snprintf(filename, sizeof(filename), "../logs/net_log.txt");
filename[sizeof(filename)-1]=0;
Open(filename);
m_Initialized=true;
}
void CNetLog::Open(const char *filename)
{
m_pFile=fopen(filename, "a");
if (m_pFile)
{
time_t t = time(NULL);
struct tm* now = localtime(&t);
fprintf(m_pFile, "\n\n***************************************************\n");
fprintf(m_pFile, "LOG STARTED: %04d-%02d-%02d at %02d:%02d:%02d\n",
1900+now->tm_year, now->tm_mon, now->tm_mday, now->tm_hour,
now->tm_min, now->tm_sec);
fprintf(m_pFile, "Timestamps are in seconds since engine startup\n\n");
}
}
void CNetLog::Flush()
{
fflush(m_pFile);
}
void CNetLog::Write(const char *format, ...)
{
Initialize();
if (!m_pFile) return;
char buf[512];
va_list args;
va_start(args, format);
vsnprintf(buf, 512, format, args);
buf[511]=0;
va_end(args);
m_Mutex.Lock();
fprintf(m_pFile, LOG_FORMAT, LOG_ARGS_PREFIX buf LOG_ARGS_SUFFIX);
if (m_Flush)
Flush();
m_Mutex.Unlock();
}
void CNetLog::SetFlush(bool flush)
{
CScopeLock scopeLock(m_Mutex);
m_Flush=flush;
if (m_Flush)
Flush();
}