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. */
void FCEUD_PrintError(char *s);
void FCEUD_Message(char *s);
void FCEUD_Message(const char *s);
/* Network interface */

View File

@ -1,106 +1,191 @@
#include <stdlib.h>
#include "common.h"
static HWND logwin=0;
static HWND logwin = 0;
static char *logtext[64];
static int logcount=0;
static char *logtext[MAXIMUM_NUMBER_OF_LOGS];
static unsigned int logcount=0;
static void RedoText(void)
unsigned int truncated_logcount()
{
char textbuf[65536];
int x;
return logcount & ( MAXIMUM_NUMBER_OF_LOGS - 1 );
}
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)
{
char textbuf[65536] = { 0 };
unsigned int x;
// TODO: This if can be made much simpler.
if(logcount >= MAXIMUM_NUMBER_OF_LOGS)
{
x=logcount&63;
x = truncated_logcount();
for(;;)
{
strcat(textbuf,logtext[x]);
x=(x+1)&63;
if(x==(logcount&63)) break;
strcat(textbuf, logtext[x]);
x = ( x + 1 ) & ( MAXIMUM_NUMBER_OF_LOGS - 1 );
if(x == truncated_logcount())
{
break;
}
}
}
else
for(x=0;x<logcount;x++)
{
for(x = 0; x < logcount; x++)
{
strcat(textbuf,logtext[x]);
}
SetDlgItemText(logwin,100,textbuf);
SendDlgItemMessage(logwin,100,EM_LINESCROLL,0,200);
}
SetDlgItemText(logwin, 100, textbuf);
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);
switch(uMsg)
{
case WM_INITDIALOG:logwin=hwndDlg;
RedoText();break;
case WM_INITDIALOG:
RedoText();
break;
case WM_COMMAND:
if(HIWORD(wParam)==BN_CLICKED)
{
DestroyWindow(hwndDlg);
logwin=0;
// Clear the handle
logwin = 0;
}
break;
}
return 0;
}
/**
* Creates the log window if it's not already open.
**/
void MakeLogWindow(void)
{
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;
char *t;
// Used to count the number of new line characters in text
int number_of_newlines;
if(logcount>=64) free(logtext[logcount&63]);
// Used to iterate over the text
const char *text_iterator_c;
x=0;
t=text;
while(*t)
// Used to iterate over the message log created in this function
char* msg_iterator;
// Free a log message if more messages than necessary were logged.
if(logcount >= MAXIMUM_NUMBER_OF_LOGS)
{
if(*t=='\n') x++;
t++;
free(logtext[truncated_logcount()]);
}
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;
}
t=logtext[logcount&63];
msg_iterator = logtext[truncated_logcount()];
// Copy the characters from text to the allocated buffer
while(*text)
{
if(*text=='\n')
// Replace \n with \r\n
if(*text == '\n')
{
*t='\r';
t++;
*msg_iterator='\r';
msg_iterator++;
}
*t=*text;
t++;
*msg_iterator = *text;
msg_iterator++;
text++;
}
if(newline)
// Add a final newline if requested
if(add_newline)
{
*t='\r';
t++;
*t='\n';
t++;
*msg_iterator = '\r';
msg_iterator++;
*msg_iterator = '\n';
msg_iterator++;
}
*t=0;
// Terminating 0-byte
*msg_iterator = 0;
// Keep track of the added log
logcount++;
if(logwin)
{
RedoText();
}
}
void FCEUD_Message(char *text)
/**
* 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,0);
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 AddLogText(char *text,int newline);
void AddLogText(const char *text, unsigned int add_newline);

View File

@ -416,9 +416,9 @@ int main(int argc,char *argv[])
{
char *t;
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();