Constructing log messages with streams
Having a log
function that accepts a single string to display is not the easiest function to use. Sometimes, we might want to log more information. We might also want to log different types and not just strings. This is where we can use the powerful streaming ability used extensively in C++.
We’re already using a stream inside the log
function. All we need to do to give the full power of the stream to the caller is to stop sending the single message text to the stream inside the log
function and instead return the stream itself to the caller. The caller will then be free to stream whatever is needed.
We can see what this will look like by first modifying the test; we have to use the log
function as if it returned a stream. The modified test looks like this:
TEST("Simple message can be logged")
{
std::string message = "simple ";
message += Util::randomString();...