Code Example #6 – UT3 Logging Essentials

Below is the library that my team and I use for our UT3 Development. Please feel free to enjoy as well.

All you have to do is include the uci file

`include ({PackageName}/Classes/LibraryMessage.uci)

LibraryMessage.uci

// Macros for easy calling of Library_Message functions.

`define GetObjectMethodPrefix() self $ "." $ GetFuncName() $ ": "

`define ClientMessage(message) class'LibraryMessage'.static.ClientMessage(self, `message)
`define DebugMessage(message) class'LibraryMessage'.static.DebugMessage(self, `GetObjectMethodPrefix() $ `message)
`define LogMessage(message) class'LibraryMessage'.static.LogMessage(`GetObjectMethodPrefix() $ `message)

LibraryMessage.uc

//=============================================================================
// LibraryMessage
//
// This class is a number of static methods that allow you to send a number of
// useful and static messages to clients, logs and make warnings.
//
// A few of these have been wrapped into macros in order to simplify Calling
// them.
//
// Use this directive to get the macros
// `include ({PackageName}/Classes/LibraryMessage.uci)
//
// Use `DebugMessage if you are trying to print debug information. Its output
// will be omitted if the class has not been compiled in debug mode (-debug)
// Use `LogMessage to send a message to the hud.
// Use `ClientMessage to send a message to all clients.
//
// Contact : [email protected]
// Website : www.gneu.org
// License : Content is available under Creative Commons Attribution-ShareAlike
//			 3.0 License.
//=============================================================================
class LibraryMessage extends Object;

// Edit this to your preference to make messages stand out among the clutter of
// the log.
const prefix = ' ';

// If debugging is enabled, write the message to the log and try to write it to
// the screen.
public static function DebugMessage(Object sender, string message)
{
`if(`isdefined(DEBUG))
        LogMessage(message);
        GenericClientMessage(sender, message);
`endif
}

// Try to somehow write the message to the screen.
private static function GenericClientMessage(Object sender, string message)
{
    if(Actor(sender) != none)
        ClientMessage(Actor(sender), message);
    else if(ActorComponent(sender) != none)
        ClientMessage(ActorComponent(sender).Owner, message);
    else
        WorldInfoClientMessage(message);
}

// Write the message to the log.
public static function LogMessage(string message)
{
    `log(message,true,prefix);
}

// Write the message to the log.
public static function WarnMessage(string message)
{
    `warn(message,, prefix);
}

// Write the message to the screen.
public static function ClientMessage(Actor sender, string message)
{
    local PlayerController PC;

    foreach sender.LocalPlayerControllers(class'PlayerController', PC)
    {
        PC.ClientMessage(message);
    }
}

// Try to grab the WorldInfo and use that to write the message to the screen.
public static function WorldInfoClientMessage(string message)
{
    local WorldInfo wi;

    wi = GetWorldInfo();

    if(wi != none)
        ClientMessage(wi, message);
    else
    {
        LogMessage(message);
        LogMessage("Could not send the previous message to clients because WorldInfo0 was not found.");
    }
}

// Try to grab the WorldInfo.
private static function WorldInfo GetWorldInfo()
{
    return WorldInfo(FindObject("WorldInfo_0", class'WorldInfo'));
}