1

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 simply log __FILE__ & __LINE__ without the function name. BTW, where is the logging going (to disk, to network)? Commented Feb 18, 2015 at 9:03
  • The logging is done to disk. Tried that but the __FILE__ is also too long, since it prints the relative path of the file, folder names included. But thank you for the suggestion.
    – Ioanna
    Commented Feb 18, 2015 at 10:40
  • Show some code which does the actual logging. It is not clear what is your real issue, so please edit your question to improve it. And if possible show the logging function. Commented Feb 18, 2015 at 11:05
  • "#define a short function name at the beginning of every method" - why not an integer, which packs more info into less space (and before you say that it might lead to errors as methods are added, copied & deleted, so might a short function name)
    – Mawg
    Commented Feb 18, 2015 at 15:24
  • Indeed... I suppose the only advantage of names is the redability of the traces.
    – Ioanna
    Commented Feb 19, 2015 at 6:37

2 Answers 2

1

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
  • The first suggestion is missing the name of at least the file... We tried the combination __FILE__ and __LINE__, but the relative path of the file is still too long. The second alternative sounds like the #define-s that we used at every beginning of a method, and the corresponding undefines at the end. Or am I misunderstanding?
    – Ioanna
    Commented Feb 18, 2015 at 10:51
  • no, its just using RAII to ensure the equivalent of your #defines are set and unset correctly. People will make cut and paste errors in putting those #defines in, and then your logs will report the wrong function. Always happens.
    – gbjbaanb
    Commented Feb 18, 2015 at 11:05
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.