C++ logging using ostream

前端 未结 3 1672
鱼传尺愫
鱼传尺愫 2021-01-17 04:02

I\'m making a logger. I want to create a function log() that takes a stream as input.

For instance:

log(\"hello\"<<\" \"<<\"         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-17 04:59

    Have log() return a temporary object that buffers all output in memory. The destructor for this object will run at the end of the expression, and should flush the accumulated data to the actual stream in a single atomic operation (up to you to make that operation atomic).

    That will make your second syntax feasible:

    log()<<"hello"<<"world"<<10<<"\n";
    

提交回复
热议问题