- Solution
Explore three solutions for message logging in C++ through if-else statements, switch cases, and dispatch tables. Learn how to use enumerations with std::steady_clock to track elapsed time and understand dynamic and static polymorphism concepts applicable to design patterns in C++.
Solution 1: Using the if Statement
Explanation
Note:
std::cerrof the classstd::ostreamrepresents the standard error stream. This is not a runtime error.
The function writeMessage in line 25 displays the elapsed time in seconds in line 27 since the start of the program and a log message. It uses an enumeration in line 6 for the message severity. We used the start time in line 12 and the current time in line 15 to calculate ...