Files
itgmania212121/stepmania/src/RageLog.cpp
T

376 lines
9.3 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2002-05-01 19:14:55 +00:00
/*
-----------------------------------------------------------------------------
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
#include "arch/ArchHooks/ArchHooks.h"
2003-07-22 07:56:46 +00:00
#include "arch/arch.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
#include "RageUtil.h"
#include "RageTimer.h"
#include "PrefsManager.h"
2003-07-22 07:56:46 +00:00
#include "RageFile.h"
2003-02-14 07:18:00 +00:00
#include <time.h>
2003-08-12 06:50:42 +00:00
#include "ProductInfo.h"
2002-06-14 22:25:22 +00:00
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.
*
*/
2003-04-22 05:21:25 +00:00
/* Map data names to logged data.
*
* This lets us keep individual bits of changing information to be present
* in crash logs. For example, we want to know which file was being processed
* if we crash during a texture load. However, we don't want to log every
* load, since there are a huge number, even for log.txt. We only want to
* know the current one, if any.
*
* So, when a texture begins loading, we do:
* LOG->MapLog("TextureManager::Load", "Loading foo.png");
* and when it finishes loading without crashing,
* LOG->UnmapLog("TextureManager::Load");
*
* Each time a mapped log changes, we update a block of static text to be put
* in info.txt, so we see "Loading foo.png".
*
* The identifier is never displayed, so we can use a simple local object to
* map/unmap, using any mechanism to generate unique IDs. */
map<CString, CString> LogMaps;
2002-05-01 19:14:55 +00:00
// constants
2003-09-07 08:27:38 +00:00
#define LOG_PATH BASE_PATH "log.txt"
#define INFO_PATH BASE_PATH "info.txt"
2003-01-24 01:35:15 +00:00
#if defined(HAVE_VERSION_INFO)
2003-02-23 06:26:01 +00:00
extern unsigned long version_num;
extern const char *version_time;
#endif
2003-01-24 01:35:15 +00:00
/* staticlog gets info.txt
* crashlog gets log.txt */
enum {
2003-02-17 06:34:43 +00:00
/* If this is set, the message will also be written to info.txt and
* will be flagged "important" when sent to HOOKS->Log. (info and warnings) */
WRITE_TO_INFO = 0x01,
/* Whether this line should be loud when written to log.txt (warnings). */
WRITE_LOUD = 0x02
2003-01-24 01:35:15 +00:00
};
2002-05-01 19:14:55 +00:00
RageLog::RageLog()
{
m_bEnabled = true;
2002-05-01 19:14:55 +00:00
// delete old log files
2003-07-22 07:56:46 +00:00
remove( LOG_PATH );
remove( INFO_PATH );
2002-05-01 19:14:55 +00:00
// Open log file and leave it open.
m_fileLog = fopen( LOG_PATH, "w" );
// Failing to open shouldn't be fatal
//if( m_fileLog == NULL )
// RageException::Throw( " Couldn't open log.txt: %s", strerror(errno) );
m_fileInfo = fopen( INFO_PATH, "w" );
2003-08-05 01:28:48 +00:00
// Failing to open shouldn't be fatal
//if( m_fileInfo == NULL )
// RageException::Throw( " Couldn't open info.txt: %s", strerror(errno) );
2002-05-01 19:14:55 +00:00
2003-08-12 06:50:42 +00:00
this->Info( PRODUCT_NAME_VER );
2002-11-16 09:12:55 +00:00
#if defined(_MSC_VER)
2003-08-12 06:50:42 +00:00
this->Info( "Last compiled on %s.", __TIMESTAMP__ );
2002-11-16 09:12:55 +00:00
#endif
2003-02-14 07:18:00 +00:00
time_t cur_time;
time(&cur_time);
const struct tm *now = localtime(&cur_time);
this->Trace( "Log starting %.4d-%.2d-%.2d %.2d:%.2d:%.2d",
2003-02-14 07:18:00 +00:00
1900+now->tm_year, now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec );
2003-09-22 09:26:36 +00:00
this->Trace( " " );
#if defined(HAVE_VERSION_INFO)
2003-09-22 09:26:36 +00:00
this->Info("Compiled %s (build %lu)", version_time, version_num);
2003-02-23 06:26:01 +00:00
#endif
2002-05-01 19:14:55 +00:00
}
RageLog::~RageLog()
{
2003-04-22 05:21:25 +00:00
/* Add the mapped log data to info.txt. */
2003-09-06 06:49:01 +00:00
/* XXX: We shouldn't do this, because we shouldn't send newlines of any kind to Info();
* it results in oddly delimited output ... */
2003-08-13 04:52:41 +00:00
this->Info( "%s", GetAdditionalLog() );
2003-04-22 05:21:25 +00:00
2002-05-01 19:14:55 +00:00
Flush();
2003-02-15 00:13:23 +00:00
HideConsole();
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
}
void RageLog::SetLogging( bool b )
{
m_bEnabled = b;
}
2002-05-29 09:47:24 +00:00
void RageLog::ShowConsole()
{
#if defined(WIN32) && !defined(_XBOX)
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()
{
#if defined(WIN32) && !defined(_XBOX)
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
{
if( !m_bEnabled )
return;
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, ...)
{
if( !m_bEnabled )
return;
2003-01-19 05:00:21 +00:00
va_list va;
va_start(va, fmt);
CString sBuff = vssprintf( fmt, va );
va_end(va);
2003-01-24 01:35:15 +00:00
2003-02-17 06:34:43 +00:00
Write(WRITE_TO_INFO, sBuff);
2003-01-19 05:00:21 +00:00
}
void RageLog::Warn( const char *fmt, ...)
{
if( !m_bEnabled )
return;
va_list va;
va_start(va, fmt);
CString sBuff = vssprintf( fmt, va );
va_end(va);
2003-04-25 00:01:35 +00:00
Write(WRITE_TO_INFO | WRITE_LOUD, ssprintf("WARNING: %s", sBuff.c_str()));
2003-01-24 01:35:15 +00:00
}
2003-07-24 20:10:17 +00:00
void RageLog::Write( int where, CString str)
{
2003-07-24 20:10:17 +00:00
if( PREFSMAN && PREFSMAN->m_bTimestamping )
str = SecondsToTime(RageTimer::GetTimeSinceStart()) + ": " + str;
if( where&WRITE_TO_INFO && m_fileInfo )
fprintf(m_fileInfo, "%s\n", str.c_str() );
HOOKS->Log(str, where & WRITE_TO_INFO);
if( where & WRITE_TO_INFO )
AddToInfo( str );
AddToRecentLogs( str );
/* Only do this for log.txt and stdout. The other outputs are
* fairly quiet anyway, so the prepended "WARNING" makes them stand
* out well enough and this is just clutter. */
if( where & WRITE_LOUD )
{
2003-07-24 20:10:17 +00:00
str = ssprintf(
"/////////////////////////////////////////\n"
"%s\n"
"/////////////////////////////////////////",
str.c_str());
}
2003-07-24 20:10:17 +00:00
if( m_fileLog )
fprintf(m_fileLog, "%s\n", str.c_str() );
printf("%s\n", str.c_str() );
if( (PREFSMAN && PREFSMAN->m_bForceLogFlush) || (where & WRITE_TO_INFO) )
2003-07-24 20:10:17 +00:00
Flush();
}
2003-07-24 20:10:17 +00:00
void RageLog::Flush()
{
2003-07-24 20:10:17 +00:00
fflush( m_fileLog );
fflush( m_fileInfo );
}
/* These Get* functions are designed to be called from crash conditions. We
* store the text in a static buffer, which we can always access, and we double-
* check that it's null terminated when we return them. Finally, multi-line
* outputs can specify the string used to delineate lines (\n, \r or \r\n). That
* way, crash handlers can simply write() the buffer to a file without having
* to convert line endings, which is tedious when you can't malloc(). */
#if defined(_WIN32)
#define NEWLINE "\r\n"
2003-09-05 07:36:40 +00:00
//#elif defined(DARWIN)
//#define NEWLINE "\r"
2003-07-24 20:10:17 +00:00
#else
#define NEWLINE "\n"
#endif
static char staticlog[1024*32]="";
static CString staticlog_buf;
void RageLog::AddToInfo( CString str )
{
int old_len = staticlog_buf.size();
staticlog_buf += str + NEWLINE;
if( staticlog_buf.size() >= sizeof(staticlog) )
{
2003-07-24 20:10:17 +00:00
CString txt( NEWLINE "Staticlog limit reached" NEWLINE );
unsigned size_wanted = sizeof(staticlog) - txt.size() - 1;
if( size_wanted < staticlog_buf.size() )
staticlog_buf.erase( size_wanted, CString::npos );
staticlog_buf += txt;
old_len = 0;
}
2003-07-24 20:10:17 +00:00
2003-07-24 20:14:11 +00:00
strcpy(staticlog+old_len, (const char *)(staticlog_buf) + old_len);
2003-07-24 20:10:17 +00:00
}
const char *RageLog::GetInfo()
{
staticlog[ sizeof(staticlog)-1 ] = 0;
return staticlog;
}
static const int BACKLOG_LINES = 10;
static char backlog[BACKLOG_LINES][1024];
static int backlog_start=0, backlog_cnt=0;
void RageLog::AddToRecentLogs( CString str )
{
2003-08-01 12:12:02 +00:00
unsigned len = str.size();
if(len > sizeof(backlog[backlog_start])-1)
len = sizeof(backlog[backlog_start])-1;
strncpy(backlog[backlog_start], str, len);
backlog[backlog_start] [ len ] = 0;
backlog_start++;
if(backlog_start > backlog_cnt)
backlog_cnt=backlog_start;
backlog_start %= BACKLOG_LINES;
}
const char *RageLog::GetRecentLog( int n )
{
if( n >= BACKLOG_LINES || n >= backlog_cnt )
return false;
2003-08-19 02:19:17 +00:00
if( backlog_cnt == BACKLOG_LINES )
{
n += backlog_start;
n %= BACKLOG_LINES;
}
/* Make sure it's terminated: */
backlog[n][ sizeof(backlog[n])-1 ] = 0;
return backlog[n];
}
2003-07-22 05:05:39 +00:00
static char g_AdditionalLogStr[10240] = "";
static int g_AdditionalLogSize = 0;
2003-04-22 05:21:25 +00:00
void RageLog::UpdateMappedLog()
{
CString str;
for(map<CString, CString>::const_iterator i = LogMaps.begin(); i != LogMaps.end(); ++i)
2003-07-24 20:10:17 +00:00
str += ssprintf("%s" NEWLINE, i->second.c_str());
2003-04-22 05:21:25 +00:00
2003-07-23 21:41:35 +00:00
g_AdditionalLogSize = min( sizeof(g_AdditionalLogStr), str.size()+1 );
2003-07-22 05:05:39 +00:00
memcpy( g_AdditionalLogStr, str.c_str(), g_AdditionalLogSize );
2003-07-23 21:41:35 +00:00
g_AdditionalLogStr[ sizeof(g_AdditionalLogStr)-1 ] = 0;
2003-07-22 05:05:39 +00:00
/* XXX: deprecated */
2003-04-22 05:21:25 +00:00
HOOKS->AdditionalLog(str);
}
const char *RageLog::GetAdditionalLog()
2003-07-22 05:05:39 +00:00
{
int size = min( g_AdditionalLogSize, (int) sizeof(g_AdditionalLogStr)-1 );
g_AdditionalLogStr[size] = 0;
return g_AdditionalLogStr;
2003-07-22 05:05:39 +00:00
}
2003-04-22 05:21:25 +00:00
void RageLog::MapLog(const CString &key, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
LogMaps[key] = vssprintf( fmt, va );
va_end(va);
2003-07-23 21:41:35 +00:00
UpdateMappedLog();
2003-04-22 05:21:25 +00:00
}
void RageLog::UnmapLog(const CString &key)
{
LogMaps.erase(key);
2003-07-23 21:45:48 +00:00
UpdateMappedLog();
2003-04-22 05:21:25 +00:00
}
2003-04-22 05:53:18 +00:00
2003-04-27 21:55:31 +00:00
Checkpoint_::Checkpoint_(CString key_, int n, const char *fmt, ...)
2003-04-22 05:53:18 +00:00
{
key = ssprintf("%s:%i", key_.c_str(), n);
va_list va;
va_start(va, fmt);
LOG->MapLog( key, vssprintf(fmt, va) );
va_end(va);
}
Checkpoint_::~Checkpoint_()
{
LOG->UnmapLog( key );
}