Cleaned up Windows logging code.
This commit is contained in:
parent
a85e1ca59f
commit
54ff3f3b9d
|
@ -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 */
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
textbuf[0]=0;
|
||||
if(logcount>=64)
|
||||
{
|
||||
x=logcount&63;
|
||||
for(;;)
|
||||
{
|
||||
strcat(textbuf,logtext[x]);
|
||||
x=(x+1)&63;
|
||||
if(x==(logcount&63)) break;
|
||||
}
|
||||
}
|
||||
else
|
||||
for(x=0;x<logcount;x++)
|
||||
{
|
||||
strcat(textbuf,logtext[x]);
|
||||
}
|
||||
SetDlgItemText(logwin,100,textbuf);
|
||||
SendDlgItemMessage(logwin,100,EM_LINESCROLL,0,200);
|
||||
return logcount & ( MAXIMUM_NUMBER_OF_LOGS - 1 );
|
||||
}
|
||||
|
||||
static BOOL CALLBACK LogCon(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
DSMFix(uMsg);
|
||||
switch(uMsg)
|
||||
{
|
||||
case WM_INITDIALOG:logwin=hwndDlg;
|
||||
RedoText();break;
|
||||
case WM_COMMAND:
|
||||
if(HIWORD(wParam)==BN_CLICKED)
|
||||
{
|
||||
DestroyWindow(hwndDlg);
|
||||
logwin=0;
|
||||
}
|
||||
break;
|
||||
char textbuf[65536] = { 0 };
|
||||
unsigned int x;
|
||||
|
||||
}
|
||||
return 0;
|
||||
// TODO: This if can be made much simpler.
|
||||
if(logcount >= MAXIMUM_NUMBER_OF_LOGS)
|
||||
{
|
||||
|
||||
x = truncated_logcount();
|
||||
|
||||
for(;;)
|
||||
{
|
||||
strcat(textbuf, logtext[x]);
|
||||
x = ( x + 1 ) & ( MAXIMUM_NUMBER_OF_LOGS - 1 );
|
||||
|
||||
if(x == truncated_logcount())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(x = 0; x < logcount; x++)
|
||||
{
|
||||
strcat(textbuf,logtext[x]);
|
||||
}
|
||||
}
|
||||
|
||||
SetDlgItemText(logwin, 100, textbuf);
|
||||
SendDlgItemMessage(logwin, 100, EM_LINESCROLL, 0, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function for the log window.
|
||||
**/
|
||||
BOOL CALLBACK LogCon(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
DSMFix(uMsg);
|
||||
|
||||
switch(uMsg)
|
||||
{
|
||||
case WM_INITDIALOG:
|
||||
RedoText();
|
||||
break;
|
||||
case WM_COMMAND:
|
||||
if(HIWORD(wParam)==BN_CLICKED)
|
||||
{
|
||||
DestroyWindow(hwndDlg);
|
||||
|
||||
// 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);
|
||||
if(!logwin)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if(*t=='\n') x++;
|
||||
t++;
|
||||
}
|
||||
// Used to iterate over the message log created in this function
|
||||
char* msg_iterator;
|
||||
|
||||
if(!(logtext[logcount&63]=(char*)malloc(strlen(text)+1+x+newline*2))) //mbg merge 7/17/06 added cast
|
||||
return;
|
||||
// Free a log message if more messages than necessary were logged.
|
||||
if(logcount >= MAXIMUM_NUMBER_OF_LOGS)
|
||||
{
|
||||
free(logtext[truncated_logcount()]);
|
||||
}
|
||||
|
||||
t=logtext[logcount&63];
|
||||
number_of_newlines = 0;
|
||||
text_iterator_c = text;
|
||||
|
||||
while(*text)
|
||||
{
|
||||
if(*text=='\n')
|
||||
{
|
||||
*t='\r';
|
||||
t++;
|
||||
}
|
||||
*t=*text;
|
||||
t++;
|
||||
text++;
|
||||
}
|
||||
if(newline)
|
||||
{
|
||||
*t='\r';
|
||||
t++;
|
||||
*t='\n';
|
||||
t++;
|
||||
}
|
||||
*t=0;
|
||||
logcount++;
|
||||
if(logwin)
|
||||
RedoText();
|
||||
// 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;
|
||||
}
|
||||
|
||||
msg_iterator = logtext[truncated_logcount()];
|
||||
|
||||
// Copy the characters from text to the allocated buffer
|
||||
while(*text)
|
||||
{
|
||||
// Replace \n with \r\n
|
||||
if(*text == '\n')
|
||||
{
|
||||
*msg_iterator='\r';
|
||||
msg_iterator++;
|
||||
}
|
||||
|
||||
*msg_iterator = *text;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue