 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Koos van der Merwe Guest
|
Posted: Mon Mar 07, 2005 2:23 pm Post subject: Background console for GUI program. Possible? |
|
|
Hi
I'm debugging somebody else's code and want to add simple debug messages at certain points in the program to get a better idea of the program flow. I would like to know if it is possible to use <iostream.h>'s cout << (and cin >>) for debugging purposes on a console while the main (graphical) program is running in the forground. (This could be very usefull when using Kylix e.g.)
I would prefer to not have messageboxes popping up. Or a "debug" form that disappears when the application shuts down. Anybody with experience of a similar problem?
Thanks
Koos
|
|
| Back to top |
|
 |
Michel Leunen Guest
|
Posted: Mon Mar 07, 2005 7:13 pm Post subject: Re: Background console for GUI program. Possible? |
|
|
Koos van der Merwe wrote:
| Quote: | I'm debugging somebody else's code and want to add simple debug messages at certain points in the program to get a better idea of the program flow. I would like to know if it is possible to use <iostream.h>'s cout << (and cin >>) for debugging purposes on a console while the main (graphical) program is running in the forground. (This could be very usefull when using Kylix e.g.)
|
Here is a trace utility function given by Ken Reisdorph a long time ago
which outputs the result to a console:
template<class T>void Trace(const T& value)
{
static HANDLE handle;
if (!handle)
{
AllocConsole();
handle = GetStdHandle(STD_OUTPUT_HANDLE);
}
std::ostringstream oss;
oss<
std::string text=oss.str();
text+="n";
WriteConsole(handle,text.c_str(), text.length(), 0, 0);
}
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
|
|
| Back to top |
|
 |
Richard Ulrich Guest
|
Posted: Tue Mar 08, 2005 11:04 am Post subject: Re: Background console for GUI program. Possible? |
|
|
I had the same proble a while ago.
I found some code that should do exactly that. It showed a console
window, but cout was not redirected as the comments indicated.
Meanwhile I changed my code to use a function instead of cout to write
to the debug window.
Richard
Here's the code I'm using:
#ifndef DebugConsoleH
#define DebugConsoleH
//---------------------------------------------------------------------------
// Creates a debug console that lasts the duration of the DebugConsole
object
// lifetime, and syncs stdin/stdout/stderr and the C++ streams to print
there.
#include <windows.h>
class DebugConsole
{
public:
DebugConsole(unsigned short bufwid = 80, unsigned short bufheig
= 500);
~DebugConsole();
void LogMsg(char *msg);
private:
bool m_exists;
HANDLE stdouth;
};
#endif
#include "DebugConsole.h"
#include <iostream>
#include <fcntl.h>
#include <io.h>
#include <windows.h>
#include <assert.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
using namespace std;
typedef long intptr_t;
//---------------------------------------------------------------------------
DebugConsole::DebugConsole(unsigned short bufwid, unsigned short bufheig)
{
int ret, hConHandle;
intptr_t pStdHandle;
FILE* pFile;
char tmp[512];
m_exists = ::AllocConsole() != 0;
if(!m_exists)
{
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
0, tmp, sizeof(tmp)-1, NULL);
MessageBox(GetActiveWindow(), tmp, "Couldn't create debug
console!", MB_OK | MB_ICONERROR);
return;
}
stdouth = ::GetStdHandle(STD_OUTPUT_HANDLE);
//sync stdin, stdout, stderr fds up with the new console
//original source: http://www.halcyon.com/ast/dload/guicon.htm
//changes: removed all warnings by changing types and casting
// set the screen buffer to be big enough to let us scroll text
CONSOLE_SCREEN_BUFFER_INFO coninfo;
::GetConsoleScreenBufferInfo(stdouth, &coninfo);
coninfo.dwSize.X = bufwid;
coninfo.dwSize.Y = bufheig;
::SetConsoleScreenBufferSize(stdouth, coninfo.dwSize);
// redirect unbuffered STDOUT to the console
pStdHandle = reinterpret_cast<intptr_t>(stdouth);
SetConsoleMode(stdout,ENABLE_PROCESSED_OUTPUT |
ENABLE_WRAP_AT_EOL_OUTPUT);
hConHandle = _open_osfhandle(pStdHandle, O_TEXT);
pFile = _fdopen(hConHandle, "w");
*stdout = *pFile;
ret = setvbuf(stdout, NULL, _IONBF, 0);
if(ret)
{
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
0, tmp, sizeof(tmp)-1, NULL);
// MessageBox(GetActiveWindow(), tmp, "Could not set up debug
console window.", MB_OK | MB_ICONERROR);
}
/* not using stdin and stder for the time being
// redirect unbuffered STDIN to the console
pStdHandle = reinterpret_cast<intptr_t>(::GetStdHandle(STD_INPUT_HANDLE));
hConHandle = _open_osfhandle(pStdHandle, _O_TEXT);
pFile = _fdopen(hConHandle, "r");
*stdin = *pFile;
ret = setvbuf(stdin, NULL, _IONBF, 0);
// redirect unbuffered STDERR to the console
pStdHandle = reinterpret_cast<intptr_t>(::GetStdHandle(STD_ERROR_HANDLE));
hConHandle = _open_osfhandle(pStdHandle, _O_TEXT);
pFile = _fdopen(hConHandle, "w");
*stderr = *pFile;
ret = setvbuf(stderr, NULL, _IONBF, 0);
*/
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
// point to console as well
ios::sync_with_stdio(true);
ret = ret;
}
//---------------------------------------------------------------------------
DebugConsole::~DebugConsole()
{
if(m_exists)
{
::FreeConsole();
m_exists = false;
}
}
//---------------------------------------------------------------------------
void DebugConsole::LogMsg(char *msg)
{
DWORD byteswrritten;
WriteFile(stdouth, msg, strlen(msg), &byteswrritten, NULL);
if(strlen(msg) != byteswrritten)
MessageBeep(-1);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
|
|
| Back to top |
|
 |
Sam S. Firouz Guest
|
Posted: Tue Mar 08, 2005 6:50 pm Post subject: Re: Background console for GUI program. Possible? |
|
|
check out OutputDebugString and EventLog window of Builder IDE
Sam
"Koos van der Merwe" <kosie (AT) operamail (DOT) com> wrote
| Quote: |
Hi
I'm debugging somebody else's code and want to add simple debug messages
at certain points in the program to get a better idea of the program flow.
I would like to know if it is possible to use <iostream.h>'s cout << (and
cin >>) for debugging purposes on a console while the main (graphical)
program is running in the forground. (This could be very usefull when
using Kylix e.g.)
I would prefer to not have messageboxes popping up. Or a "debug" form that
disappears when the application shuts down. Anybody with experience of a
similar problem?
Thanks
Koos
|
|
|
| Back to top |
|
 |
evil Guest
|
Posted: Wed Mar 09, 2005 7:27 am Post subject: Re: Background console for GUI program. Possible? |
|
|
On 7 Mar 2005 06:23:24 -0800, Koos van der Merwe <kosie (AT) operamail (DOT) com> revealed:
| Quote: |
Hi
I'm debugging somebody else's code and want to add simple debug messages at certain points in the program to get a better idea of the program flow. I would like to know if it is possible to use <iostream.h>'s cout << (and cin >>) for debugging purposes on a console while the main (graphical) program is running in the forground. (This could be very usefull when using Kylix e.g.)
I would prefer to not have messageboxes popping up. Or a "debug" form that disappears when the application shuts down. Anybody with experience of a similar problem?
Thanks
Koos
|
To open a console
AllocConsole();
SetConsoleTitle("Your title here");
mF = freopen("CONOUT$", "wb", stdout);
and when done just do
fclose(mF);
FreeConsole();
....to clean it up.
With this you can use printf() and friends to output text. The console will
disappear when the application shuts down, though. But you could make your
own output functions that writes both to the console and a file that you can
review after the program terminates, and only does this when the program is
compiled in debug mode.
--
Acting stupid is often smart.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|