NxLog

class nxlib.log.NxLog(level=None, filename=None, open_in_profiler=False)

This class offers a simple to use interface for logging NxLib events. It implements the context manager protocol and thus can be used in a with statement, which encloses the code to be logged. This has the nice side effect, that the code to be logged becomes a nested block of code that visually contrasts from its surrounding.

All first-level NxLog objects produce a log file that can be opened with NxProfiler. These log files are by default serially numbered and placed in a folder named nxlog/ relative to the executing script. The output path can be changed for all upcoming NxLog objects with the set_output_path() method. Resetting the output path is can be done by calling the same method with None.

By default the NxLib log level is NX_LOG_LEVEL_TRACE. It can either be set globally for all NxLog instances with the set_log_level() method or individually for each instance with the level parameter in the constructor. Note that the NX_LOG_LEVEL_OFF will be ignored and NX_LOG_LEVEL_TRACE will be used instead, because otherwise nothing will be logged.

NxLog instances can be nested to further increase the granularity of event grouping. Each nested with statement opens a new level of indentation and should be called with a block name, otherwise there won’t be any grouping within the log file.

Note

All nested NxLog objects log to its corresponding first-level/parent object, which finally writes the log file to disk when its scope ends.

If desired, the written log file is automatically opened with the NxProfiler executable if the open_in_profiler parameter of the constructor was set to True or the opening was globally enabled with the enable_open_in_profiler() class method.

With the write_debug_message() class method user defined debug messages can be written to the log. The message will automatically belong to the currently open debug block if there is one.

Parameters
  • level (str, optional) – The NxLib log level. Defaults to NX_LOG_LEVEL_TRACE.

  • filename (str, optional) – The NxLog filename. If none is given, a unique serially numbered filename is created. The .nxlog extension is added if missing. Defaults to None.

  • open_in_profiler (bool, optional) – If True, the written log file is automatically opened with the NxProfiler executable. Defaults to False.

static disable_open_in_profiler()

Disables automatic opening of the created log files with NxProfiler.

static disable_warnings()

Disables printing of warnings.

static enable_open_in_profiler()

Enables automatic opening of the created log files with NxProfiler.

static enable_warnings()

Enables printing of warnings.

static get_debug_buffer()

This method uses nxlib.api.get_debug_buffer() to retrieve all bytes from the debug buffer. Therefore the API function is called twice, once to determine the number of bytes held by the debug buffer and a second time to actually read and clear the debug buffer.

Note

A warning is printed if the debug buffer overflowed and warnings are enabled. In this case the debug buffer size should be increased via the tree item /Debug/BufferSize.

Returns

The debug buffers content as bytes.

Return type

bytes

static set_log_level(log_level)

Sets the log level globally for all instances to the given value. Reset the log level by either calling this method with log_level=None or log_level=NX_LOG_LEVEL_OFF.

Parameters

log_level (int) – The log level to be set.

static set_output_path(output_path)

Sets the output path globally for all instances to the given path. Reset the output path by calling this method with output_path=None.

Parameters

output_path (str or pathlib.Path) – The output path to be set.

static write_debug_message(message)

Inserts a user defined debug message into the NxLib debug stream. The message will belong to the last opened debug block if there is any.

Parameters

message (str) – The message to be written.

NxDebugBlock

class nxlib.log.NxDebugBlock(name, level=None, filename=None, open_in_profiler=False)

This class offers a simple to use interface for opening an NxLib debug block. It is derived from the nxlib.log.NxLog class and thus can be used as this to create a log file. It also implements the context manager protocol and thus can be used in a with statement, which encloses the code to be logged. This has the nice side effect, that the code to be logged becomes a nested block of code that visually contrasts from its surrounding.

Parameters
  • name (str, optional) – The name of the debug block. Defaults to None.

  • level (str, optional) – The NxLib log level. Defaults to NX_LOG_LEVEL_TRACE.

  • filename (str, optional) – The NxLog filename. If none is given, a unique serially numbered filename is created. The .nxlog extension is added if missing. Defaults to None.

  • open_in_profiler (bool, optional) – If True, the written log file is automatically opened with the NxProfiler executable. Defaults to False.