When you write a plugin for the Media Server, one of your first tasks is to print out debug information. Because a plugin is a dynamic library that runs directly within the server process, you can choose between several logging methods.
This guide explains your logging options, how to use the built-in nx_kit library, and how to capture output streams.
Choose a logging option
You can log data from a plugin in two ways:
Print to
stdoutorstderr: This is the recommended approach. You can use standard C++ streams likestd::coutandprintf(), or use thenx_kithelper library included with the SDK. Thenx_kitlibrary automatically adds logging levels and context prefixes to your messages.Use a custom logging subsystem: You can integrate your own library or a third-party logging framework. This approach is only recommended if you already have an existing logging infrastructure.
| NOTE: The advanced native logging subsystem of the Nx Witness Server writes logs directly to files, but this subsystem is currently unavailable to plugins. To view server-side issues caused by plugins, see the [VMS logging documentation] to learn how to access server logs and adjust log levels. |
Log with nx_kit
The nx_kit library (nx/kit/debug.h) provides a simple way to log messages to stderr. It uses two primary macros to define log levels:
NX_PRINT: Prints the message unconditionally. Use this for regular log messages.NX_OUTPUT: Prints the message only if your configuration allows it. Use this for verbose log messages.
Compared to std::cerr, these macros offer the following advantages:
Automatic newlines: You do not need to add
\nat the end of your messages.Context prefixes: The macro automatically prefixes the message with the filename that calls it. To customize this prefix, define
NX_PRINT_PREFIXbefore including the header.
nx_kit defaults to stderr because it is unbuffered, which reduces the risk of message collision when multiple threads write simultaneously. To change the target stream, see the Doxygen documentation for nx/kit/debug.h.
Code example
// my_module.cpp
#define NX_DEBUG_ENABLE_OUTPUT true
#include <nx/kit/debug.h>
void func()
{
// Automatically appends a newline character
NX_PRINT << "Printed unconditionally.";
NX_OUTPUT << "Printed only if NX_DEBUG_ENABLE_OUTPUT is true.";
}Output:
[my_module.cpp] Printed unconditionally.
[my_module.cpp] Printed only if NX_DEBUG_ENABLE_OUTPUT is true.Switch log levels at runtime with an INI file
To dynamically change your log levels without recompiling your code, use the IniConfig mechanism in nx_kit.
-
Add an
enableOutputboolean flag to your.inioptions structure:
// my_module_ini.h #include <nx/kit/ini_config.h> struct Ini: nx::kit::IniConfig { Ini(): IniConfig("my_module.ini") { reload(); } NX_INI_FLAG(0, enableOutput, "Whether to produce verbose log on stderr."); }; inline Ini& ini() { static Ini ini; return ini; } -
Include your INI header in your source file. The
NX_OUTPUTmacro automatically detects theini().enableOutputexpression:
// my_module.cpp #include "my_module_ini.h" #include <nx/kit/debug.h> void func() { NX_OUTPUT << "Printed only if my_module.ini contains the line: enableOutput=1"; }
For more details, see the article about configuring via .ini files.
Redirect streams to files with OutputRedirector
The nx_kit library includes an OutputRedirector feature that automatically captures stdout and stderr streams into log files. This feature works on both Windows and Linux, and is especially useful on Windows when the server runs as a background service.
NOTE: On Windows, the OutputRedirector mechanism does not capture streams if the plugin uses a Debug build configuration. |
Configure file redirection
Navigate to the
.inifiles folder for your process. To find this path, see the [Configuring via .ini files – IniConfig] documentation.-
Create one or both of the following empty files in that directory:
<executable-name-without-extension>_stdout.log<executable-name-without-extension>_stderr.log
When the process starts, OutputRedirector detects these files and begins redirecting the respective streams. The application overwrites these files and clears previous data every time the process restarts.
For details on integrating this into your own apps, see the Doxygen documentation for nx/kit/output_redirector.h.
Example: Redirect server output on Linux
To capture the output of an Nx Witness Server running as a service on Ubuntu Linux, complete the following steps:
-
Create the empty log files in your configuration directory:
sudo touch /home/networkoptix/.config/nx_ini/mediaserver_stdout.log sudo touch /home/networkoptix/.config/nx_ini/mediaserver_stderr.log -
Restart the server:
sudo systemctl restart networkoptix-mediaserver.service -
Monitor the standard error log in real time:
tail -F /home/networkoptix/.config/nx_ini/mediaserver_stderr.log
View console output without redirection
If you do not want to redirect output to files, you can view the streams directly through your operating system's tools.
For Linux
If you run the default server installation as a system service, run the following command to view the live log stream:
journalctl -u networkoptix-mediaserver.serviceAlternatively, you can run the server directly in your terminal console:
Navigate to the directory that contains your server executables.
-
Launch the executable with the
-eoption:./networkoptix-mediaserver/bin/mediaserver -e
For Windows
Launching the Nx Witness Server from the Windows Command Prompt does not display standard output streams. To view stdout and stderr logs on Windows, you must use the Configure file redirection method with OutputRedirector.
Comments
0 comments
Article is closed for comments.