If I'm parsing it correctly...
1 2 3
|
(m_loglevel > 1) ?
((m_loglevel < 2 && m_loglevel > 0) ? "error" : "warning")
: "info";
|
Let's follow the code:
You set loglevel to 2.
Then, outer ternary sees if loglevel > 1.
It is, so it goes into your nested ternary.
Then, inner ternary sees if loglevel < 2.
It isn't, so it sets it to warning.
So, your logic is just wrong.
(Furthermore, in my opinion, it's simply hard to parse as a human without indentation, even if it was correct).
I don't even suggest using ternary operator here. I'll edit my post in a few minutes with an alternative.
Edit:
1 2 3 4 5
|
void log(std::string message)
{
static const char* log_level_LUT[] = { "info", "warning", "error" };
std::cout << log_level_LUT[m_loglevel] << " " << message << " " << m_loglevel << '\n';
}
|
PS:
(m_loglevel < 2 && m_loglevel > 0) |
This is logically equivalent to
m_loglevel == 1
PPS: Why add unnecessary state to your logger?
Why not just do "log.error()", "log.warning()" "log.info()"?
Or, log.log("message", Log::Error), log.log("message", Log::Warning), etc.