There are two problems I see above. The first is forking your message (to both a file, and the console). The second is wrapping what is written with some extra stuff.
meta_stream
handles the operator<<
overloading. It uses CRTP to statically dispatch to its child type:
template<class D, class substream>
struct meta_stream {
D& self() { return *static_cast<D*>(this); } // cast myself to D
// forwarders of operator<<
template<class X>
friend D& operator<<( meta_stream<D>& d, X const& x ) {
d.self().write_to(x);
return d.self();
}
friend D& operator<<(
meta_stream<D>& d,
substream&(*mod_func)(substream&)
) {
d.self().write_to(mod_func);
return d.self();
}
};
I had to override <<
twice because of how std::endl
and other modifiers work -- they are the name of an overloaded function.
This solves the problem of outputing the same string to two different ostreams:
template<class substream>
struct double_ostream:
meta_stream<double_ostream<substream>,substream>
{
substream* a = nullptr;
substream* b = nullptr;
template<class X>
void write_to( X&&x ) {
if (d.a) (*d.a) << x;
if (d.b) (*d.b) << std::forward<X>(x);
}
double_ostream( std::basic_ostream<CharT>* a_, std::basic_ostream<CharT>* b_ ):
a(a_), b(b_)
{}
double_ostream(double_ostream const&)=default;
double_ostream()=default;
double_ostream& operator=(double_ostream const&)=default;
};
note the use of CRTP via meta_stream
. I just have to implement write_to
.
First, write your 4 loggers to this array:
enum loglevel {
debug, error, warning, info
};
double_stream<std::ostream> loggers[4];
giving each a pointer to a std::cout
and a pointer to a (stored elsewhere) stream wrapping a file you want to save the log to. You can pass nullptr
if you don't want that level to be logged to that output stream (say, in release, skip debug logs), and you can log stuff to different log file (debug to one file, info to another).
double_stream<std::ostream> log( loglevel l ) {
double_stream<std::ostream> retval = loggers[l];
std::string message;
// insert code to generate the current date here in message
// insert code to print out the log level here into message
retval << message;
return retval;
}
now log(debug) << "hello " << "world\n"
will write your message for you.
You can do more fancy stuff if you don't want to write the newline at the end of the log message, but I doubt it is worth it. Just write the newline.
If you really want that feature:
template<class substream>
struct write_after_ostream:
meta_stream<write_after_ostream<substream>,substream>
{
substream* os = nullptr;
template<class X>
void write_to( X&&x ) {
if (os) *os << std::forward<X>(x);
}
~write_after_ostream() {
write_to(message);
}
write_after_ostream( substream* s, std::string m ):
os(s), message(m)
{}
std::string message;
}
write_after_ostream<double_stream<std::ostream>> log( loglevel l ) {
// note & -- store a reference to it, as we will be using a pointer later:
double_stream<std::ostream>& retval = loggers[l];
std::string pre_message;
// insert code to generate the current date here in pre_message
// insert code to print out the log level here into pre_message
retval << pre_message;
return {&retval, "\n"};
}
but I don't think it is worth it.
LOG(level) << "hello " << "world"
? Do you want[Thu Jan 29 18:32:11 2015] [1] hello [Thu Jan 29 18:32:11 2015] [1] world
? That seems like a bad idea. – Kenleigh[Thu Jan 29 18:32:11 2015] [1] hello world
– Nomination