 |
|
 |
I am fraid that it seems not to work. Can you help me? (pkuwgg@gmail.com)
thanks~!
|
|
|
|
 |
|
 |
Just what I needed. Works like a charm.
|
|
|
|
 |
|
 |
Hi,
Very helpful program, works great. The message buffer is declared as char buffer[1024] but wvsprintf can write up to 1024 characters AND a terminating '0' character so I would suggest char buffer[1025].
|
|
|
|
 |
|
 |
I recently blew my program up because I accidentally mixed up my format specifiers and my arguments in a call to the PRINTF macro.
For example, this code will blow up someday:
int nNumber = //some number
char * szString = //some string
PRINTF("printing: %d %s", szString, nNumber);
The enhancement I made to this handy class is a __try/__except block around the call to wvsprintf(). If an exception is raised when wvsprintf is called, a message box is displayed w/ the offending format string so that you can find and fix the cause of the exception easier.
//old code:
va_list arglist;
va_start( arglist, fmt );
wvsprintf( buffer, fmt, arglist );
va_end(arglist);
//new code:
DWORD dwExceptionCode = 0;
bool bExit = false;
va_list arglist;
va_start( arglist, fmt );
__try{
wvsprintf( buffer, fmt, arglist );
}
__except(dwExceptionCode = GetExceptionCode(), EXCEPTION_EXECUTE_HANDLER)
{
//show programmer the offending format string
//format a string to programmer,
//including offending format string: fmt
::MessageBox(NULL, buffer,_T("Programmer Note:"), MB_OK);
bExit = true;//<-- leave the function
}
va_end(arglist);
if( bExit ){
LeaveCriticalSection(&m_hLock);
return;
}
|
|
|
|
 |
|
 |
Hello,
I have noticed some problems while using the Console window for debug output, and I'm wondering if anybody here has seen this and maybe even has a fix for it....
First, if you close the console window, my app exits. Also, if the console window has the focus, and I hit Ctrl+C, my app exits. I've got the close button(& menu item) disabled thanks to some code I found around here, but what can I do about myself or somebody else hitting Ctrl+C. Is there any way around this?
Also, I'm using the PRINTF() macro throughout my code in a multi-threaded app (uses MFC), and sometimes when I have the console window visible, my app becomes hung. When this happens the console window will still respond and if I set the focus to the console window and hit Ctrl+C, my app becomes 'un-hung' and every thing begins running normally again. This seems rather wierd to me.
Does anybody have any tips and tricks for using the console window ?
Thanks!
|
|
|
|
 |
|
 |
I still don't know why my app sometimes hangs until I press Ctrl+C.
I did the find the way to prevent a windows application that is using a console window for std output (such as with the code from this article) from exitting when Ctrl+C is pressed.
The code to prevent Ctrl+C(on your console window) from killing your app looks like this:
BOOL WINAPI ConsoleCtrlEvent_Handler(DWORD dwCtrlEvent)
{
switch(dwCtrlEvent)
{
case CTRL_C_EVENT:
return TRUE;
case CTRL_BREAK_EVENT: case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
default:
return FALSE;
}
}
Note that the CTRL_CLOSE_EVENT will cause the system to display the 'End Program' dialog box. The best way to help prevent this is to disable the close button on the console window.
Here is code I found to remove the close button from the console window:
HWND GetConsoleHwnd()
{
#define MY_BUFSIZE 1024 HWND hwndFound;
TCHAR pszNewWindowTitle[MY_BUFSIZE]; TCHAR pszOldWindowTitle[MY_BUFSIZE]; GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
_stprintf(pszNewWindowTitle,_T("%d/%d"), GetTickCount(), GetCurrentProcessId());
SetConsoleTitle(pszNewWindowTitle);
Sleep(40);
hwndFound = FindWindow(NULL, pszNewWindowTitle);
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
BOOL DisableConsoleClose()
{
return RemoveMenu(GetSystemMenu(GetConsoleHwnd(), FALSE),
SC_CLOSE, MF_BYCOMMAND);
}
then where you call AllocConsole():
if( AllocConsole() )
{
DisableConsoleClose();
SetConsoleCtrlHandler( ConsoleCtrlEvent_Handler, TRUE) }
and where you call FreeConsole():
SetConsoleCtrlHandler(ConsoleCtrlEvent_Handler, FALSE); FreeConsole();
|
|
|
|
 |
|
 |
Hello,
Shouldn't CDebugPrintf::HideConsole()
{
..
m_bUseConsole = true;
AllocConsol();
..
}
actually be:
{
..
m_bUseConsole = false;
FreeConsole();
..
}
????
ps. Thanks, this will come in handy!
|
|
|
|
 |
|
 |
Check out the changes below for the multi-line comment problem
#define IFDEBUG( doit )
#define SLASH /
#define COMMENT ;SLASH/
// begin new
#define BEG_COMMENT SLASH*
#define END_COMMENT *SLASH
#define PRINTF(a) BEG_COMMENT a END_COMMENT
// end new
// begin old
//#define PRINTF COMMENT
// begin new
#define SHOW_CONSOLE COMMENT
#define HIDE_CONSOLE COMMENT
#define SHOW_LASTERROR COMMENT
#define CLOSE_LOG COMMENT
#endif
|
|
|
|
 |
|
 |
You could easily enhance the logging by including the ThreadID of the thread writing the log entry
|
|
|
|
 |
|
 |
In the printf function, change the following:-
wsprintf( buf, "%d.%03d %02d/%02d/%02d %02d:%02d:%02d ",
dwDiffTime/1000, dwDiffTime%1000,
time.wMonth, time.wDay, time.wYear,
time.wHour, time.wMinute, time.wSecond);
to
wsprintf( buf, "%d.%03d %02d/%02d/%02d %02d:%02d:%02d (%d) ",
dwDiffTime/1000, dwDiffTime%1000,
time.wMonth, time.wDay, time.wYear,
time.wHour, time.wMinute, time.wSecond, _getpid());
This will now record the process id.
|
|
|
|
 |
|
 |
The PID is useless. You want GetThreadID
|
|
|
|
 |
|
|
 |