• Create Account

### #Actualkrippy2k8

Posted 19 June 2012 - 02:52 AM

Specifically, client code wants to log a message, not to "Get" a Log instance, worry about Log references, and still have to call Log methods to do something useful. Free functions are clearly the best fitting API for logging; static variables can also be used with functions and classes can be used behind the scenes, so nothing is lost from a technological standpoint.

Unless you want to log something other than simple strings. What if you want to log other values as well? You could use a printf-style log function, but then you lose type safety and must spend time generating an appropriate format string, and hope that the type of a variable never changes... with the possibility that your logging will itself cause a crash or bug! Or you would need to generate the string some other way before calling your log function.

Or you could do something like:

Logger::warning(__FILE__,__LINE__) << "foo is out of expected range: " << foo << endlog;


or use a macro to eliminate some of the dirty work, and just do something like this:
WARNING << "foo is out of expected range: " << foo << endlog;

or even (my preference):
cs_warn( "foo is out of expected range: ", foo );

which makes adding logging much more pleasant, while maintaining type safety.

You could also generate a whole crapload of templated free functions to accomplish something similar, but that makes things a little more difficult when you need to mix integral types with user-defined types, and is generally quite a bit less flexible overall.

### #2krippy2k8

Posted 19 June 2012 - 02:46 AM

Specifically, client code wants to log a message, not to "Get" a Log instance, worry about Log references, and still have to call Log methods to do something useful. Free functions are clearly the best fitting API for logging; static variables can also be used with functions and classes can be used behind the scenes, so nothing is lost from a technological standpoint.

Unless you want to log something other than simple strings. What if you want to log other values as well? You could use a printf-style log function, but then you lose type safety and must spend time generating an appropriate format string, and hope that the type of a variable never changes... with the possibility that your logging will itself cause a crash or bug! Or you would need to generate the string some other way before calling your log function.

Or you could do something like:

Logger::warning(__FILE__,__LINE__) << "foo is out of expected range: " << foo << endlog;


or use a macro to eliminate some of the dirty work, and just do something like this:
WARNING << "foo is out of expected range: " << foo << endlog;

or even:
cs_warn( "foo is out of expected range: ", foo );

which makes adding logging much more pleasant, while maintaining type safety.

You could also generate a whole crapload of templated free functions to accomplish something similar, but that makes things a little more difficult when you need to mix integral types with user-defined types, and is generally quite a bit less flexible overall.

### #1krippy2k8

Posted 19 June 2012 - 02:38 AM

Specifically, client code wants to log a message, not to "Get" a Log instance, worry about Log references, and still have to call Log methods to do something useful. Free functions are clearly the best fitting API for logging; static variables can also be used with functions and classes can be used behind the scenes, so nothing is lost from a technological standpoint.

Unless you want to log something other than simple strings. What if you want to log other values as well? You could use a printf-style log function, but then you lose type safety and must spend time generating an appropriate format string, and hope that the type of a variable never changes... with the possibility that your logging will itself cause a crash or bug! Or you would need to generate the string some other way before calling your log function.

Or you could do something like:

Logger::warning(__FILE__,__LINE__) << "foo is out of acceptable range: " << foo << endlog;


or use a macro to eliminate some of the dirty work, and just do something like this:
WARNING << "foo is out of acceptable range: " << foo << endlog;

or even:
cs_warn( "foo is out of acceptable range: ", foo );

which makes adding logging much more pleasant, while maintaining type safety.

You could also generate a whole crapload of templated free functions to accomplish something similar, but that makes things a little more difficult when you need to mix integral types with user-defined types, and is generally quite a bit less flexible overall.

PARTNERS