avnet-iotconnect / iotc-generic-c-sdk

Generic IoTConnect C SDK for Linux, Windows and MacOS
0 stars 3 forks source link

Add debugging routines #41

Closed nmatthews-witekio closed 9 months ago

nmatthews-witekio commented 10 months ago

Add debugging routines

Macro definition with (()) allows all debugging strings to be removed from the executable if required -- keeping executable size smaller -- but allowing debugging to be turned on if/when required.

nmatthews-witekio commented 10 months ago

Clarification:

Think of a debug routine like printf...

one_bracket_debug("This is the value %d", x); then the one_bracket_debug macro has 2 arguments -- "This is the value %d" and x This means you have to use __va_args__ to deal with the extra multiple optional arguments.

if you use the double-bracket technique then: two_bracket_debug(("This is the value %d", x)); then the two_bracket_debug macro has 1 argument -- ("This is the value %d", x) So it can work without needing __va_args__i.e. on an older compiler.

Also if you do:

define two_bracket_debug(x)

the compiler knows that the argument x is unused -- so it can throw away all of -- ("This is the value %d", x) so the string literal "This is the value %d" probably doesn't make it into the executable

You could use __va_args__:

define one_bracket_debug(x...)

but there's probably a bit less chance for the compiler to be able to discard the string "This is the value %d"

That's all. It looks a bit weird in the code -- but actually it sort of makes sense -- especially if you do

define two_bracket_debug(x) printf x

then the single argument -- ("This is the value %d", x) -- gets rewritten easily as printf ("This is the value %d", x)

nmatthews-witekio commented 10 months ago

If you use a #define that just discards its argument -- then the pre-processor stage will just remove the argument -- so the compilation stage won't even "see" the argument -- as it'll have already been removed.

nmatthews-witekio commented 9 months ago

This still needs reviewing @PatriciaFieldhouse.

nmatthews-witekio commented 9 months ago

Not sure what happened -- have adjusted an opened https://github.com/avnet-iotconnect/iotc-generic-c-sdk/pull/46