Cleaned up Windows logging code.

This commit is contained in:
rheiny 2007-02-05 19:37:24 +00:00
parent a85e1ca59f
commit 54ff3f3b9d
4 changed files with 174 additions and 84 deletions

View File

@ -22,7 +22,7 @@ void FCEUD_GetPalette(uint8 i,uint8 *r, uint8 *g, uint8 *b);
/* Displays an error. Can block or not. */ /* Displays an error. Can block or not. */
void FCEUD_PrintError(char *s); void FCEUD_PrintError(char *s);
void FCEUD_Message(char *s); void FCEUD_Message(const char *s);
/* Network interface */ /* Network interface */

View File

@ -3,104 +3,189 @@
static HWND logwin = 0; static HWND logwin = 0;
static char *logtext[64]; static char *logtext[MAXIMUM_NUMBER_OF_LOGS];
static int logcount=0; static unsigned int logcount=0;
static void RedoText(void) unsigned int truncated_logcount()
{ {
char textbuf[65536]; return logcount & ( MAXIMUM_NUMBER_OF_LOGS - 1 );
int x; }
textbuf[0]=0; /**
if(logcount>=64) * Concatenates formerly logged messages into a single string and
* displays that string in the log window.
*
* TODO: This function contains a potential buffer overflow
**/
void RedoText(void)
{ {
x=logcount&63; char textbuf[65536] = { 0 };
unsigned int x;
// TODO: This if can be made much simpler.
if(logcount >= MAXIMUM_NUMBER_OF_LOGS)
{
x = truncated_logcount();
for(;;) for(;;)
{ {
strcat(textbuf, logtext[x]); strcat(textbuf, logtext[x]);
x=(x+1)&63; x = ( x + 1 ) & ( MAXIMUM_NUMBER_OF_LOGS - 1 );
if(x==(logcount&63)) break;
if(x == truncated_logcount())
{
break;
}
} }
} }
else else
{
for(x = 0; x < logcount; x++) for(x = 0; x < logcount; x++)
{ {
strcat(textbuf,logtext[x]); strcat(textbuf,logtext[x]);
} }
}
SetDlgItemText(logwin, 100, textbuf); SetDlgItemText(logwin, 100, textbuf);
SendDlgItemMessage(logwin, 100, EM_LINESCROLL, 0, 200); SendDlgItemMessage(logwin, 100, EM_LINESCROLL, 0, 200);
} }
static BOOL CALLBACK LogCon(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) /**
* Callback function for the log window.
**/
BOOL CALLBACK LogCon(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ {
DSMFix(uMsg); DSMFix(uMsg);
switch(uMsg) switch(uMsg)
{ {
case WM_INITDIALOG:logwin=hwndDlg; case WM_INITDIALOG:
RedoText();break; RedoText();
break;
case WM_COMMAND: case WM_COMMAND:
if(HIWORD(wParam)==BN_CLICKED) if(HIWORD(wParam)==BN_CLICKED)
{ {
DestroyWindow(hwndDlg); DestroyWindow(hwndDlg);
// Clear the handle
logwin = 0; logwin = 0;
} }
break; break;
} }
return 0; return 0;
} }
/**
* Creates the log window if it's not already open.
**/
void MakeLogWindow(void) void MakeLogWindow(void)
{ {
if(!logwin) if(!logwin)
CreateDialog(fceu_hInstance,"MESSAGELOG",0,LogCon); {
logwin = CreateDialog(fceu_hInstance, "MESSAGELOG" , 0, LogCon);
}
} }
void AddLogText(char *text,int newline) /**
* Adds a textual log message to the message buffer.
*
* @param text Message to add.
* @param add_newline Either DO_ADD_NEWLINE or DONT_ADD_NEWLINE
**/
void AddLogText(const char *text, unsigned int add_newline)
{ {
int x; // Used to count the number of new line characters in text
char *t; int number_of_newlines;
if(logcount>=64) free(logtext[logcount&63]); // Used to iterate over the text
const char *text_iterator_c;
x=0; // Used to iterate over the message log created in this function
t=text; char* msg_iterator;
while(*t)
// Free a log message if more messages than necessary were logged.
if(logcount >= MAXIMUM_NUMBER_OF_LOGS)
{ {
if(*t=='\n') x++; free(logtext[truncated_logcount()]);
t++;
} }
if(!(logtext[logcount&63]=(char*)malloc(strlen(text)+1+x+newline*2))) //mbg merge 7/17/06 added cast number_of_newlines = 0;
text_iterator_c = text;
// Count the number of \n characters in the text
while(*text_iterator_c)
{
if(*text_iterator_c == '\n')
{
number_of_newlines++;
}
text_iterator_c++;
}
unsigned int necessary_size = strlen(text) // len(text)
+ 1 // 0-byte
+ number_of_newlines // Space for additional \r characters
+ 2 * add_newline; // \r\n if a newline was requested
//mbg merge 7/17/06 added cast
logtext[truncated_logcount()] = (char*)malloc(necessary_size);
// Apparently there's no memory left.
if(!logtext[truncated_logcount()])
{
return; return;
}
t=logtext[logcount&63]; msg_iterator = logtext[truncated_logcount()];
// Copy the characters from text to the allocated buffer
while(*text) while(*text)
{ {
// Replace \n with \r\n
if(*text == '\n') if(*text == '\n')
{ {
*t='\r'; *msg_iterator='\r';
t++; msg_iterator++;
}
*t=*text;
t++;
text++;
}
if(newline)
{
*t='\r';
t++;
*t='\n';
t++;
}
*t=0;
logcount++;
if(logwin)
RedoText();
} }
void FCEUD_Message(char *text) *msg_iterator = *text;
{
AddLogText(text,0); msg_iterator++;
text++;
}
// Add a final newline if requested
if(add_newline)
{
*msg_iterator = '\r';
msg_iterator++;
*msg_iterator = '\n';
msg_iterator++;
}
// Terminating 0-byte
*msg_iterator = 0;
// Keep track of the added log
logcount++;
if(logwin)
{
RedoText();
}
}
/**
* Adds a textual message to the message buffer without adding a newline at the end.
*
* @param text The text of the message to add.
*
* TODO: This function should have a better name.
**/
void FCEUD_Message(const char *text)
{
AddLogText(text, DONT_ADD_NEWLINE);
} }

View File

@ -1,2 +1,7 @@
#define MAXIMUM_NUMBER_OF_LOGS 64
#define DONT_ADD_NEWLINE 0
#define DO_ADD_NEWLINE 1
void MakeLogWindow(void); void MakeLogWindow(void);
void AddLogText(char *text,int newline); void AddLogText(const char *text, unsigned int add_newline);

View File

@ -418,7 +418,7 @@ int main(int argc,char *argv[])
if(timeBeginPeriod(1) != TIMERR_NOERROR) if(timeBeginPeriod(1) != TIMERR_NOERROR)
{ {
AddLogText("Error setting timer granularity to 1ms.",1); AddLogText("Error setting timer granularity to 1ms.", DO_ADD_NEWLINE);
} }
InitCommonControls(); InitCommonControls();