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. */
|
/* 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 */
|
||||||
|
|
||||||
|
|
|
@ -1,106 +1,191 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
char textbuf[65536] = { 0 };
|
||||||
switch(uMsg)
|
unsigned int x;
|
||||||
{
|
|
||||||
case WM_INITDIALOG:logwin=hwndDlg;
|
|
||||||
RedoText();break;
|
|
||||||
case WM_COMMAND:
|
|
||||||
if(HIWORD(wParam)==BN_CLICKED)
|
|
||||||
{
|
|
||||||
DestroyWindow(hwndDlg);
|
|
||||||
logwin=0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
// TODO: This if can be made much simpler.
|
||||||
return 0;
|
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)
|
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)
|
|
||||||
{
|
|
||||||
if(*t=='\n') x++;
|
|
||||||
t++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!(logtext[logcount&63]=(char*)malloc(strlen(text)+1+x+newline*2))) //mbg merge 7/17/06 added cast
|
// Free a log message if more messages than necessary were logged.
|
||||||
return;
|
if(logcount >= MAXIMUM_NUMBER_OF_LOGS)
|
||||||
|
{
|
||||||
|
free(logtext[truncated_logcount()]);
|
||||||
|
}
|
||||||
|
|
||||||
t=logtext[logcount&63];
|
number_of_newlines = 0;
|
||||||
|
text_iterator_c = text;
|
||||||
|
|
||||||
while(*text)
|
// Count the number of \n characters in the text
|
||||||
{
|
while(*text_iterator_c)
|
||||||
if(*text=='\n')
|
{
|
||||||
{
|
if(*text_iterator_c == '\n')
|
||||||
*t='\r';
|
{
|
||||||
t++;
|
number_of_newlines++;
|
||||||
}
|
}
|
||||||
*t=*text;
|
|
||||||
t++;
|
text_iterator_c++;
|
||||||
text++;
|
}
|
||||||
}
|
|
||||||
if(newline)
|
unsigned int necessary_size = strlen(text) // len(text)
|
||||||
{
|
+ 1 // 0-byte
|
||||||
*t='\r';
|
+ number_of_newlines // Space for additional \r characters
|
||||||
t++;
|
+ 2 * add_newline; // \r\n if a newline was requested
|
||||||
*t='\n';
|
|
||||||
t++;
|
//mbg merge 7/17/06 added cast
|
||||||
}
|
logtext[truncated_logcount()] = (char*)malloc(necessary_size);
|
||||||
*t=0;
|
|
||||||
logcount++;
|
// Apparently there's no memory left.
|
||||||
if(logwin)
|
if(!logtext[truncated_logcount()])
|
||||||
RedoText();
|
{
|
||||||
|
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 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;
|
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();
|
InitCommonControls();
|
||||||
|
|
Loading…
Reference in New Issue