C ++ Pass Boost :: log severity as function argument
I only want to have one function to write logs that will parse the ellipsis and send the result to Boost :: log based on severity. Different macros will be defined in the header file that will select the correct severity level. There is a code:
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#define DEBUG(msg, ...) Logger::write_log(debug, msg, ##__VA_ARGS__);
#define INFO(msg, ...) Logger::write_log(info, msg, ##__VA_ARGS__);
#define WARNING(msg, ...) Logger::write_log(warning, msg, ##__VA_ARGS__);
#define ERROR(msg, ...) Logger::write_log(error, msg, ##__VA_ARGS__);
namespace logging = boost::log;
void write_log(auto level, const char *message, ...)
{
char buffer[512];
va_list args;
// Parse ellipsis and add arguments to message
va_start (args, message);
vsnprintf (buffer, sizeof(buffer), message, args);
va_end (args);
BOOST_LOG_TRIVIAL(level) << buffer;
}
int main(int argc, char** argv)
{
DEBUG("Test string %s", "additional string");
return 0;
}
But at compile time I get the following error:
error: 'level' is not a member of 'boost::log::v2s_mt_nt5::trivial'
BOOST_LOG_TRIVIAL(level) << buffer;
It looks like my level argument is of the wrong type. I also tried using logging::trivial::severity_level level
instead auto level
, but it didn't help. How can I fix this error?
UPDATED:
there is a working solution:
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#define DEBUG(msg, ...) Logger::write_log(debug, msg, ##__VA_ARGS__);
#define INFO(msg, ...) Logger::write_log(info, msg, ##__VA_ARGS__);
#define WARNING(msg, ...) Logger::write_log(warning, msg, ##__VA_ARGS__);
#define ERROR(msg, ...) Logger::write_log(error, msg, ##__VA_ARGS__);
namespace logging = boost::log;
enum severity_level
{
debug,
info,
warning,
error,
exception
};
src::severity_logger<severity_level> slg;
void write_log(severity_level level, const char *message, ...)
{
char buffer[512];
va_list args;
// Parse ellipsis and add arguments to message
va_start (args, message);
vsnprintf (buffer, sizeof(buffer), message, args);
va_end (args);
BOOST_LOG_SEV(slg, level) << buffer;
}
int main(int argc, char** argv)
{
DEBUG("Test string %s", "additional string");
return 0;
}
source to share
Follow the increment log example and define:
// severity levels
enum severity_level
{
trace,
debug,
info,
warning,
error,
fatal
};
And you need to force your function to take the correct type:
void write_log(severity_level level, const char *message, ...){ ... }
Another option:
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#define DEBUG(msg, ...) Logger::write_log(logging::trivial::debug, msg, ##__VA_ARGS__);
#define INFO(msg, ...) Logger::write_log(logging::trivial::info, msg, ##__VA_ARGS__);
#define WARNING(msg, ...) Logger::write_log(logging::trivial::warning, msg, ##__VA_ARGS__);
#define ERROR(msg, ...) Logger::write_log(logging::trivial::error, msg, ##__VA_ARGS__);
namespace logging = boost::log;
#define LOG_TRIVIAL(lvl)\
BOOST_LOG_STREAM_WITH_PARAMS(::boost::log::trivial::logger::get(),\
(::boost::log::keywords::severity = lvl))
void write_log(logging::trivial::severity_level level, const char *message, ...)
{
char buffer[512];
va_list args;
// Parse ellipsis and add arguments to message
va_start(args, message);
vsnprintf(buffer, sizeof(buffer), message, args);
va_end(args);
LOG_TRIVIAL(level) << buffer;
}
int main(int argc, char** argv)
{
DEBUG("Test string %s", "additional string");
return 0;
}
source to share
My advice: create your seriousness. It's just a rename! Follow the source code at this level (using your IDE) to see that it is a simple enumeration. Copy it to your implementation and change as needed. This is what it looks like (after changing its name):
enum my_severity_level
{
trace,
debug,
info,
warning,
error,
fatal
};
Take this to your code and use it as needed.
This function write_log
should be like this:
void write_log(my_severity_level level, const char *message, ...) { ... }
source to share