Writing to the Event Log

From The Foundry MODO SDK wiki
Revision as of 01:35, 28 February 2012 by Shf (Talk | contribs) (CLxLogMessage)

Jump to: navigation, search

Status of the running plug-in can be written to the event log, either for the user or for the developer to examine debug state.


This utility class is the easiest way to write to the log, although it was designed for file I/O and still has some of that legacy. You need to declare your own sub-class which implements the Format() and Version() methods for your plug-in.

class CMyLogMessage : public CLxLogMessage
        const char * GetFormat ()
                return "My Format";

        const char * GetVersion ()
                return "My Version";

Declare your class as a persistent object somewhere, and you can then output info and errors to the event log.

        CMyLogMessage        my_log;

        my_log.Error ("Help! Something scary!");
        my_log.Info ("Nevermind -- just my own shadow");

These messages will be tagged with your format and version. If you don't care for that you can compose messages more directly. It's also possible to append sub-messages in the log, which will be appear as collapsible sub-items in the event log viewport.

        my_log.Message ("CMyPlug", "MyMethod", "Multiple values already set:");
        my_log.SubMessage (0, 0, "Value 1");
        my_log.SubMessage (0, 0, "Value 2");

Custom Logs

By default the CLxLogMessage implementation writes to the "io-status" event log. That can be overwritten in the constructor:

class CMyLogMessage : public CLxLogMessage
        CMyLogMessage() : CLxLogMessage ("myLog") {}

The name can be any existing log, but if the log doesn't otherwise exist it has to be declared by the server. This can be done by adding the name(s) of logs that the server wants to use to the server's tags.

   { LXsSRV_LOGSUBSYSTEM,  "myLog" },

Direct Log System Access

If the log message helper class is unsuitable or you want more control over the formatting of your events, you can access the log more directly. You initialize a log wrapper by using the allocate-by-name method.

        CLxUser_Log       my_log;

        my_log.setByName ("myLog");

You can then allocate log entries from the service and post them to the log.

        CLxUser_LogService log_svc;
        CLxUser_LogEntry   entry;

        log_svc.NewEntry (LXe_INFO, "My Info Message", entry);
        my_log.AddEntry (entry);

Adding sub-entries is done by making the same AddEntry() call on the parent entry.

        CLxUser_LogEntry   sub;

        log_svc.NewEntry (LXe_INFO, "My Sub-message", sub);
        entry.AddEntry (sub);