2

I'm using a library called spdlog for logging. I'd like to build my own Logger around the library so that I have the option of adding 'extra' functionality that's specific to my application.

I was able to get this code below working:

#include <spdlog/spdlog.h>

int main()
{
  spdlog::log(spdlog::level::level_enum::info, "this is an info message");
  return 0;
}

As you can see, logging levels are available via the enum that's namespaced at spdlog::level::level_enum.

I may be overcomplicating this, but if I create my own Logger class will I have to expect classes using my logger to type out the entire enum's namespace in their logging function calls?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Max
  • 808
  • 11
  • 25

2 Answers2

8

but if I create my own Logger class will I have to expect classes using my logger to type out the entire enum's namespace in their logging function calls

This is where you can use using. Something like:

using info = spdlog::level::level_enum::info;

that would prevent from typing the whole thing every time you need to use it. All you only need to use is info then.

artm
  • 17,291
  • 6
  • 38
  • 54
0

if I create my own Logger class will I have to expect classes using my logger to type out the entire enum's namespace in their logging function calls?

I would recommend your class define its own info value and then map it internally to spdlog's info value when needed. spdlog is an implementation detail of your class's internals, so don't expose spdlog outside of your class, keep it hidden if you can help it. That also allows you to swap out another logger library in the future, if you choose to, without breaking code that uses your class.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770