C++ logger with channels through bitmask operations.

Javier Carracedo
2 min readOct 8, 2023

--

Some couple of (wood) logs 😜

This a simple proof of concept about how the classic bit mask operations in C++ can help us to build a powerful message logger with different channels to be used by a client.

In a couple of lines you can defining the core of the idea. First of all, you can define the different levels of the logger to manage information relative to the operations that you want. Here, I am only defining five levels, but I can define more until the limit of the enum size (uint16_t).

The most powerful of the bitmask operations is different operations that you can to do. On this case, I am defining:

  • Example A: Selecting only two channels (INFO and WARNING channels)
  • Example B: Selecting all channels less two (INFO and ERROR channels).

Here it´s the code example:

You can see the console output:

ample_A_log_level: 6
Message: Activated LoggerChannel::INFO
Message: Activated LoggerChannel::WARNING
sample_B_log_level: 65525
Message: Activated LoggerChannel::TRACE
Message: Activated LoggerChannel::WARNING
Message: Activated LoggerChannel::CRITICAL

In conclusion, I would like to said that it´s a good point to use this implementation in big applications that manage a different logics and scale horizontally adding new functionalities. For example, defining differents values into the enun to filter the information that you want to see.

--

--

Javier Carracedo

Hi! SW Engineer from León (Spain) . I ❤ my work, I like improve and to grow my knowledge about different technologies. SW Engineer at HP SCDS - León (Spain)