Logging And Exceptions
Welcome To CIDCorp
Home of the CIDLib Class Libraries


CIDLib Logging Concepts

CIDLib is unusual in its approach to logging, but I think you will like it once you get used to it. Logging of errors and throwing of exceptions are tightly linked, and basically are all done in a single operation. Logging of exceptions is a very stylized thing in CIDLib and it bends over backwards to make it very easy so that you will want to make use of it.
This discussion assumes that you understand the ramifications of exceptions overall. I discuss exceptions in general elsewhere.
To begin with, CIDLib is probably unusual to begin with in its strong emphasis on logging. Since I've tended to work on highly distributed, non-shrink wrapped code a lot in my time, I've come to appreciate what it takes to have a system provide an audit trail for after the fact diagnosis. CIDLib lots all unhandled system and C++ exceptions and allows you to log any system milestomes that you feel are appropriate. It supports severity driven filtering of logged messages, and it fully integrates the logging and exception mechanisms to as to avoid doing twice the work.

In order to serve as many different application types as possible, CIDLib supports a layered logging system in which higher level DLLs or the user EXE can take over the routing of logged messages for that process. This allows the logging system to work the same for a simple TTY output program or for a distributed system that logs to a central server.

Logging

As mentioned above, CIDLib believes strongly in logging messages, both during debugging and (to a slightly lesser degree) in the field. Any large, 7x24 application that must be administered remotely, etc... really needs to have an integrated logging mechanism so that application status can be recorded over time and errors debugged after the fact. Therefore logging is supported in a very powerful way, that encourages resource based text (for language portability) and expressive error messages with lots of information. CIDLib tries to kick in as much information as it can for logged messages, so that you don't have to type in all that stuff every time.

Note that CIDLib's logging mechanism is much more elaborate than the usual ASSERT() type macro based error checking. It does require a little more work to use, but the resulting payoff of very finely grained information concerning the application status makes it more than worth it. The payback is way out of proportion to the extra effort.

Logging is done via the facility object of the facilty doing the logging. This allows the facility object to kick in a good bit of information automatically. It is able to provide the facility name, knows what radix your facility object's error codes should be in, knows what version of itself it is, knows the name of the program, gets the name of the calling thread, etc... It will add this stuff into the mix for you so you don't have to handle it.

The facility object handles taking the provided information, adding what it needs to, extracting resource strings, doing token replacement, etc... and getting the information together needed to create a TCIDError object. This object is what is actually logged. It contains a number of fields, see the class reference for details, among them are a severity level and an error category.

Logging of messages and errors are done via the LogMsg() and LogErr() methods of the TCIDFac class, the base class of all facility classes.

Error Severity and Category

Each logged message has an associated severity and category. These values indicate the relative severity of the object and the broad type of condition that it pertains to. The category is more for human consumption and will allow smart error file browsers to filter for certain types of errors. The severity level though plays a much more important part.

The CIDLib.Dll facility object, facCIDLib, contains a process wide threshold for error severities. Only logged messages above or equal to that threshold are actually logged. This provides a means for selectively limiting or increasing the sensitivity of the information logged. By default, the level is set to the eSev_APIFailed severity.

The error severity levels that are defined by CIDLib are:
    enum    eSeverities
    {
        eSev_Information
        , eSev_Warning
        , eSev_APIFailed
        , eSev_ProcessFatal
        , eSev_SystemFatal

        , eSev_Status
    };
Informational messages are just what you would think. They just convey some meaning that is only of interest when desparately searching for the answer to a problem :-) In other words stuff that otherwise you would take for granted, until you have a problem. Some informational messages are often conditionally compiled away for production code. They are only logged if the current threshold is at the information level.

Warning messages are about things that normally are not errors but might represent problem spots. They are only logged if the threshold is at or below the warning level.

API Failed messages indicate that a called API failed to be able to perform its function. These messages will be logged if the logging threshold is at or below the API Failed level, then the TCIDError object will be thrown as a C++ exception.

Process Fatal messages indicate that something has gone so wrong that the program must be aborted or damage could occur. This should be used VERY sparingly, but has a legitimate use. It will be logged if the threshold is at or below the Process Fatal level. The the program will be aborted.

System Fatal messages basically don't get logged anywhere right now. But, if something indicates total system failure it could be used. It will be logged if the threshold is at or below the System Failure level, and will cause the system to be rebooted (ungracefully.)

Status messages are special. They are never thrown, only logged. And they cannot be turned off because CIDLib will not allow the threshold to be set that high. These messages are for big process milestones that you might always want to know after the fact if something goes wrong. You should not do many of them because they are always logged, and definitely not in a loop that could possibly fill up the disk by spitting out messages.

Throwing

Most of the time, you will always just log messages and any throwing of exceptions will be handled within that context, as described above. However, sometimes you may want to just throw an exception without it getting logged because its a special case of some sort. There is no severity level that does that at this time, so its done via the ThrowErr() and Throw() message methods of the TCIDFac class (the base class of all facility classes.)
* For instance, doing a 'find first' operation on a directory to find all the matching files. Failure to find any files does need to cause an exception, to indicate the failure. However, it is an 'expected exception' that could happen just because a program is speculatively searching for anything (not because it failed to find something specific.) In such cases you need to throw but not log, because you could fill the log up. The catcher of the exception should log any desired errors if indeed this failure to find a match is an error. Only it has that context.
These methods just build a TCIDError object, as described above in the logging section, and throw it. Otherwise everything happens the same.

CIDCorp
Dean Roddey
Internet: droddey@ng.netgate.net
CIS: 72170,1614