I just want a way to have a function ( or anything else ) which have one standard argument and another one coming from the operator <<.
my_comp(argument) << "HelloWorld" ;
The goal is to purpose a Logger class that any other class can easily extends and call its unique functionality with a single line of code.
The Logger class mainly use boost/log/ packages, and its unique functionally may be write many time. This is why I want it in a single line.
I do not authorize myself to use the function:
BOOST_LOG_SEV(argument_boost_dependent, argument_logger_class_dependent) << "something"
because I do not want any dependency with boost on my interface.
So I'm trying to do something like this:
loggit(argument_logger_class_dependent) << "something"
and just call BOOST_LOG_SEV
with boosts arguments in the class implementation
For now I just have a struct that extends std::streambuf so it only work like this: loggit << "HelloLog"
or by overriding operator () loggit(severity_level::warning)
but both together do not work.
If anyone know how to add this sweety argument, would be welcome:)
Thanks
Slightly improvising on @Swordfish's answer: instead of calling a constructor each time, one can overload the operator() as below:
#include <iostream>
struct foo
{
int bar;
foo() {};
foo& operator() (int arg)
{
bar = arg;
return *this;
}
foo& operator<<(std::string baz)
{
std::cout << bar << ' ' << baz;
// call BOOST_LOG_SEV with arg and baz
return *this;
}
};
int main()
{
foo logger;
logger(42) << "Hells in the World!\n"; //prints " 42 Hells in the World!"
logger(32) << "So is heaven\n"; //prints "32 So is heaven"
}
#include <iostream>
struct foo
{
int bar;
foo(int bar) : bar{ bar } {};
foo& operator<<(std::string baz)
{
std::cout << bar << ' ' << baz;
return *this;
}
};
int main()
{
foo(42) << "Hells in the World!\n";
}
Makes sense? No? Question answered?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.