Files
itgmania212121/stepmania/src/RageLog.cpp
T

165 lines
3.4 KiB
C++
Raw Normal View History

2002-05-01 19:14:55 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
Class: RageLog
Desc: See header.
2003-01-24 01:35:15 +00:00
Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
2002-05-01 19:14:55 +00:00
Chris Danford
2003-01-24 01:35:15 +00:00
Glenn Maynard
2002-05-01 19:14:55 +00:00
-----------------------------------------------------------------------------
*/
2002-08-22 20:21:37 +00:00
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
#include "RageUtil.h"
#include <fstream>
2002-06-14 22:25:22 +00:00
2003-01-24 22:28:23 +00:00
/* XXX */
#include "archutils/win32/crash.h"
2002-05-01 19:14:55 +00:00
RageLog* LOG; // global and accessable from anywhere in the program
2003-01-24 01:35:15 +00:00
/* We have a couple log types and a couple logs.
*
* Traces are for very verbose debug information. Use them as much as you want.
*
* Warnings are for things that shouldn't happen, but can be dealt with. (If
* they can't be dealt with, use RageException::Throw, which will also send a
* warning.)
*
* Info is for important bits of information. These should be used selectively.
* Try to keep Info text dense; lots of information is fine, but try to keep
* it to a reasonable total length.
*
* log.txt receives all logs. This file can get rather large.
*
* info.txt receives warnings and infos. This file should be fairly small; small
* enough to be mailed without having to be edited or zipped, and small enough
* to be very easily read.
*
*/
2002-05-01 19:14:55 +00:00
// constants
#define LOG_FILE_NAME "log.txt"
2003-01-24 01:35:15 +00:00
#define INFO_FILE_NAME "info.txt"
/* staticlog gets info.txt
* crashlog gets log.txt */
enum {
TO_LOG = 0x01,
TO_INFO = 0x02,
};
2002-05-01 19:14:55 +00:00
RageLog::RageLog()
{
// delete old log files
2002-12-18 06:45:11 +00:00
remove( LOG_FILE_NAME );
2003-01-24 01:35:15 +00:00
remove( INFO_FILE_NAME );
2002-05-01 19:14:55 +00:00
// Open log file and leave it open.
2002-05-01 19:14:55 +00:00
m_fileLog = fopen( LOG_FILE_NAME, "w" );
2003-01-24 01:35:15 +00:00
m_fileInfo = fopen( INFO_FILE_NAME, "w" );
2002-05-01 19:14:55 +00:00
2002-11-16 09:12:55 +00:00
#if defined(_MSC_VER)
this->Trace( "Last compiled on %s.", __TIMESTAMP__ );
2002-11-16 09:12:55 +00:00
#endif
2003-01-24 01:35:15 +00:00
SYSTEMTIME st;
GetLocalTime( &st );
this->Trace( "Log starting %.4d-%.2d-%.2d %.2d:%.2d:%.2d",
2002-05-01 19:14:55 +00:00
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond );
2003-01-24 02:16:43 +00:00
this->Trace( "" );
2003-01-24 01:35:15 +00:00
ShowConsole();
2002-05-01 19:14:55 +00:00
}
RageLog::~RageLog()
{
Flush();
2002-05-29 09:47:24 +00:00
FreeConsole();
2003-01-24 01:35:15 +00:00
if(m_fileLog) fclose( m_fileLog );
if(m_fileInfo) fclose( m_fileInfo );
2002-05-01 19:14:55 +00:00
}
2002-05-29 09:47:24 +00:00
void RageLog::ShowConsole()
{
2003-01-24 01:35:15 +00:00
#if defined(WIN32)
2002-05-29 09:47:24 +00:00
// create a new console window and attach standard handles
AllocConsole();
freopen("CONOUT$","wb",stdout);
freopen("CONOUT$","wb",stderr);
2003-01-24 01:35:15 +00:00
#endif
2002-05-29 09:47:24 +00:00
}
void RageLog::HideConsole()
{
2003-01-24 01:35:15 +00:00
#if defined(WIN32)
2002-05-29 09:47:24 +00:00
FreeConsole();
2003-01-24 01:35:15 +00:00
#endif
2002-05-29 09:47:24 +00:00
}
void RageLog::Trace( const char *fmt, ...)
2002-05-01 19:14:55 +00:00
{
va_list va;
va_start(va, fmt);
CString sBuff = vssprintf( fmt, va );
va_end(va);
2002-05-01 19:14:55 +00:00
2003-01-24 01:35:15 +00:00
Write(0, sBuff);
2002-05-01 19:14:55 +00:00
}
2003-01-19 05:00:21 +00:00
/* Use this for more important information; it'll always be included
* in crash dumps. */
void RageLog::Info( const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
CString sBuff = vssprintf( fmt, va );
va_end(va);
2003-01-24 01:35:15 +00:00
Write(TO_INFO, sBuff);
2003-01-19 05:00:21 +00:00
}
void RageLog::Warn( const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
CString sBuff = vssprintf( fmt, va );
va_end(va);
2003-01-24 01:35:15 +00:00
Write(TO_INFO, ssprintf(
"/////////////////////////////////////////\n"
"WARNING: %s\n"
"/////////////////////////////////////////", sBuff.GetString()));
}
void RageLog::Write( int where, CString str)
{
if( m_fileLog )
fprintf(m_fileLog, "%s\n", str.GetString() );
if( where & TO_INFO )
{
if( m_fileInfo )
fprintf(m_fileInfo, "%s\n", str.GetString() );
StaticLog(str);
}
printf("%s\n", str.GetString() );
CrashLog(str);
#ifdef DEBUG
Flush();
#else
if(where & TO_INFO)
Flush();
#endif
2002-05-01 19:14:55 +00:00
}
void RageLog::Flush()
{
fflush( m_fileLog );
}