ScarletLib / Scarlet

C# cross-platform robotics library with hardware support for Raspberry Pi and BeagleBone Black.
https://github.com/huskyroboticsteam/Scarlet/wiki
GNU Lesser General Public License v3.0
7 stars 3 forks source link

Using delegate for logging messages? #5

Closed ritzow closed 6 years ago

ritzow commented 6 years ago

This is a minor thing, but it seems like it would be more efficient to use a delegate for getting the message to log to the console (the Log.Output message parameter). Using a delegate would mean that if logging is turned off, the overhead of calling Log.Output would be close to zero, whereas currently, string concatenation will occur even if logging is disabled. For an example of this, see this example from the Java standard library. The Supplier's method will only be called if the message is needed. This could also have an even greater performance benefit once sending log messages over the network is implemented.

ritzow commented 6 years ago

Maybe I should just make a branch and create a pull request. The code was only a few lines:


public delegate string MessageProvider();
public static void Output(Severity Sev, Source Src, MessageProvider message) {
      //repeat this code because the delegate shouldn't be called unless its log level is enabled
      if (OutputLevels[(int)Src] <= Sev) { ForceOutput(Sev, Src, message()); }
 }`
CaiB commented 6 years ago

Go ahead, this sounds like a good idea.