I have a couple of rather large gcc-based C-projects (more than 400 files, partially shared by the projects) where each file has something like a file id for logging output. This boils down to something like this:
// At start of each c-file#include "log.h"#define MOD_ID 12 // "Unique" number for each file...TRACE("hello world");
With log.h:
// log.h#define TRACE(msg) printf("module=%d msg=%s", MOD_ID, msg) // Fixed format with number and text
This concept is flawed in two respects:
- The MOD_IDs are hard to maintain and get easily duplicated
- The messages are sent across network and the receiver does not know, which id belongs to which module, which makes Debugging hard, as each time, the MOD_ID needs to be searched in the correct source code.
I would like to change this so that the uniqueness is guaranteed by compiler/linker and at the same time, I would like to have a "dictionary" or "lookup-table" for each program. I could use __FILE__
for creating one part of the dictionary, but how do I create a unique number for each file (starting at 0 and no (large) holes allowed - that requirement comes from a different part of the code)? Is there an existing solution or do I have to invent the wheel myself?