c++argumentsoperatorsstreambuf

How to add argument to stream buffer c++


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


Solution

  • #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?