DDE, LIB: setting defines in DDE to setup the debugging of the lib

Helle,
I use to implement a globlal switch in my Application to setup the debugging in my lib as its done in the MT GNSS lib. Here its possible to define GNSS_DEBUG 1 in the DDE of the application and the GNSS lib issues the debug infos to console or watchpanel.

To implement this functionality to my project, I used to do something like this:

main.DDE of application

#define POSITIONING_DEBUG 1 // set debugging of positioning.inc in my lib to level 1

positioning.inc inside of myLib

#if POSITIONING_DEBUG >= 1
#log("[POS] Hello Debugging!");
#endif

But the problem is, that the POSITIONING_DEBUG define is not known in myLib.

Does anyone have any ideas on how to best implement something like this?

Best regards,
Marcel

Hello @mklueh,

if you want to support multiple debug levels, you can proceed with the approach that you described.
But you have to define POSITIONING_DEBUG in case the application does not define it.

This can be done via the following code block in your library, before POSITIONING_DEBUG is checked. For example, at the beginning of your library code.

#ifndef POSITIONING_DEBUG
	#define POSITIONING_DEBUG (0)
#endif

A second approach would be to only check if POSITIONING_DEBUG is defined in general, before the #log prints.
This approach is limited to only one debug level, but makes it possible to omit the additional code block, described in the first approach.

In main.dde the application can use the define as before:

#define POSITIONING_DEBUG 1

In the library, only check if POSITIONING_DEBUG was defined by the application.

#ifdef POSITIONING_DEBUG
#log("[POS] Hello Debugging!");
#endif

Kind regards,
Stefan

Hello Stefan,
Thank you for your help.
I have now implemented the dubugging with several levels according to your example as follows:

//this is in main.dde of my lib
#ifndef POSITIONING_DEBUG
#define POSITIONING_DEBUG (0)
#endif

//this is in positioning.inc of my lib
public Timer1s_GpsTimer() {

	if (POSITIONING_DEBUG >= 1) {
		#watch("[POS] LAT=%d", aGPS.Latitude);
		#watch("[POS] LONG=%d", aGPS.Longitude);
	} 
}

//this is in main.dde of my application
#define POSITIONING_DEBUG 1 // set debugging of positioning.inc in my lib to level 1

Now I have the following warning and a questions:

image

Should I ignore the warning or is there a solution?
What is the difference between the following spellings:

#define MY_DEFINE
#define MY_DEFINE 0
#define MY_DEFINE (0)

Kind regards,
Marcel

Hello @mklueh,

to get the advantage of the pre-processor conditions, you can use the “#if” keyword, this should also resolve the compiler warning.
Otherwise the “if” condition you described would be checked every time the Timer1s_GpsTimer() function is called.
With the help of the pre-processor, the debug statements will only be compiled if debugging is enabled. Therefore, this condition does not have to be checked during runtime and the #watch output will only be present if debugging is enabled.

The explained pre-processor condition would look like this:

#if POSITIONING_DEBUG >= 1
#watch([POS] LAT=%d, aGPS.Latitude);
#watch([POS] LONG=%d, aGPS.Longitude);
#endif

Regarding your second question,

  1. #define MY_DEFINE will not assign a value to the given define, therefore you can only check if the given define is present (e.g. with #ifdef)
  2. #define MY_DEFINE 0 will assign a value to the given define, therefore you can additionally check the value of the define (e.g. with #if)
  3. #define MY_DEFINE (0) is similar to variant 2, because a value is assigned to the define. Using brackets can be useful if you want to define more complex expressions.
    To prevent errors, this variant with brackets should not be used in the main.dde file.

Kind regards,
Stefan