import Codeware.*
import MyMod.Logger.*
class CrazyService extends ScriptableService {
private cb func OnLoad() {
MyLog("Psycho stuff!");
MyLogWarn("Psycho stuff!");
MyLogError("Psycho stuff!");
}
}
// It will log something like:
// ... [MyMod] Psycho stuff!
// ... [MyMod] Psycho stuff! // Colored in Game Log
// ... [MyMod] Psycho stuff! // Colored in Game Log
We can even improve on this with Codeware. It provides the function GetStackTrace so we can deduce the class and function where our logging function is executed:
module MyMod.Logger
public static func MyLog(value: script_ref<String>) -> Void {
let entries = GetStackTrace(1, true);
let entry = entries[0];
let trace = "";
if IsDefined(entry.object) {
trace = s"[\(entry.class)][\(entry.function)]";
} else {
trace = s"[\(entry.function)]";
}
FTLog(s"[MyMod] \(trace) \(value)");
}
// Same with MyLogWarn / MyLogError and FTLogWarning / FTLogError.
You can use it the same way, this time it will log something like:
This is convenient as you don't have to worry when you copy/paste this line in another function. The class and function name will be deduced for you. It also works when using a global function.
You can improve this function to list more entries of the stack trace. This is up to you to implement it if you want. An other alternative worth checking is to use redscript-dap, a debugger which integrates with VS Code.