Logging
Enabling Logging
In C++, SDK logging is controlled through free functions in RealtimeCore/Common/LogUtils.h, in the RealtimeCore::Common namespace.
LogEnable(level) and LogDisable(level) switch levels on and off, and IsLogEnabled(level) queries the current state.
Messages are only delivered once at least one log output is registered with AddLogOutput.
The C API has no logging functions, and a JavaScript client writes the SDK's log lines to the console without any setup; their tabs show what to do instead.
A typical startup sequence registers an output and enables the levels of interest:
C++
using namespace RealtimeCore::Common;
// ConsoleLogOutput is a user-defined LogOutput implementation, shown further down this page.
static ConsoleLogOutput consoleLog;
AddLogOutput(&consoleLog);
LogEnable(LogLevel::Info | LogLevel::Warning | LogLevel::Error);
C
/* Not available: LogUtils.h has no C entry points, so SDK logging cannot be
enabled or routed over the ABI. A C client logs what it observes itself.
Logging support will ship with a future release for the C API*/
if (e->type == RealtimeEventType_Error)
{
fprintf(stderr, "[Photon][ERROR] code %u: %.*s\n",
e->origin, (int)e->blobLength,
(e->blobOffset >= 0) ? (const char*)(blob + e->blobOffset) : "");
}
else if (e->type == RealtimeEventType_Warning)
{
fprintf(stderr, "[Photon][WARN] code %u: %.*s\n",
e->origin, (int)e->blobLength,
(e->blobOffset >= 0) ? (const char*)(blob + e->blobOffset) : "");
}
The JavaScript package has no functions to enable or disable log levels or to register log outputs.
Every SDK log line goes to the console with a [RealtimeCore] prefix, through the console method that matches its level, as listed under Log Levels.
Use the console's own level filter to hide the levels you do not need.
Log Levels
The SDK logs at five levels: Trace, Debug, Info, Warning and Error.
By default, every level except Trace is enabled.
In C++, LogLevel is a bitmask enum with the usual bitwise operators, so any combination of levels can be enabled or disabled at once.
There is no implicit hierarchy: enabling Warning does not enable Error; combine exactly the levels you want.
SetLogLevelsFromBitmask(mask) applies a complete level set in one call.
TryGetLogLevelFromString(name, outLevel) parses a level from text, which is handy for config files and command-line switches, and PrintLogLevels() logs the active set.
In JavaScript, the enabled levels stay at the default, and the level of a line decides which console method writes it.
| Level | Typical Content | JavaScript Console Method |
|---|---|---|
Trace |
Highest-volume detail, down to individual calls and wire activity. | console.debug() |
Debug |
Internal state transitions and diagnostic detail for development builds. | console.debug() |
Info |
Lifecycle milestones: connected, room joined, region selected. | console.info() |
Warning |
Unexpected but recoverable conditions worth investigating. | console.warn() |
Error |
Failures; something did not work as requested. | console.error() |
Implementing a Log Output
Log outputs are a C++ mechanism. The C API cannot register one, and in JavaScript the package's own output writes to the console; see Enabling Logging.
A log output is a class deriving from the abstract RealtimeCore::Common::LogOutput, implementing one sink per level: LogTrace, LogDebug, LogInfo, LogWarning and LogError.
Each sink receives the finished message as a UTF-8 const CharType*; route it to wherever your game's diagnostics live, such as a console, a file or an in-game overlay.
A minimal output writing every message to stderr:
C++
class ConsoleLogOutput : public RealtimeCore::Common::LogOutput
{
public:
void LogTrace(const RealtimeCore::Common::CharType* message) override { Print("TRACE", message); }
void LogDebug(const RealtimeCore::Common::CharType* message) override { Print("DEBUG", message); }
void LogInfo(const RealtimeCore::Common::CharType* message) override { Print("INFO", message); }
void LogWarning(const RealtimeCore::Common::CharType* message) override { Print("WARN", message); }
void LogError(const RealtimeCore::Common::CharType* message) override { Print("ERROR", message); }
private:
static void Print(const char* level, const RealtimeCore::Common::CharType* message)
{
std::fprintf(stderr, "[Photon][%s] %s\n", level, reinterpret_cast<const char*>(message));
}
};
The C API has no logging functions, so a C client cannot register a log output. It logs the errors and warnings it receives through its event queue itself, as shown under Enabling Logging.
Custom log outputs are not available in JavaScript. The package registers its own output, which writes every SDK log line to the console; see Enabling Logging.
Registering Log Outputs
In C++, AddLogOutput(&output) registers a sink and RemoveLogOutput(&output) removes it, returning whether it was found.
Several outputs can be active at the same time, LogOutputCount() reports how many, and every enabled message is delivered to all of them.
The registry stores a raw pointer, so the output object must stay alive for as long as it is registered.
Give outputs static or application lifetime, or call RemoveLogOutput before destroying one.
Last updated on
Back to top