I've created an ofstream and there is a point in which I need to check if it's empty or has had things streamed into it.
Any ideas how I would go about doing this?
The std::ofstream files don't support this directly. What you can do if this is an important requirement is to create a filtering stream buffer which internally used std::filebuf but also records if there was any output being done. This could look look as simple as this:
struct statusbuf:
std::streambuf {
statusbuf(std::streambuf* buf): buf_(buf), had_output_(false) {}
bool had_output() const { return this->had_output_; }
private:
int overflow(int c) {
if (!traits_type::eq_int_type(c, traits_type::eof())) {
this->had_output_ = true;
}
return this->buf_->overflow(c);
}
std::streambuf* buf_;
bool had_output_;
};
You can initialize an std::ostream with this and query the stream buffer as needed:
std::ofstream out("some file");
statusbuf buf(out.rdbuf());
std::ostream sout(&buf);
std::cout << "had_output: " << buf.had_output() << "\n";
sout << "Hello, world!\n";
std::cout << "had_ouptut: " << buf.had_output() << "\n";
you could use ofstream.rdbuff to get the file buffer and than use streambuf::sgetn to read it. I believe that should work.
Related
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?
Consider the following C code:
void openFile(const char *mode, char *filename, FILE **fileptr)
{
...
*fileptr = fopen(filename, mode);
...
}
FILE *logstream;
if (LOG_FILE_ENABLED)
{
openFile("w", "mylogfile.txt", logstream);
}
else
{
logstream = stderr;
}
fprintf(logstream, "[DEBUG] Some debug message...\n");
fclose(logstream);
I am attempting to translate this to idiomatic C++. How can I overload openFile() such that it takes a std::ofstream, but keep logstream stream-agnostic? I was assuming it would be something like this:
void openFile(const char *mode, char *filename, std::ofstream &ofs)
{
...
ofs.open(filename);
...
}
std::ostream logstream;
if (LOG_FILE_ENABLED)
{
logstream = std::ofstream();
openFile("w", "mylogfile.txt", logstream);
}
else
{
logstream = std::cerr;
}
logstream << "[DEBUG] Some debug message..." << std::endl;
logstream.close();
However this is apparently wildly incorrect - you can't even initialize a plain std::ostream like that. How should I handle this - preferably while avoiding the use of raw pointers?
I would move the actual work to a separate function or lambda that takes a std::ostream as input. The caller can then decide which type of std::ostream to pass in, eg:
void doRealWork(std::ostream &log)
{
...
log << "[DEBUG] Some debug message..." << std::endl;
...
}
if (LOG_FILE_ENABLED)
{
std::ofstream log("mylogfile.txt");
doRealWork(log);
}
else
{
doRealWork(std::cerr);
}
Or:
auto theRealWork = [&](std::ostream &&log)
{
...
log << "[DEBUG] Some debug message..." << std::endl;
...
}
if (LOG_FILE_ENABLED) {
theRealWork(std::ofstream{"mylogfile.txt"});
} else {
theRealWork(static_cast<std::ostream&&>(std::cerr));
}
UPDATE: Otherwise, you can do something more like this instead:
using unique_ostream_ptr = std::unique_ptr<std::ostream, void(*)(std::ostream*)>;
unique_ostream_ptr logstream;
if (LOG_FILE_ENABLED) {
logstream = unique_ostream_ptr(new std::ofstream("mylogfile.txt"), [](std::ostream *strm){ delete strm; });
} else {
logstream = unique_ostream_ptr(&std::cerr, [](std::ostream *){});
}
*logstream << "[DEBUG] Some debug message...\n";
Or:
using shared_ostream_ptr = std::shared_ptr<std::ostream>;
shared_ostream_ptr logstream;
if (LOG_FILE_ENABLED) {
logstream = std::make_shared<std::ofstream>("mylogfile.txt");
} else {
logstream = shared_ostream_ptr(&std::cerr, [](std::ostream*){});
}
*logstream << "[DEBUG] Some debug message...\n";
C++ stream library has pretty ancient design.
Nevertheless - its basic idea is that ostream or istream are just wrapper objects over stream-buffers.
So you might try something like in this code:
std::ostream get_log(bool str) {
if (str) return std::ostream(new std::stringbuf());
// else
std::filebuf* f = new std::filebuf();
f->open("log", std::ios_base::out);
return std::ostream(f);
}
But, as I mentioned, this is very ancient design - so no RAII - this buffer is not owned by stream - you would need to delete it by yourself:
int main() {
std::ostream log = get_log(true);
log << "aaa";
std::cout << static_cast<std::stringbuf&>(*log.rdbuf()).str();
delete log.rdbuf(); // (!)
}
So this is not very usable.
So my final advice - use smart pointer over ostream - like this:
std::unique_ptr<std::ostream> get_log(bool str) {
if (str) return new std::ostringstream();
std::ofstream* f = new std::ofstream();
f->open("log", std::ios_base::out);
return f;
}
int main() {
auto log = get_log(true);
*log << "aaa";
}
You were nearly there; you just need to be mindful of scoping rules, and of the fact that there is no such thing as an std::ostream other than as an abstract base class.
So:
std::ostream* logstreamPtr = nullptr;
std::ofstream ofs;
if (LOG_FILE_ENABLED)
{
logstreamPtr = &ofs;
openFile("w", "mylogfile.txt", ofs);
}
else
{
logstreamPtr = &std::cerr;
}
std::ostream& logstream = *logstreamPtr;
logstream << "[DEBUG] Some debug message..." << std::endl;
logstream.close();
You don't need the reference logstream, but it saves you from having to repeatedly reference logstreamPtr later, which would be boring.
Don't be afraid of this raw pointer. This is the purest application of pointers there is. You can go down the smart pointer route if you like, but you gain nothing and lose readability (and, in some cases, performance).
By the way, if you're worried about performance, don't open and close the log file for every single message; that's extremely wasteful.
Without using a full-blown logging library (or IF statements) - is there a way in C++ to sometimes print out messages to the console and sometimes not?
I am using std::cerr, is there a way to control when this outputs or not?
Ideally I could have:
std::cerr << "Constructor called" << endl;
and have a way to enable/disable this line of code?
I am not sure what you mean by "without if", but you can write code without using the if yourself. A macro can check a flag for you.
#define CERR if (cerr_disabled) {} else std::cerr
bool cerr_disabled = false;
Then, in your code:
CERR << "error message" << std::endl;
If cerr_disabled is true, then nothing is printed.
The advantage of this macro approach is that none of the print arguments get evaluated if the err logging is disabled. For instance, if you needed to call a function to create a more complicated log string:
std::string fancy_log_message () {
//...
}
CERR << fancy_log_message();
If cerr_disabled is true, fancy_log_message() is not called. This is something that can't be achieved by just suppressing the stream object itself.
The simple approach is to set/clear std::ios_base::failbit on the stream: while std::ios_base::failbit is set, the streams won't do any work [unless the output operators are written incorrectly]:
std::cerr.setstate(std::ios_base::failbit);
std::cerr << "this won't show\n";
std::cerr.clear();
std::cerr << "this will show!\n";
To make these operations easier to use you can create manipulators, e.g.:
std::ostream& stream_on(std::ostream& out) {
out.clear();
return out;
}
std::ostream& stream_off(std::ostream& out) {
out.setstate(std::ios_base::failbit);
return out;
}
std::cerr << stream_off << "not printed\n" << stream_on << "printed\n";
If you really want to disable the stream even if the output operators are badly implemented, you can save the current rdbuf() (e.g., in a suitable std::ostream::pword()) and set the stream buffer to nullptr:
static int stream_off_index() { static int rc = std::ios_base::xalloc(); return rc; }
std::ostream& stream_on(std::ostream& out) {
out.pword(stream_off_index) = out.rdbuf(nullptr);
return out;
}
std::ostream& stream_off(std::ostream& out) {
if (!out.rdbuf()) {
out.rdbuf(out.pword(stream_off_index);
}
return out;
}
Here is a solution without macros:
#include <iostream>
void toggle_cerr()
{
static std::streambuf* p = std::cerr.rdbuf();
std::cerr.rdbuf(std::cerr.rdbuf() ? nullptr : p);
}
int main()
{
toggle_cerr();
std::cerr << "str";
}
Redirecting cout or cerr to a file is easy enough. I can use this to redirect third party output to a file. However, after I have redirected the third party output to a file, how do I then use cout myself to output to the console?
I'm a great fan of RAII, so I once wrote this small helper class. It will redirect the stream until it goes out of scope, at which point it restores the original buffer. Quite handy. :)
class StreamRedirector {
public:
explicit StreamRedirector(std::ios& stream, std::streambuf* newBuf) :
savedBuf_(stream.rdbuf()), stream_(stream)
{
stream_.rdbuf(newBuf);
}
~StreamRedirector() {
stream_.rdbuf(savedBuf_);
}
private:
std::streambuf* savedBuf_;
std::ios& stream_;
};
Can be used like this:
using namespace std;
cout << "Hello stdout" << endl;
{
ofstream logFile("log.txt");
StreamRedirector redirect(cout, logFile.rdbuf());
cout << "In log file" << endl;
}
cout << "Back to stdout" << endl;
You save the buffer and restore it later:
std::streambuf *buf = std::cout.rdbuf(); //save
// Do other stuff
std::cout.rdbuf(buf); // restore
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;
}