stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Log function ala log4j
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5920
Page 1 of 1
Author:  biobier [ Thu Sep 03, 2020 7:53 pm ]
Post subject:  Log function ala log4j

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

extern LogLevel         Logging                             = info;

enum LogLevel
{
   trace=0,
   debug=1,
   info=2,
   warn=3,
   error=4   
};

void log(LogLevel lvl, string st1, 
   string s2="", 
   string s3="", 
   string s4="", 
   string s5="", 
   string s6="", 
   string s7="", 
   string s8="", 
   string s9="", 
   string s10="", 
   string s11="", 
   string s12="", 
   string s13="", 
   string s14="", 
   string s15="")
{
	/*
		trace=0,
		debug=1,
		info=2,
		warn=3,
		error=4
   */
   string out= StringTrimRight(StringConcatenate(	  
      EnumToString(lvl), ":"
      " ", st1, 
      " ", s2, 
      " ", s3, 
      " ", s4, 
      " ", s5, 
      " ", s6, 
      " ", s7, 
      " ", s8, 
      " ", s9, 
      " ", s10, 
      " ", s11, 
      " ", s12, 
      " ", s13, 
      " ", s14, 
      " ", s15
	));
	if(lvl>=Logging)
		Print(out);	
}//void log
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!
Author:  renexxxx [ Thu Sep 03, 2020 10:21 pm ]
Post subject:  Log function ala log4j

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

enum LogLevel
{
   trace=0,
   debug=1,
   info=2,
   warn=3,
   error=4  
};

extern LogLevel         Logging                             = info;
 
void log(LogLevel lvl, string text) 
{
   string out = StringConcatenate(EnumToString(lvl), ": ",text);
   
   if(lvl>=Logging) Print(out);
}//void log
And you would call the log function like so:

Code: Select all

   int i = 5;
   double f = 3.1415;
   bool flag = true;

   log(debug, "Some text");
   log(warn, StringFormat("integer=%d, double=%5.2f, bool=%s", i, f, (string)flag ));
Hope this helps.
Author:  biobier [ Fri Sep 04, 2020 11:09 am ]
Post subject:  Log function ala log4j

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 :?
All times are UTC Page 1 of 1