Logging
Access the log view through master.logs(), or control the log switches through the static methods of com.darra.ethercat.statics.StaticOther.
Runtime logs reach master.logs() (a read-only list) through the global log callback. Call StaticOther.initializeLogging() once before use to register the SDK's log callback (idempotent); you can also take over this global slot with master.setLogCallback(...), but then logs no longer enter master.logs() automatically and you have to forward them yourself.
The output location of logs is the in-memory view master.logs(); the SDK does not write log files and does not print to the console. To get them into a file or the console, forward them yourself in a listener. Every log entry carries a timestamp, a category and a message, and toString() outputs [yyyy-MM-dd HH:mm:ss.SSS] [类别] 消息.
Quick start
// Register the SDK log callback (idempotent, once is enough)
StaticOther.initializeLogging();
// Bind console output
master.logs().addUpdatedListener(() -> {
LogManager.LogView logs = master.logs();
if (logs.getCount() > 0) {
System.out.println(logs.get(logs.getCount() - 1));
}
});
// Show errors and warnings only
master.logs().setFilter(LogManager.LogCategory.ERROR, LogManager.LogCategory.WARNING);
// Enable debug logging (off by default)
StaticOther.enableDebugLogging(true);
Log categories
public enum LogCategory {
ERROR(0), // system errors and exceptions
WARNING(1), // potential problems
MESSAGE(2), // general information (state changes and so on)
MAILBOX(3), // CoE/SoE/FoE/EoE mailbox communication (off by default)
PDO(4), // PDO data-change log (off by default)
DEBUG(5), // detailed debug information (off by default)
LOCAL(6); // local log
}
Related enums:
| Member | Value | Meaning | In view by default |
|---|---|---|---|
ERROR | 0 | System errors and exceptions | Yes |
WARNING | 1 | Potential problems | Yes |
MESSAGE | 2 | General information (state changes and so on) | Yes |
MAILBOX | 3 | CoE/SoE/FoE/EoE mailbox communication | No; add it to the filter explicitly and turn the switch on |
PDO | 4 | PDO data-change log | No; add it to the filter explicitly and turn the switch on |
DEBUG | 5 | Detailed debug information | No; start with -Ddarra.sdk.debug=true |
LOCAL | 6 | Local log | Yes |
LogCategory exposes the public field value (the enum's numeric value) and the static method fromValue(int) (an unknown value falls back to MESSAGE).
- Write side:
DEBUGis discarded by default and is kept only when the JVM is started with-Ddarra.sdk.debug=true;PDO/MAILBOXare discarded by default and must be let through withsetPdoTraceEnabled(true)/setMailboxTraceEnabled(true)respectively. - View side:
setFilter(...)decides which categories the view shows; the default isERROR/WARNING/MESSAGE.
Both have to be open before high-frequency protocol logs appear in master.logs().
The PDO log is output only when PDO data changes; it does not produce a log entry every cycle. It is useful for debugging PDO mapping problems.
Method overview
The live log view (a read-only list) is maintained automatically by the logging system:
| Category | Method | Type | R/W | Description |
|---|---|---|---|---|
| Log view | master.logs() | LogManager.LogView | Read-only | Live log view |
getCount() | int | Read-only | Number of entries after filtering | |
get(int index) | LogEntry | Read-only | Gets a log entry by index (index after filtering) | |
getAll() | List<LogEntry> | Read-only | All entries (ignoring the filter) | |
getFilteredSnapshot() | List<LogEntry> | Read-only | Snapshot after filtering | |
setFilter(LogCategory...) | void | Write-only | Sets the category filter | |
addUpdatedListener(Runnable) / removeUpdatedListener(Runnable) | void | Write-only | Update listener | |
| Log singleton | LogManager.getInstance() | LogManager | Read-only | Log manager singleton |
getDefaultView() | LogView | Read-only | Default log view (the one master.logs() returns) | |
addLog(LogCategory, String) / addLog(int, String) | void | Write-only | Writes one entry to the view (overload takes the category value) | |
clear() | void | Write-only | Clears all entries | |
| Write switches | LogManager.getInstance().setPdoTraceEnabled(boolean) / isPdoTraceEnabled() | void / boolean | Read-write | Whether PDO logs are let into the view (off by default) |
LogManager.getInstance().setMailboxTraceEnabled(boolean) / isMailboxTraceEnabled() | void / boolean | Read-write | Whether mailbox logs are let into the view (off by default) | |
| Log switches | StaticOther.enablePDOLogging(boolean) | static void | Write-only | Runtime-side PDO log switch (off by default) |
StaticOther.enableMailboxLogging(boolean) | static void | Write-only | Runtime-side mailbox log switch (off by default) | |
StaticOther.enableDebugLogging(boolean) | static void | Write-only | Runtime-side debug log switch (off by default) | |
StaticOther.initializeLogging() | static void | Write-only | Registers the SDK log callback (idempotent) | |
StaticOther.isLoggingInitialized() | static boolean | Read-only | Whether initialization has happened | |
StaticOther.setSdkLogCallbackEnabled(boolean) | static void | Write-only | Whether the SDK may take the callback slot (must be called before initialization) | |
StaticOther.releaseLogging() | static boolean | Write-only | Returns the callback slot | |
| Callbacks | master.setLogCallback(...) | void | Write-only | Takes over the underlying log slot |
master.setCrashCallback(...) | void | Write-only | Crash notification callback |
Related structures:
public class LogEntry {
public final Date timestamp; // log timestamp
public final LogCategory category; // log category
public final String message; // log message text
// toString() output: [yyyy-MM-dd HH:mm:ss.SSS] [category] message
}
LogEntry has a public constructor LogEntry(LogCategory, String), which you can use to output the application's own log entries in the same format.
API details
master.logs()
public LogManager.LogView logs()
Returns the live log view (a read-only list), maintained automatically by the logging system. The view holds at most 10000 entries; when the limit is exceeded the oldest batch is removed automatically. getAll() returns a snapshot copy of the current contents, so entries written afterwards are not reflected in a list you have already taken.
Return value:
LogManager.LogView— the default view (equivalent toLogManager.getInstance().getDefaultView())
setFilter(LogCategory... categories)
public void setFilter(LogCategory... categories)
Sets the filter so that only the given categories are shown. Replaces the previous filter settings.
Parameters:
categories(LogCategory...) — log categories to show; pass nothing and the view is empty
Example:
// Show errors and warnings only
master.logs().setFilter(LogManager.LogCategory.ERROR, LogManager.LogCategory.WARNING);
// Switch to mailbox communication only (replaces the filter)
master.logs().setFilter(LogManager.LogCategory.MAILBOX);
getCount()
public int getCount()
Return value:
int— the current number of entries after filtering (not the total number of entries)
get(int index)
public LogEntry get(int index)
Gets a filtered log entry by index.
Parameters:
index(int) — index after filtering (0..getCount() - 1)
Return value:
LogEntry— the log entry
Exceptions:
IndexOutOfBoundsException— the index is out of range
getAll()
public List<LogEntry> getAll()
Gets all log entries (ignoring the filter) and returns a snapshot copy. Useful for export or custom filtering.
Return value:
List<LogEntry>— a copy of all entries
getFilteredSnapshot()
public List<LogEntry> getFilteredSnapshot()
Gets a snapshot copy of the filtered log entries (the batch form of getCount() + get(int), so you can take it once and then traverse it at your own pace).
Return value:
List<LogEntry>— a copy of the filtered entries
addUpdatedListener(Runnable) / removeUpdatedListener(Runnable)
public void addUpdatedListener(Runnable listener)
public void removeUpdatedListener(Runnable listener)
Adds / removes a log change listener: it fires when a new entry is written or the filter changes. An add call with null is ignored.
Exceptions:
- An exception thrown inside the listener is swallowed and does not interrupt the main logging path; do not rely on throwing exceptions inside a listener to control flow
Log switches
The mailbox, PDO and debug logs are off by default and are enabled manually when needed. The switches are process-level static methods defined on com.darra.ethercat.statics.StaticOther.
import com.darra.ethercat.statics.StaticOther;
enablePDOLogging(boolean enable)
public static void enablePDOLogging(boolean enable)
Enables the PDO log. It outputs only when PDO data changes; it affects real-time performance, so use it with care.
Parameters:
enable(boolean) —true= on
Notes: This switch only controls whether logs are produced. For entries to appear in master.logs() you also need LogManager.getInstance().setPdoTraceEnabled(true) and LogCategory.PDO added to the filter.
enableMailboxLogging(boolean enable)
public static void enableMailboxLogging(boolean enable)
Enables mailbox logging (CoE/SoE/FoE/EoE mailbox communication logs).
Parameters:
enable(boolean) —true= on
Notes: As above; on the view side you need setMailboxTraceEnabled(true) and the filter must let MAILBOX through.
enableDebugLogging(boolean enable)
public static void enableDebugLogging(boolean enable)
Enables debug logging (detailed debug information).
Parameters:
enable(boolean) —true= on
Notes: On the view side, DEBUG entries are controlled by the startup parameter -Ddarra.sdk.debug=true (decided when the JVM starts; it cannot be changed at run time). Debug logs appear in master.logs() only when both conditions are met.
initializeLogging / isLoggingInitialized / setSdkLogCallbackEnabled / releaseLogging
public static void initializeLogging()
public static boolean isLoggingInitialized()
public static void setSdkLogCallbackEnabled(boolean enabled)
public static boolean releaseLogging()
Initialization, state query, slot authorization and release for the logging system.
Semantics:
initializeLogging()is idempotent: repeated calls return immediately and do not overwrite a callback you registered afterwards. It must be called once explicitly before runtime logs are forwarded tomaster.logs(); the DC event stream, which depends on the log stream, also requires initialization.- The log / crash callback is a single process-wide slot. If the application manages the callback entirely on its own, call
setSdkLogCallbackEnabled(false)first and theninitializeLogging(), and the SDK does not occupy the slot. Calling that method after initialization has no effect (the slot is already settled). releaseLogging()returns the slot: it unregisters the callback the SDK registered, after which the application can register its own. Returnstrue= unregistered (or nothing was registered);false= unregistering failed.
Callbacks
master.setLogCallback(...)
public void setLogCallback(DarraCore.LogCallback callback)
public interface LogCallback {
void invoke(int category, Pointer message); // message = UTF-8 string pointer
}
Sets the log callback, which receives all runtime log messages.
Parameters:
callback— the callback;categoryis the log category value (matchingLogCategory.value) andmessageis a UTF-8 string pointer
Example:
master.setLogCallback((category, message) -> {
String text = (message != null) ? message.getString(0, "UTF-8") : "";
System.out.println("[" + LogManager.LogCategory.fromValue(category) + "] " + text);
});
Semantics:
- The log callback is a single process-wide slot. This method occupies it and makes a later
StaticOther.initializeLogging()leave your callback untouched; after this call, logs no longer entermaster.logs()automatically and you have to forward them yourself - To hand the slot back to the SDK, use
StaticOther.releaseLogging()
master.setCrashCallback(...)
public void setCrashCallback(DarraCore.CrashNotifyCallback callback)
public interface CrashNotifyCallback {
void invoke(int errorCode, Pointer details); // details = detail string pointer
}
Sets the crash notification callback, which fires when a fatal error occurs.
Parameters:
callback— the callback;errorCodeis the error code anddetailsis the detail (a UTF-8 string pointer)
Example:
master.setCrashCallback((errorCode, details) -> {
String text = (details != null) ? details.getString(0, "UTF-8") : "";
System.out.println("崩溃: code=" + errorCode + ", " + text); // Crash: code={errorCode}, {text}
});
Semantics: The crash slot is separate from the log slot: once registered, the SDK's crash notifications are still forwarded to each master instance through the master registry, so the two do not compete.
Performance constraints
- What is off by default is the high-frequency categories:
PDO/MAILBOX/DEBUGare discarded by default. Before enabling them, remember they are for debugging; do not leave them on in production. - The view has a limit: at most 10000 entries are kept, and when the limit is exceeded the oldest batch is removed in one go. For long-term retention, write the entries to disk yourself in a listener.
- The listener runs synchronously on the thread that writes the log: the implementation must return quickly and must not do heavy work; to update the UI, switch back to the UI thread (see the Swing / JavaFX example below).
- Do not write logs from inside a listener: it re-enters.
- An exception thrown by the callback itself is turned into an ERROR log entry on the spot and does not propagate into the runtime.
Full example
// 0. Register the SDK log callback (idempotent)
StaticOther.initializeLogging();
// 1. Bind console output (show all logs live)
master.logs().addUpdatedListener(() -> {
LogManager.LogView logs = master.logs();
if (logs.getCount() > 0) {
System.out.println(logs.get(logs.getCount() - 1));
}
});
// 2. Enable debug logging (produced on the runtime side + let through on the view side)
StaticOther.enableDebugLogging(true);
StaticOther.enableMailboxLogging(true);
LogManager.getInstance().setMailboxTraceEnabled(true); // let mailbox logs through on the view side
// 3. Filter logs (errors and debug only)
master.logs().setFilter(
LogManager.LogCategory.ERROR,
LogManager.LogCategory.DEBUG);
// 4. Export all logs (ignoring the filter)
List<LogManager.LogEntry> allLogs = master.logs().getAll();
for (LogManager.LogEntry entry : allLogs) {
System.out.println(entry);
}
addUpdatedListener fires on a non-UI thread; when updating the UI you need to synchronize:
// Swing
master.logs().addUpdatedListener(() ->
SwingUtilities.invokeLater(() ->
listModel.addElement(master.logs().get(master.logs().getCount() - 1))));
// JavaFX
master.logs().addUpdatedListener(() ->
Platform.runLater(() ->
logItems.add(master.logs().get(master.logs().getCount() - 1))));
Failures and handling
| Symptom | Meaning | Action |
|---|---|---|
No logs visible in master.logs() | StaticOther.initializeLogging() was not called, or the slot was taken over by setLogCallback | Initialize first; if you took over the callback yourself, forward the entries into the view yourself |
enablePDOLogging is on but no PDO logs appear | The view side has not let them through | Call setPdoTraceEnabled(true) and add PDO to setFilter(...) |
enableMailboxLogging is on but no mailbox logs appear | As above | Call setMailboxTraceEnabled(true) and let MAILBOX through |
enableDebugLogging is on but no debug logs appear | The view side is decided by the startup parameter | Add -Ddarra.sdk.debug=true when starting the JVM |
| Only a few categories are wanted but nothing appears | setFilter(...) replaces the whole filter | Pass all the categories you want to show, not just the newly added one |
get(index) throws IndexOutOfBoundsException | The index is counted after filtering | Call getCount() first, or use getFilteredSnapshot() |
| Older entries keep disappearing | The view is capped at 10000 entries | For long-term retention, write them to disk yourself in a listener |
| The UI update reports a thread error | The listener fires on the thread that writes the log | Switch back to the UI thread with SwingUtilities.invokeLater / Platform.runLater |
initializeLogging() throws | The runtime is not loaded | Load the runtime first, then initialize logging |