Log function ala log4j

Log function ala log4j

Postby biobier » Thu Sep 03, 2020 7:53 pm

Hej coders,
I would like to have a logging function which does only print if the external setting is equal or higher. Similar as with log4j library at java and others.

I came that far:
Code: [Select all]
 Please Login or Register to see this content
<

But my problem with this is that any non string types (doubles, integer, etc) "need" to be converted to string when passing, not as with the default Print(). Any ideas how to overcome that? It would be good to see the definition of Print() to adopt that but I could not find that. Such function would help a lot debugging/testing/tracing why something was done.

Cheers!
biobier
 
Posts: 20
Joined: Mon Mar 18, 2019 7:24 am

Log function ala log4j

Postby renexxxx » Thu Sep 03, 2020 10:21 pm

biobier ยป Fri Sep 04, 2020 5:53 am wrote:But my problem with this is that any non string types (doubles, integer, etc) "need" to be converted to string when passing, not as with the default Print(). Any ideas how to overcome that?

You can use function overloading to create different "versions" of the log function like so:

void log(LogLevel lvl, string text) { ... }
void log(LogLevel lvl, double num) { ... }
void log(LogLevel lvl, int num) { ... }

However, with variable number of arguments of different types this is not practical as you would end up with zillions of log function definitions. Therefore it is far better to do the conversion of data into a string by the caller of the log function. So, you would only have one simple version of the log function like so:
Code: [Select all]
 Please Login or Register to see this content
<


And you would call the log function like so:

Code: [Select all]
 Please Login or Register to see this content
<


Hope this helps.
User avatar
renexxxx
 
Posts: 848
Joined: Sat Dec 31, 2011 3:48 am

Log function ala log4j

Postby biobier » Fri Sep 04, 2020 11:09 am

Thanks Rene, did not come across StringFormat yet for this! So far I used IntegerToString()/ DoubleToStr().
Both not ideal. In a modern language we could just pass object and try to parse it. Did not found a way to do that in MQL4 :?
biobier
 
Posts: 20
Joined: Mon Mar 18, 2019 7:24 am


Return to Coders Hangout

Who is online

Users browsing this forum: Google [Bot] and 1 guest