-1

I'm currently writing a library in C++ and was wondering if I should log from within it. Googling the issue I came across this question but it makes reference to a logging facade. Is there anything equivalent for C++?

  • In the debug build, it might make sense for you to log something. Maybe. Are you sure you are qualified to determine what should be, and can be, logged, without too much overhead? – Deduplicator Jan 26 at 15:45
  • That's what log levels are for, no? – ruipacheco Jan 26 at 16:00
  • Not really. Or at least not completely. It's both a compile-time question (what logging is compiled in) and a run-time question (what logging is skipped / discarded). – Deduplicator Jan 26 at 16:18
  • related: See Logging for Libraries overview in this post (written from the .NET perspective). – Nick Alexeev Jan 26 at 22:11
  • Logging facades are kinda solution to the logging system mess of Java, as well as advanced library repository systems like Maven's and Gradles, with differeny libraries using different logging. C++ as such doesn't have a similar logging ecosystem. For Linux/Mac/Unix look into syslog. – hyde Jan 27 at 15:29
4

The facade pattern is not really what you want here, as that’s more a way to hide the complexity of a system behind a simpler api, which IMO is not really relevant to your problem.

As the user of your library, I’ll probably be grateful for useful log messages. However, I would prefer those messages to end up along with those of the application and those of other libraries. If you hard code logging through stdout, or decide to use any arbitrary logging library, I may not be able to configure it the way I need to. Thus, you want to log through an interface, normally a pure virtual class in C++, for which you may supply a default or a null implementation if you like, but for which you also let the library user provide an implementation.

If I use a number of different libraries that all offer an interface for logging, sure I need to implement all of those interfaces to use my logging library of choice, but it’s normally way simpler than trying to make potentially the same number of logging libraries work well together for the whole application.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.