0

My project uses a logging mechanism, which is basically a call to a macro in each of my components.

Behind the macro is a function call, which is used to write the log using a slow line like UART.

Example:

LOG("An error has occurred. Please check XYZ. Code: %d", errorcode) 

Instead of writing the whole text to UART, I want to replace every occurrence of LOG like this:

LOG("12345 %d", errorcode) 

Each call to LOG must be replaced by a short version, where the text is replaced by a unique number. The dynamic part may not be replaced. The mapping of the unique number to the original text must be placed into a file. Something like this:

12345:"An error has occurred. Please check XYZ. Code:"

At the other end of the UART a log reader has to read this mapping file to make it human readable.

The advantage is that this is perfect for low bandwidth.

My question : How can I do this while compiling my code? I am using gcc and working on a Linux environment. I assume that the preprocessor has to handle this. Or a kind of pre-script? Or is there a better way of doing this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ferhat
  • 471
  • 3
  • 9
  • 21
  • Not clear for me. Have you got the function to write characters to UART or it is what are you looking for? – LPs Jan 19 '16 at 07:05
  • No, the UART part is working. I just need to know how to do such a replacement as described above to reduce the data to be transferred over UART. – Ferhat Jan 19 '16 at 07:28
  • 1
    It doesn't seem to be a programming problem, but a text editor text replacement problem? Just go through the code and change all the messages. Also, if you are concerned about bandwidth why do you send the data as some secret nonsense numbers in ASCII? If humans would have problems to understand it anyhow, send it as raw binary. Two integers = 4 or 8 bytes, instead of 10+ bytes that the string needs. – Lundin Jan 19 '16 at 07:43
  • I just want to do the replacement during compiling. The original calls must be kept. I can't just use a text editor for replacing. I will think about sending it in raw binary mode. This would be more efficient. – Ferhat Jan 19 '16 at 16:53

1 Answers1

1

You are most assuredly going to need a pre-processing script for this. The power of the C preprocessor is quite limited -- pretty much it is limited to simple text substitution. If all your LOG calls are on 1 line each, and none of their messages contain parens, then you can probably do it with a fairly simple text processing tool, perhaps awk. If not, I would use a programming language, perhaps perl, to construct it.

I am assuming there are a great number of these LOG calls in your program -- otherwise simply editing the program might make more sense.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
  • I can't guarantee that all of them are just single lined calls. So that would mean that I can't use awk? Just to be honest I didn't use awk before. So you prefer writing a script in perl. Is it easy to integrate it into a makefile so it can run first before the compiler can do his work? Yes it's a huge amount of log calls. – Ferhat Jan 19 '16 at 16:57
  • You might be able to do it in awk, it's just that the closer your requirements come to actually parsing the code (like a compiler) the more work it becomes in simpler tools. (Although, I wouldn't bet that some person with an excess of free-time hasn't written a compiler in awk). Yes, a Makefile can execute a series of commands, so incorporating an extra step in a compile is easy. – John Hascall Jan 19 '16 at 20:08
  • Let's say I will use a perl script for this. Do you prefer to use a kind of text search and replace (regex?) ? I need to check if there is a C parser which could help me. I really don't want to write one on my own :) – Ferhat Jan 19 '16 at 20:40
  • Unless your code is crazy complicated. I would think something far simpler than a full parser would be sufficient. Roughly speaking I would read the whole file in one big gulp (see http://stackoverflow.com/a/953885/2040863 ) and then search for "LOG" and once I found that I would call a function to find the two parens and make sense of everything in between. Repeat until at the end. – John Hascall Jan 19 '16 at 20:46
  • Thanks I will try that! – Ferhat Jan 20 '16 at 06:50