stdout

Started by
8 comments, last by SW INC 18 years, 9 months ago
not sure that this is the correct place for this but i need help in dealing with stdout iknow how to redirect stdout from the console to a file but i need stdout to go to the console AND a log file i cant find any information on google about copying this any help please
Advertisement
What operating system and programming language are you using?
Assuming you are using C++, you could write a function that writes it to the console through cout, and writes it to a file using fstream. If you're using SDL, anything using cout automatically goes to stdout.txt.
c++ devc++ windows
yes Oberon thats what i'm trying to do but i dont know how to catch the stuf from cout
all i have been able to find information on is redirecting to a file, nothing more
this is what i found for the file redirection

if( freopen("Logfile.txt", "w", stdout) == NULL)
{
MessageBox(NULL,"Error Redirecting stdout", "Error", MB_OK);
}
else
{
setbuf( stdout, NULL );
}

i just cant find out how to intercept stdout
is there a way that stdout will be driected into a function instead of the console?

ADD::
ohh
can i use window's SetStdHandle to set the stdout into a pipe and then read it later?
thats all i cant think of sleep is pulling me down now....
zzzzzzzzzzzzzzzzz
May I ask why you're using stdout when you could be using cout just as easily? In fact, its probably a little bit more easier! Just a question though. If you aren't sure on how to use cout, its rather easily:
#include <iostream> // for std::cout, cin, endl#include <string>using namespace std;int main(void){  cout << "Hello World" << endl;  cout << "Hello" << ' ' << "World" << endl;    string szLine("Hello World");  cout << szLine << endl;}

Just some examples! Good luck!
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
uhhh thanks
i AM using cout, and sprintf and printf in libraries
i need to catch it
The easiest method to do this would probably to spawn a child process that used the C++ stream facility to direct output to both a file and stdout, using a pipe to redirect the parent's stdout to the child's stdin.

Here is an article creating a child process with redirected standard in and here is source to a program that would dump stdin to a file and stdout.
#include <iostream>#include <fstream>class twinbuf : public std::streambuf {  protected:    std::streambuf * buf1_;    std::streambuf * buf2_;  public:    twinbuf(std::streambuf * buf1, std::streambuf * buf2)      : buf1_(buf1), buf2_(buf2) {}    virtual ~twinbuf() {      sync();    }  protected:    virtual int_type overflow (int_type c) {      if (c != EOF) {         buf1_->sputc(traits_type::to_char_type(c));         buf2_->sputc(traits_type::to_char_type(c));      }      return c;    }    virtual int sync () {      if (buf1_->pubsync() == -1) return -1;      if (buf2_->pubsync() == -1) return -1;      return 0;    }};int main(int argc, char ** argv) {  if (argc != 2) {    return -1;  }  std::ofstream ofs(argv[1]);  if (!ofs.good()) {    return -1;  }    twinbuf twin(ofs.rdbuf(), std::cout.rdbuf());   std::cout.rdbuf(&twin);  std::cout << std::cin.rdbuf();  return 0;}

thanks sicrane thats what i was looking for

does the child process continually dump what it recieves or does it need a while/for statement to output the redirected stuff

now i have my main program create a pipe
create the child process and pass the pipe's read handle to the child
and the child dups it to a console and file
until the parent exits or dies


my head hurts
With the source code from above, the child will keep dumping as long as std::cin doesn't fail (like with a borken pipe), so you don't need an explicit while loop.
ok i have this

int CreateListener(void){// create a pipe    SECURITY_ATTRIBUTES saAttr;    bool fSuccess;    // Set the bInheritHandle flag so pipe handles are inherited.     saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);    saAttr.bInheritHandle = TRUE;    saAttr.lpSecurityDescriptor = NULL;    // Create a pipe for the child process's STDOUT.     if (! CreatePipe(&piperead, &pipewrite, &saAttr, 0))        return 0;    // set the stdoutput    SetStdHandle(STD_OUTPUT_HANDLE,pipewrite);// start up the child with pipe handle    PROCESS_INFORMATION piProcInfo;     STARTUPINFO siStartInfo;    bool CreateRet = false;         // Set up members of the PROCESS_INFORMATION structure.     ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );        // Set up members of the STARTUPINFO structure.     ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );    siStartInfo.cb = sizeof(STARTUPINFO);     siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);    siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);    siStartInfo.hStdInput = piperead;    siStartInfo.dwFlags |= STARTF_USESTDHANDLES;        // Create the child process.     CreateRet = CreateProcess(listenerexe,         listenerlogfile,       // command line         NULL,          // process security attributes         NULL,          // primary thread security attributes         TRUE,          // handles are inherited         0,             // creation flags         NULL,          // use parent's environment         NULL,          // use parent's current directory         &siStartInfo,  // STARTUPINFO pointer         &piProcInfo);  // receives PROCESS_INFORMATION         if (CreateRet == 0)        return 0;    else    {        CloseHandle(piProcInfo.hProcess);        CloseHandle(piProcInfo.hThread);    }    return 1;}


the text appears in the console window but the log file is empty and i dont think that the output from my app is traveling through the pipe into the child
in the child process if i add a cout << "whatever" before the std::cout << std::cin.rdbuf();
it appears in the log twice but not in the console
if i place it after std::cout << std::cin.rdbuf(); noting come out any where

This topic is closed to new replies.

Advertisement