Our logging module has a buffer that often gets full and we are searching for a solution to shorten our logs, without making any compormise in performance.
The code is written in C++ and currently we are using __FUNCTION__ to extract the function name for a trace, but on windows environments it also appends the whole namespace, which often represents more than half of the trace.
One of the proposed solutions was to parse the string returned by “__FUNCTION__” and extract the function name only, but we would rather not make this time performance compromise.
Another proposed solution was to #define a short function name at the beginning of every method and #undefine it at the end.
Is there any other approach for shortening (the file name in) logs on systems that have few resources?
5
You could use __LINE__
instead, and although the function names will no longer be present in your logs, you can easily resolve them when analysing the logs.
An alternative is a static object in each function, declared at the start, that wraps trhe logger functions so you use it to write the log lines. The constructor would store a code (eg the line number of the declaration) that would be logged in place of the function name subsequently.
2
Maintain a map in which you accumulate function names and associate them with unique integer ids.
You should be able to optimize the performance of the map by taking into consideration the fact that the function name produced by the compiler for a certain function will always have the same address. (There will be only one string constant per function name in the data segment.) Exactly how you do that might be the subject of another stackoverflow question, but it is not even necessary.
So, the first time you encounter a function name, issue a unique id for it (by incrementing a counter) and generate a log entry documenting the fact that this id will from that moment on refer to the entire function name (namespace and all).
Then, from that moment on, whenever you encounter that function name, emit only the integer id instead.
You might also want to write a utility to run on your desktop computer which reads such a log and deciphers it by replacing these ids with the actual function names.