I am currently working on a threaded Logging library as the first part of a bigger project for graphics drawing (for personal learning and development of skills).
currently i am using a singleton with a thread running on the side and taking Log messages and data into queues (so that they are not blocking events) to be processed later. I wrote a small wrapper around std::map<> as LogData that can be displayed by the logger in a stream or file in the following way.
[0.000155][DEBUG]: FILE LOGGER ADDED { ID="1" LVL="TRACE" }
The constructors allows to pass string,char*,floats,int,short long etc and will convert it to a string to be displayed later in thoses brackets.
Currently constructing this LogData is a bit bloaty. Example that produced the log above:
GG::LogData id_dat;
id_dat.push("ID", id);
id_dat.push("LVL",GG::loglevel_toString(lvl));
GG_DEBUG("FILE LOGGER ADDED", id_dat);
Since my Class is a Singleton i use macro's to allow for ease of use they are all the same as :
#define GG_TRACE(MESS, ...) GG::Logging::get()->push_to_queue(GG::LOG_LEVEL::TRACE, MESS, ##__VA_ARGS__);
This works fine for most use. But i wanted to make it possible to use on one line, and make it less bloaty. the effect i wanted to achieve was something like this :
//Desired Usage
GG_TRACE("VARIADIC TEST", {"X","1"}, {"Y","2"}, {"Z","3"});
this is expanded here:
void Logging::push_to_queue(GG::LOG_LEVEL level, std::string mess, std::pair<const char*, std::string> log_data ...)
I would use brace initialized list to produce the log data, then i would loop over the variadic argument and construct the LogData in a function instead of having to do it manually every time.
i had it working directly with a function like so.
void test(std::pair<char*,int> p) {
GG::LogData dat;
dat.push("key", p.first);
dat.push("value", p.second);
GG_TRACE("PAIR: ", dat);
}
// In main...
test({ "test",1 });
and that worked fine. But when i try to use that same pattern and have the macro's forward it to the push_to_queue fonction i get the following error with GCC.
Anybody have ever used brace enclosed initializer list in this way or know how to fix this bug ? i am fairly new to this kind of pattern. Any other suggestion or pointers to improve on this is appreciated. (sorry for the long post)