I have somewhat different suggestion:
1. Users of your logging interface may want to write their own logging function, but retain
some internals.
2. You may want to have different behavior (ex. different logging color in console) depending on what's being logged.
3. You want to conditionally enable disable logging.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// logging.h
#include <string>
namespace logging
{
namespace detail
{
void Prettify(std::string& ugly_text);
}
void LogThis(const std::string& text);
}; // namespace logging
// conditionally use logging functionality
#ifdef USE_LOGGING
#define TRACE(text) logging::LogThis(text)
#else
#define TRACE(text) static_cast<void>(0)
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// logging.cpp
namespace logging
{
namespace detail
{
// TODO: provide overloads or color enum for different formatting behavior
void Prettify(std::string& ugly_text) {
// making it pretty...
}
}
void LogThis(const std::string& text) {
detail::Prettify(text);
std::cout << "Log: " << text << '\n';
}
}
|
Now for point one, some user or you can write it's own logging function outside this header, but reuse your formatting internals.
In point 2, you can achieve different behavior by using inline namespaces or by providing color enum and pass it to prettify function. Or you could write formatting overloads all in detail namespace.
And finally, logging will happen only if
USE_LOGGING
is defined, otherwise logging is disabled.
to use logging you would write:
1 2
|
#define USE_LOGGING
TRACE("logging test");
|
if
USE_LOGGING
is not defined
TRACE
macro expands to empty statement, and nothing happens.
If you want particular logging event to always happen simply call function instead of macro.
However it would be better to make a logging class probably, to be able to better reuse code.