Condition and Logging

A running pipeline cannot raise an exception in your code: its nodes run on threads nobody is waiting on. It reports through two channels instead. The session log says what happened; the condition says whether the run is still good.

Every log line goes to the session log. An info line leaves the condition as it is; a warning turns it to Warning until the next start(), and the run goes on. An error, raised or logged, turns it to Error and stops the run, and is then available from p.failure, p.raise_if_failed() and an error handler.

The session log

Each run writes a new file, in %APPDATA%\gtec\gPype on Windows, ~/Library/Application Support/gtec/gPype on macOS and ~/.local/share/gtec/gPype on Linux. A line has five columns – timestamp | type | source | thread | message – and a type of INFO, WARNING or ERROR. The source column names the node instance you passed as name=, which is what tells two nodes of the same class apart; the thread column explains why lines from different sources interleave. Season 6, Episode 1 reads one.

A node of your own writes to the same log with self.log("...", type=gp.Constants.LogTypes.WARNING).

The condition

p.get_condition() answers while the pipeline runs: Healthy, Warning or Error (gp.Constants.Conditions). A node’s log line moves it by its level:

  • A warning turns it to Warning, and the run goes on. A node that logs at WARNING – one of g.Pype’s, or your own with self.log() – moves the condition to Warning within a second. It stays there until the next start(), however quiet the rest of the run.

  • An error turns it to Error and stops the run. That happens when a node raises in setup() or step(), and equally when anything logs at ERROR without raising: the condition reads Error and the state Stopped straight after.

g.Pype’s own warnings go the same way. One about where an event was placed (Time and Synchronisation) is printed on the console and written to the session log under the placing node’s name – “merge: Router.log() … 1 observation(s) fell in a sample period that was already occupied” – and the condition turns to Warning. So are the transport’s warnings between processes, logged by the side that receives the stream: a connection lost, data dropped after the first connection, a stream refused because another edge already sends it. Two kinds stay on the console only: an edge that cannot reach its server yet, because an edge started ahead of its server is an ordinary start, and the warnings that concern no one stream, such as a refused authentication.

The second rule is how a device that dies mid-run is reported. An amplifier’s driver acquires on a thread of its own, and when that thread ends on an error – the device switched off, the cable pulled – a watch thread notices within half a second and logs it at ERROR. The run stops with Error instead of carrying on while no data arrives. A BCI Core that sends nothing is a warning after 2 s and an error after 60 s, whether its link still reports connected or the driver reports it down.

After a failure

Three ways to hear about it, for three kinds of program:

  • p.failure is the log entry that ended the run: its message, the node and where it was raised. None while nothing has failed.

  • p.raise_if_failed() turns it into an exception, for a script that would rather crash than carry on: “The pipeline failed: RuntimeError: frame 20 broke me in node ‘flaky’ at Flaky.step() …”.

  • p.add_error_handler(fn) registers a function called once when the run fails, after the condition reads Error and the pipeline has stopped. It runs on the monitoring thread, so a GUI should hand the news to its own thread rather than touch a widget from there. Without a handler, the only report is on the console, which a GUI application may not show.

A script with no window can check between waits, and stops at the first failure with the node’s name:

p.start()
for _ in range(60):      # one minute
    time.sleep(1)
    p.raise_if_failed()
p.stop()

A failed pipeline can be started again, and its condition reads Healthy in the new run. Season 6, Episode 4 shows what a failed run leaves on disk: a recording that ends early, closed properly.