I want to be notified when a file associated with a std::istream is read from using the istream and the same with an ostream being written to. With this in mind I decided to try deriving my own std::basic_filebuf, and just to experiment I tried to override the xsgetn() and xsputn() functions so that they would output to the console before calling the parent implementation:
#include <iostream>
#include <fstream>
#include <cstdio>
class MyFileBuff : public std::filebuf
{
public:
std::streamsize xsgetn(char_type* s, std::streamsize count) override
{
std::cout << "using my xsgetn()" << std::endl;
return std::filebuf::xsgetn(s, count);
}
std::streamsize xsputn(const char_type* s, std::streamsize count) override
{
std::cout << "using my xsputn()" << std::endl;
return std::filebuf::xsputn(s, count);
}
using std::filebuf::basic_filebuf;
};
int main()
{
//open file
FILE* filePtr{ nullptr };
fopen_s(&filePtr, "text.txt", "w+");
//associate file with instance of custom std:filebuff
MyFileBuff fileBuff(filePtr);
//initalize i/o streams with file buffer
std::istream myIStream(&fileBuff);
std::ostream myOStream(&fileBuff);
//output to file
myOStream << "hello world" << std::endl;
const short buffSize{ 1024 };
char buff[buffSize];
myIStream.seekg(0);
//read from file
myIStream.getline(buff, buffSize);
//output what was read to console
std::cout << buff << std::endl;
return 0;
}
This outputs:
> using my xsputn()
> hello world
As you can see, my override of xsputn() is called on the write operation. But despite the read operation completing successfully, my override of xsgetn() is never called. I would like help understanding why.
I have also tried overloading the std::basic_filebuf::uflow() and std::basic_filebuf::underflow() functions in a similar manner and they do successfully output to the console when the istream is read, but I don't understand why xsgetn() never gets called?
Related
I try to find a way to catch the standard outputs in a C++ application. This app is quite large, using third parties and may use outputs in both C or C++ ways (assuming there are std::cout calls as well as printf calls)
we can simulate by something like:
void MyAwesomeApp()
{
std::cout << "I am starting" << std::endl;
std::cerr << "Getting a warning!\n";
printf("With old style print\n");
std::cout << "That's all folk!\n";
}
I already tried 2 approachs:
1. std::streambuf
class MyStreambuf : public std::streambuf {
public:
explicit MyStreambuf(const std::string& fileName = "" )
{
if (!fileName.empty())
{
_file1.open(std::filesystem::temp_directory_path() / fileName,std::ios::out);
_file2.open(std::filesystem::current_path() / fileName, std::ios::out);
}
if (_file1.is_open())
{
saveOut = std::cout.rdbuf();
std::cout.rdbuf(this);
saveErr = std::cerr.rdbuf();
std::cerr.rdbuf(this);
}
}
const std::string& GetOutput()const{return saveStr;}
~MyStreambuf() override
{
if (saveOut) std::cout.rdbuf(saveOut);
if (saveErr) std::cerr.rdbuf(saveErr);
_file1.close();
_file2.close();
}
protected:
std::streamsize xsputn(char_type const* s, std::streamsize count) override {
if (_file1.is_open()) _file1.write(s,count);
saveStr.append(s, static_cast<std::size_t>(count));
// .... DO SOME MODIFICATIONS ....
if (_file2.is_open()) _file2.write(s,count);
return count;
}
private:
std::ofstream _file1{};
std::ofstream _file2{};
std::string saveStr{};
std::streambuf* saveOut = nullptr;
std::streambuf* saveErr = nullptr;
};
So I can call, for example:
int main()
{
MyStreambuf outbuf;
MyAwesomeApp();
return !outbuf.GetOutput().empty()
}
I can have the 2 files filled as well as the string, but without the content of the printf call (that remains in the console)
2. dup2
using dup2 I can redirect all the stdout (and stderr) output to a given file. so I get all the contents of std::cout AND printf. but I can Only do that in a file that I have to read (an parse) at the end.
The application may run for a long time And we want to parse outputs during execution not waiting for the end.
Is it possible to have the benefits of the streambuf approach, but capable of catching also the printf outputs?
Context:
I'm editing a small piece of a large program. This large program is controlling std::cout and rerouting it so that a basic piece of code like:
std::cout << "I want to see the light of the terminal!" << std::endl;
Does not display anything to the user.
Question:
How can I get something to print directly to the terminal when my standard output/error is being rerouted? (If possible)
Other notes:
I realize that I could edit the larger program, but I'm looking to use this printout for some early diagnostics before more fully integrating my code into the program. Having to mess with how the program routes the output would really extend the development cycle.
I am also currently writing to a file as a workaround but that's slightly less desirable and frankly I would like to know how to do this in the future.
I think you can do it following these steps:
Save the redirected buffer
Change buffer to console
Get your job done
Again set buffer to the saved buffer in step 1
For example
#include <sstream>
#include <iostream>
void print_to_console() {
std::cout << "Hello from print_to_console()" << std::endl;
}
void foo(){
std::cout<<"hello world"<<std::endl;
print_to_console(); // this could be printed from anything
}
int main()
{
std::stringstream ss;
//change the underlying buffer and save the old buffer
auto old_buf = std::cout.rdbuf(ss.rdbuf());
foo(); //all the std::cout goes to ss
std::cout.rdbuf(old_buf); //reset
std::cout << "<redirected-output>\n"
<< ss.str()
<< "</redirected-output>" << std::endl;
}
I haven't tested it. I took the idea and example from this accepted answer.
For convenience, you can just write a function to print in console. This function will take care of the redirection and printing.
Write to stdout and read from stdin (both are FILE descriptors).
You can wrap them in stream classes if you wish.. IE: Using streambuf and iostreamto get the same functionality as cout.
#include <iostream>
int main(int argc, const char * argv[]) {
const char* data = "DATA TO PRINT";
fwrite(data, strlen(data), sizeof(char), stdout);
return 0;
}
Small example:
#include <iostream>
class stream : public std::streambuf
{
private:
int_type overflow(int_type c = traits_type::eof());
public:
stream() {}
virtual ~stream() {}
stream(const stream& other) = delete;
stream& operator = (const stream& other) = delete;
};
stream::int_type stream::overflow(stream::int_type c)
{
if (c != traits_type::eof())
{
fwrite(&c, 1, sizeof(c), stdout);
}
return c;
}
class mcout : public std::ostream
{
public:
mcout() : std::ostream(0), sbuf() {init(&sbuf);}
virtual ~mcout() {}
private:
stream sbuf;
} mcout;
int main(int argc, const char * argv[]) {
mcout << "HELLO\n";
return 0;
}
How to determine weather ostream is a file or a console stream. In the following program I want to print "Hello file!" while writing to a file and "Hello console!" while writing to console. What condition should I specify at line 17?
#include <fstream>
#include<iostream>
#include <string>
using namespace std;
class A{
public:
A(string msg):_str(msg){}
string str()const {return _str;};
private:
string _str;
};
ostream & operator << (ostream & os, const A & a)
{
if (os is ofstream) //this is line 17
os << "Hello file! " << a.str() << endl;
else
os << "Hello console! " << a.str() << endl;
return os;
}
int main()
{
A a("message");
ofstream ofile("test.txt");
if (!ofile)
cerr << "Unable to open file";
else
ofile << a; // "Hello file"
cout << a << endl; // "Hello console"
}
Maybe not pretty, but
std::streambuf const * coutbuf = std::cout.rdbuf();
std::streambuf const * cerrbuf = std::cerr.rdbuf();
ostream & operator << (ostream & os, const A & a)
{
std::streambuf const * osbuf = os.rdbuf();
if ( osbuf == coutbuf || osbuf == cerrbuf )
os << "Hello console! " << a.str() << endl;
else
os << "Hello file! " << a.str() << endl;
return os;
}
We could use &os == &std::cout, but the Standard output might be redirected to file, so I think it is better to use the streambuf object instead. (See this answer for better understanding as to how the redirection works, and why comparing streambuf solves the problem safely! )
You could (ab)use tellp(), which returns -1 if the stream does not have a position:
bool isConsoleStream(ostream const& stream)
{
return stream.tellp() == -1;
}
Of course, there could be other streams that return -1 for this function, so use with caution.
There is no portable means. Under Unix, you can do:
if ( (&os == &std::cout && isatty( STDOUT ))
|| (&os == &std::cerr && isatty( STDERR ))
|| (&os == &std::clog && isatty( STDERR )) ) }
// is a terminal...
}
Under Windows, the isatty becomes _isatty, and I'm not sure
that the macros exist (but I suspect that they do).
Of course, this supposes that you don't do things to confuse it
in your code. Something like:
std::ostream s( std::cout.rdbuf() );
for example, or:
std::cout.rdbuf( &someFileBuf );
Or even:
std::ofstream s( "/dev/tty" ); // (or "CONS" under Windows).
But it's about as close as you can get without the actual fd
from the filebuf.
One is a ofstream and the other is a ostream. Just have two methods.
#include <iostream>
#include <string>
#include <fstream>
class A {
std::string s;
public:
A(const std::string& s) : s(s){}
std::string str() const {return s;}
};
ostream & operator << (std::ostream & os, const A & a)
{
return os << "console: " << a.str() << std::endl;
}
ofstream & operator << (std::ofstream & os, const A & a)
{
return os << "file: " << a.str() << std::endl;
}
int main()
{
A a("hello world");
std::cout << a << endl;
}
This works on Visual Studio 2012
if (typeid(os) == typeid(ofstream)) //this is line 17
But an ostream could be something that isn't an ofstream or the console so you'd have to be careful.
Function to check if a C++ character stream is connected to a terminal/console/tty.
Ideally, we would use the file descriptor under-laying the stream buffer of the C++ stdio stream (cin, cout, cerr or clog).
However, there is no way to retrieve the under-laying file descriptor.
So, we use the fact that at program start-up the stdio stream buffers are connected to the program's standard input and output.
This function only works under the following conditions:
The stream buffers of the start-up C++ stdio streams must not change.
Because the addresses of the stream buffers of the start-up C++ stdio streams are used as identifiers.
For instance by deleting them and then allocating a new stream buffer that has the same address as one of these stream buffers of the start-up C++ stdio streams.
The program's stdio must not change after program start-up.
Because the TTY statuses of the stdio stream buffers are stored at program start-up.
For instance if at start-up the std. out is connected to a terminal and later it is redirected to a pipe or file by something external to the program.
[Instead of storing the TTY statuses at start-up you could retrieve them at run-time, but then you must make sure that your program (and all libraries it uses) does not change the stdio file descriptors (0, 1 and 2). Rember that the stdio stream buffers most likely use other (duplicate) file descriptors.]
Code:
#include <iostream>
extern "C" {
#ifdef _WIN32
# include <io.h> // for: _isatty()
#else
# include <unistd.h> // for: isatty()
#endif
}
// Stdio file descriptors.
#ifndef STDIN_FILENO
# define STDIN_FILENO 0
# define STDOUT_FILENO 1
# define STDERR_FILENO 2
#endif
// Store start-up addresses of C++ stdio stream buffers as identifiers.
// These addresses differ per process and must be statically linked in.
// Assume that the stream buffers at these stored addresses
// are always connected to their underlaying stdio files.
static const streambuf* const StdioBufs[] = {
std::cin.rdbuf(), std::cout.rdbuf(), std::cerr.rdbuf(), std::clog.rdbuf()
};
static const wstreambuf* const StdioWBufs[sizeof(StdioBufs)/sizeof(StdioBufs[0])] = {
std::wcin.rdbuf(), std::wcout.rdbuf(), std::wcerr.rdbuf(), std::wclog.rdbuf()
};
// Store start-up terminal/TTY statuses of C++ stdio stream buffers.
// These statuses differ per process and must be statically linked in.
// Assume that the statuses don't change during the process life-time.
static const bool StdioTtys[sizeof(StdioBufs)/sizeof(StdioBufs[0])] = {
#ifdef _WIN32
_isatty(STDIN_FILENO), _isatty(STDOUT_FILENO), _isatty(STDERR_FILENO), _isatty(STDERR_FILENO)
#else
isatty(STDIN_FILENO), isatty(STDOUT_FILENO), isatty(STDERR_FILENO), isatty(STDERR_FILENO)
#endif
};
// Is a Terminal/Console/TTY connected to the C++ stream?
// Use on C++ stdio chararacter streams: cin, cout, cerr and clog.
bool isTTY(const ios& strm)
{
for(unsigned int i = 0; i < sizeof(StdioBufs)/sizeof(StdioBufs[0]); ++i) {
if(strm.rdbuf() == StdioBufs[i])
return StdioTtys[i];
}
return false;
}
// Is a Terminal/Console/TTY connected to the C++ stream?
// Use on C++ stdio wide-chararacter streams: wcin, wcout, wcerr and wclog.
bool isTTY(const wios& strm)
{
for(unsigned int i = 0; i < sizeof(StdioWBufs)/sizeof(StdioWBufs[0]); ++i) {
if(strm.rdbuf() == StdioWBufs[i])
return StdioTtys[i];
}
return false;
}
Note: I've only tested it on Linux.
#include <iostream>
#include <fstream>
using namespace std;
void foo(){
streambuf *psbuf;
ofstream filestr;
filestr.open ("test.txt");
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
}
int main () {
foo();
cout << "This is written to the file";
return 0;
}
Does cout write to the given file?
If not, is there a way to do it without sending the variables to foo, like new?
update :
I can't use a solution that uses class or uses global so plz can some
give me solution that use new. Also passing the from main to foo
streambuf *psbuf;
ofstream filestr;
should work right?
I am trying to do this but its not working?
I pass the stream to foo so it exist in the main so it wont end when foo finish.
void foo(streambuf *psbuf){
ofstream filestr;
filestr.open ("test.txt");
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
}
int main () {
streambuf *psbuf
foo(psbuf);
cout << "This is written to the file";
return 0;
}
I suspect that by now compiled and run your code and found that you get a segmentation fault.
You are getting this because you create and open an ofstream object within foo(), which is then destroyed (and closed) at the end of foo. When you attempt to write to the stream in main(), you attempt to access a buffer which no longer exists.
One workaround to this is to make your filestr object global. There are plenty of better ones!
Edit: Here is a better solution as suggested by #MSalters:
#include <iostream>
#include <fstream>
class scoped_cout_redirector
{
public:
scoped_cout_redirector(const std::string& filename)
:backup_(std::cout.rdbuf())
,filestr_(filename.c_str())
,sbuf_(filestr_.rdbuf())
{
std::cout.rdbuf(sbuf_);
}
~scoped_cout_redirector()
{
std::cout.rdbuf(backup_);
}
private:
scoped_cout_redirector();
scoped_cout_redirector(const scoped_cout_redirector& copy);
scoped_cout_redirector& operator =(const scoped_cout_redirector& assign);
std::streambuf* backup_;
std::ofstream filestr_;
std::streambuf* sbuf_;
};
int main()
{
{
scoped_cout_redirector file1("file1.txt");
std::cout << "This is written to the first file." << std::endl;
}
std::cout << "This is written to stdout." << std::endl;
{
scoped_cout_redirector file2("file2.txt");
std::cout << "This is written to the second file." << std::endl;
}
return 0;
}
It seems to me that your code should work but ... Why don't you try yourself ? You will see if everything is written in test.txt or not.
I saw a useful start here:
http://www.cs.technion.ac.il/~imaman/programs/teestream.html
And it works great to make a new stream which goes to both clog and a log file.
However, if I try to redefine clog to be the new stream it does not work because the new stream has the same rdbuf() as clog so the following has no effect:
clog.rdbuf(myTee.rdbuf());
So how can I modify the tee class to have its own rdbuf() which can then be the target of clog?
Thanks.
-William
If you really want to keep using std::clog for the tee instead of sending output to a different stream, you need to work one level lower: Instead of deriving from ostream, derive from streambuf. Then you can do this:
fstream logFile(...);
TeeBuf tbuf(logFile.rdbuf(), clog.rdbuf());
clog.rdbuf(&tbuf);
For more information on how to derive your own streambuf class, see here.
You don't want to do what your've trying to do because the 'tee' is not working at the rdbuf level. So setting the rdbuf to something else will not work, the output will only go to one stream.
You need to follow there example:
e.g.
fstream clog_file(...);
xstream clog_x(...);
TeeStream clog(clog_file, clog_x);
then use clog everywhere instead of your original clog.
Here is the class I created that seems to do the job, thanks to all who helped out!
-William
class TeeStream : public std::basic_filebuf<char, std::char_traits<char> >
{
private:
class FileStream : public std::ofstream {
public:
FileStream()
: logFileName("/my/log/file/location.log") {
open(logFileName.c_str(), ios::out | ios::trunc);
if (fail()) {
cerr << "Error: failed to open log file: " << logFileName << endl;
exit(1);
}
}
~FileStream() {
close();
}
const char *getLogFileName() const {
return logFileName.c_str();
}
private:
const string logFileName;
};
public:
typedef std::char_traits<char> traits;
typedef std::basic_filebuf<char, traits> baseClass;
TeeStream()
: baseClass(),
_logOutputStream(),
_clogBuf(clog.rdbuf()),
_fileBuf(_logOutputStream.rdbuf()) {
clog.rdbuf(this);
_logOutputStream << "Log file starts here:" << endl;
}
~TeeStream() {
clog.rdbuf(_clogBuf);
}
int_type overflow(char_type additionalChar =traits::eof()) {
const int_type eof = traits::eof();
const char_type additionalCharacter = traits::to_char_type(additionalChar);
const int_type result1 = _clogBuf->sputc(additionalCharacter);
const int_type result2 = _fileBuf->sputc(additionalCharacter);
if (traits::eq_int_type(eof, result1)) {
return eof;
} else {
return result2;
}
}
int sync() {
const int result1 = _clogBuf->pubsync();
const int result2 = _fileBuf->pubsync();
if (result1 == -1) {
return -1;
} else {
return result2;
}
}
private:
FileStream _logOutputStream;
streambuf * const _clogBuf;
streambuf * const _fileBuf;
};
I would just use the Boost iostreams stuff to do it.
#include <iostream>
#include <fstream>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
int main(const int a_argc, const char *a_args[])
{
namespace io = boost::iostreams;
typedef io::tee_device<std::ofstream, std::ostream> TeeDevice;
typedef io::stream<TeeDevice> TeeStream;
std::ofstream flog("logFile.txt");
//We need to copy clog, otherwise we get infinite recursion
//later on when we reassign clog's rdbuf.
std::ostream clogCopy(std::clog.rdbuf());
TeeDevice logTee(flog, clogCopy);
TeeStream logTeeStream(logTee);
logTeeStream << "This text gets clogged and flogged." << std::endl;
//Modify clog to automatically go through the tee.
std::streambuf *originalRdBuf = std::clog.rdbuf(logTeeStream.rdbuf());
std::clog << "This text doesn't only get clogged, it's flogged too." << std::endl;
std::clog.rdbuf(originalRdBuf);
std::clog << "This text avoids flogging." << std::endl;
}