I'm running into a problem trying to express something that's beyond my basic knowledge of C++.
For my module I would like to add custom message formatting to maximize flexibility and the expressability of the logger. E.g., every argument will have custom message formatting (will not support modifiers like
"%-5s", and this is not meant to support any other specifiers than listed below [i.e.,
default C++ specifiers]):
Code:
%d current date
%f function logging message
%l log message
%m current map
%n script name (AMXX that is)
%s severity of log message
%t current time
The goal of this is so that a file name could be specified as
Code:
"%n_%d"
"plugin_2015-09-13.log"
Or in terms of the messages
Code:
"[%s] [%t] %n::%f - %l"
"[ERROR] [00:00:00] plugin::function - This is an error message"
And because it will be implemented like this, you could even have different log files for different priorities (e.g., one for ERROR, one for WARN, etc.) to show only the messages that you care about.
Code:
"%s_%n_%d"
"ERROR_plugin_2015-09-13.log"
"WARN_plugin_2015-09-13.log"
"INFO_plugin_2015-09-13.log"
"DEBUG_plugin_2015-09-13.log"
My issue is that I'm trying to figure out a way to express this. I wrote an algorithm which parses the string and replaces the specifier (
d,
f,
l, etc.) with
s, however I also need to find a way to customize the parameter stack on
fprintf (and
MF_PrintSrvConsole). I think this would require some kind of stack to be saved (e.g., represent each specifier with an int, and then dynamically pass them as arguments into the final call of
fprintf.
So, e.g., if a message was formatted as
"[%s] [%t] %n::%f - %l", when the constructor is called, this is compiled into
"[%s] [%s] %s::%s - %s", with a stack representing
{SEVERITY, TIME, PLUGIN_NAME, CALLING_FUNCTION, LOG_MESSAGE}, which would then format the
fprintf call like:
Code:
fprintf(pF, "[%s] [%s] %s::%s - %s", SEVERITY, TIME, PLUGIN_NAME, CALLING_FUNCTION, LOG_MESSAGE);
However, the arguments would need to be replaced at runtime with local variables, as I would like them all to be current, e.g., TIME would be the current time at calling in the format specified by the logger (e.g.,
"%H:%M:%S"). E.g. (removing other params aside from time):
Code:
char time[16];
strftime(time, sizeof(time), getTimeFormat(), curTime);
fprintf(pF, "[%s] [%s] %s::%s - %s", _, time, _, _, _);
There is also the additional requirement that the argument order not be fixed. For instance, you could have
time at the end, beginning, or not at all.
My current idea is to pre-format the message in another function of my creation before passing into
fprintf, so essentially calling
fprint. So my custom function would handle the dynamic parameters.
source
I'll be looking into this more, trying to find a solution, and post an update here if I do. If anyone has any ideas, I would appreciate hearing them.
Currently I have:
https://github.com/collinsmith/Logge...r.cpp#L77-L162
https://github.com/collinsmith/Logge...r.cpp#L190-197
Edit:
I think I'm closer now, however the module hangs because of a bad pointer. I'll try and look into it later.
__________________