fix(device): print --print-channels and --version output to stdout

- DeviceRunner sets the console log severity to "nolog" for both
  options, but printed their output with LOGV(info), so both options
  printed nothing
- Add Device::PrintRegisteredChannels(std::ostream&) for the
  machine-readable channel list; the version goes to std::cout, like
  --help and --print-options
- Enable DeviceRunner.PrintChannels and DeviceRunner.Version
- Update the documentation pages that described the defect, and
  the source line links into the changed files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-04 20:31:28 +00:00
committed by Dennis Klein
parent b70595cebe
commit 9b6662f6a9
18 changed files with 42 additions and 41 deletions

View File

@@ -24,7 +24,7 @@ A sub-channel has the name `<channel>[<index>]`, for example `data-out[1]`. The
- [`Send(msg, "data-out", 1)`](/api/fair/mq/Device/#Send) and [`Receive(msg, "data-in")`](/api/fair/mq/Device/#Receive) send and receive through it. The index is the third argument. Its default is 0.
- [`GetNumSubChannels("data-out")`](/api/fair/mq/Device/#GetNumSubChannels) returns the number of sub-channels.
For a channel or an index that does not exist, `GetChannel()` logs `GetChannel(): 'data-out[2]' does not exist.` and throws `std::out_of_range` ([Device.h](/source/fairmq/Device.h/#L323-L329)).
For a channel or an index that does not exist, `GetChannel()` logs `GetChannel(): 'data-out[2]' does not exist.` and throws `std::out_of_range` ([Device.h](/source/fairmq/Device.h/#L324-L330)).
The sampler of the [copypush example](/examples/copypush/) sends a copy of each message to each sub-channel of its channel `data`:

View File

@@ -17,14 +17,14 @@ A property has a key of the type `std::string` and a value of any C++ type. The
A device has a pointer to its store: [`fConfig`](/api/fair/mq/Device/#fConfig), also from [`GetConfig()`](/api/fair/mq/Device/#GetConfig). A device that you make with the constructor `Device()` has its own internal store. [`SetConfig()`](/api/fair/mq/Device/#SetConfig) replaces it with another store and discards the internal store ([Device.cxx](/source/fairmq/Device.cxx/#L607-L611)). With `runDevice.h`, the [device runner](/api/fair/mq/DeviceRunner/) makes the store and gives it to the device with `SetConfig()`.
:::caution[Properties from the device constructor]
With `runDevice.h`, the device runner calls `SetConfig()` after it makes the device ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L172-L173)). A device that uses the constructor `Device()` loses all properties and channels that its constructor sets. Set them in [`Init()`](/api/fair/mq/Device/#Init), or give the store to the constructor `Device(ProgOptions&)`.
With `runDevice.h`, the device runner calls `SetConfig()` after it makes the device ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L174-L175)). A device that uses the constructor `Device()` loses all properties and channels that its constructor sets. Set them in [`Init()`](/api/fair/mq/Device/#Init), or give the store to the constructor `Device(ProgOptions&)`.
:::
The old name `FairMQProgOptions` is a deprecated alias of `fair::mq::ProgOptions`. Refer to [Move from the FairMQ* names](/guides/migrate-names/).
## Where the properties come from
A device made with `runDevice.h` gets its properties in this sequence ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L108-L194)):
A device made with `runDevice.h` gets its properties in this sequence ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L109-L196)):
1. The device runner loads the config plugin and the control plugin, and collects the options of all plugins and your `addCustomOptions()`.
2. It parses the command line. Each option becomes a property with the name of the option, for example `id` or `severity`. An option that you do not give gets its default, if it has one.
@@ -47,7 +47,7 @@ The device reads most properties once, at the request of the transition `COMPLET
| `chans.<name>.<index>.<property>` | At the request of `COMPLETE INIT` | No effect, except for the address of a connecting sub-channel that the device could not connect yet |
| `io-threads`, `session`, `shmid`, the `shm-*` and `bad-alloc-*` options | When the device makes the transport, at the request of `COMPLETE INIT` | No effect |
| `control`, `catch-signals` | When the device runner makes the control plugin | No effect |
| `severity`, `file-severity`, `verbosity`, `color`, `log-to-file` | After the parse of the command line | The device runner applies the new value at once ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L82-L101)) |
| `severity`, `file-severity`, `verbosity`, `color`, `log-to-file` | After the parse of the command line | The device runner applies the new value at once ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L83-L102)) |
| Your custom options | When your code reads them | Your code decides |
## Read and change properties

View File

@@ -211,10 +211,8 @@ You can write your own `main()` with `DeviceRunner`. The example
and catches all exceptions. It logs `Uncaught exception reached the top of DeviceRunner:` and
returns 1.
:::caution[Known defect]
For `--print-channels` and `--version`, `DeviceRunner` sets the console severity to `nolog` and then
prints with `LOG`. Thus both options print nothing in this version. The device does not start.
:::
For `--print-channels` and `--version`, `DeviceRunner` sets the console severity to `nolog` and
writes the output to the standard output. The device does not start.
A process can also run more than one device without `DeviceRunner`. Then your code must call
`RunStateMachine()` and request the transitions. See

View File

@@ -32,7 +32,7 @@ plugin ──► PluginServices ──► Device (state, transitions)
## How a device starts
[`DeviceRunner::Run()`](/api/fair/mq/DeviceRunner/#Run) loads and creates the plugins in this sequence
([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L108-L194)):
([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L109-L196)):
1. The constructor of `DeviceRunner` makes the plugin manager. The plugin manager reads `-S` and `-P` and loads the
plugins of `-P`, in their sequence.

View File

@@ -61,7 +61,7 @@ The program stops by itself about 3 s after the device enters `RUNNING`. Its exi
- In `RUNNING`, the callback waits 3 s with [`WaitFor()`](/api/fair/mq/Device/#WaitFor). Then it sets the property `please-shut-me-down` to `true`.
- [`RunWithExceptionHandlers()`](/api/fair/mq/DeviceRunner/#RunWithExceptionHandlers) runs the device. It returns 1 if an exception reaches it.
The hooks are declared in [`DeviceRunner.h`](/source/fairmq/DeviceRunner.h/#L84-L89). The order of the steps is in [`DeviceRunner.cxx`](/source/fairmq/DeviceRunner.cxx/#L108-L194).
The hooks are declared in [`DeviceRunner.h`](/source/fairmq/DeviceRunner.h/#L84-L89). The order of the steps is in [`DeviceRunner.cxx`](/source/fairmq/DeviceRunner.cxx/#L109-L196).
### The controller plugin

View File

@@ -113,7 +113,7 @@ The sampler overrides [`Run()`](/api/fair/mq/Device/#Run) instead of `Conditiona
- At `--max-iterations`, the loop ends. When `Run()` returns, the device goes to `READY`.
All channels of one poller must use the same transport. Otherwise `NewPoller()` throws an exception
([Device.h](/source/fairmq/Device.h/#L218-L238)).
([Device.h](/source/fairmq/Device.h/#L219-L239)).
## The sink

View File

@@ -311,7 +311,7 @@ device options that you do not register, for example `transport` and `rate`, hav
A [`fair::mq::PluginManager`](/api/fair/mq/PluginManager/) loads, creates and owns the plugins of a device. Without
`DeviceRunner`, your program must call its functions in the sequence of
[`DeviceRunner::Run()`](/source/fairmq/DeviceRunner.cxx/#L108-L194):
[`DeviceRunner::Run()`](/source/fairmq/DeviceRunner.cxx/#L109-L196):
| Step | Call | Notes |
| --- | --- | --- |

View File

@@ -147,7 +147,7 @@ ends. For all other errors, it logs `polling failed, reason: <error>` and throws
## Use one transport for each poller
All channels of one poller must use the same transport. Both `NewPoller()` functions compare the
transports before they make the poller ([Device.h](/source/fairmq/Device.h/#L218-L257)). For the
transports before they make the poller ([Device.h](/source/fairmq/Device.h/#L219-L258)). For the
channel names, they compare the sub-channel 0 of each channel. If the transports are different,
they log an error and throw `std::runtime_error`. If your `Run()` does not catch the exception, the
device goes to the state `ERROR`.

View File

@@ -209,7 +209,7 @@ more than `GetSize()` bytes.
[`Device::Serialize<Serializer>(msg, data, args…)`](/api/fair/mq/Device/#Serialize) and
[`Device::Deserialize<Deserializer>(msg, data, args…)`](/api/fair/mq/Device/#Deserialize) are
deprecated ([Device.h](/source/fairmq/Device.h/#L78-L88)). They make a temporary `Serializer` or
deprecated ([Device.h](/source/fairmq/Device.h/#L79-L89)). They make a temporary `Serializer` or
`Deserializer` object and call its member function `Serialize(msg, data, args…)` or
`Deserialize(msg, data, args…)`. They add nothing else. Call your class directly instead:

View File

@@ -13,7 +13,7 @@ A device made with `runDevice.h` accepts the options on this page. The [table](#
## Where the options come from
The device runner ([`DeviceRunner`](/api/fair/mq/DeviceRunner/)) collects the options from several places before it parses the command line ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L108-L132)):
The device runner ([`DeviceRunner`](/api/fair/mq/DeviceRunner/)) collects the options from several places before it parses the command line ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L109-L133)):
| Source | Group in `--help` | Group in the table | Options |
| --- | --- | --- | --- |
@@ -28,7 +28,7 @@ The device runner always loads the config plugin and the control plugin. The [co
## Show the options of a device
`--help` (or `-h`) prints all options of the device, with their groups, defaults and descriptions. Then the program stops before it makes the device ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L29-L39)).
`--help` (or `-h`) prints all options of the device, with their groups, defaults and descriptions. Then the program stops before it makes the device ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L30-L40)).
`--print-options` prints one line for each option, in the format `name:value:type:description` ([ProgOptions.cxx](/source/fairmq/ProgOptions.cxx/#L430-L448)). The value is the value after the parse of the command line. For an option without a value, the value is empty and the type is `<unknown>`. Then the program stops before it makes the device.
@@ -47,8 +47,8 @@ control:dynamic:string:Control mode, 'static' or 'dynamic' (aliases for dynamic
When the device starts, it logs all properties with the severity `debug`, after the line `Configuration:` ([ProgOptions.cxx](/source/fairmq/ProgOptions.cxx/#L384-L428)). Each line has the form `name = value <type> origin`. The origin is `[default]`, `[provided]` or `[empty]`. The channel properties from `--channel-config` and `--mq-config` are not in this log, because the config plugin reads them later, in `INITIALIZING DEVICE`.
:::caution[`--version` and `--print-channels`]
`--version` and `--print-channels` print nothing in this version. The device runner sets the console log to `nolog` for these two options, and then writes the output to the log ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L41-L42)). The device runner then ends the device with the transition `END` ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L154-L168)). `--print-channels` is for the channels that a device registers with [`RegisterChannelEndpoint()`](/api/fair/mq/Device/#RegisterChannelEndpoint), not for the configured channels.
:::note[`--version` and `--print-channels`]
The device runner sets the console log to `nolog` for these two options ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L42-L43)). It writes the output to the standard output, and then ends the device with the transition `END` ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L155-L170)). `--version` prints the lines `FairMQ version: <version>` and `User device version: <version>`. `--print-channels` prints one line `<name>:<min>:<max>` for each channel that the device registers with [`RegisterChannelEndpoint()`](/api/fair/mq/Device/#RegisterChannelEndpoint). It does not print the configured channels.
:::
## Value formats

View File

@@ -250,8 +250,7 @@ The type is part of the device and plugin API:
- The constructors [`Device(tools::Version)`](/api/fair/mq/Device/#Device-3) and
[`Device(ProgOptions&, tools::Version)`](/api/fair/mq/Device/#Device-4) set the version of your
device. The default is `{0, 0, 0}`. [`GetVersion()`](/api/fair/mq/Device/#GetVersion) returns it.
`DeviceRunner` logs it for `--version`. Today `--version` prints nothing: this is a known defect,
see the disabled tests in `test/device/_runner.cxx`.
`DeviceRunner` prints it for `--version`.
- [`Plugin::Version`](/api/fair/mq/Plugin/#Version) is an alias of `tools::Version`. See
[Write a plugin](/guides/write-plugin/).

View File

@@ -98,7 +98,7 @@ Thus `catch (std::exception const&)` catches all of them. The namespace is `fair
| [`ParserError`](/api/fair/mq/ParserError/) | [`JSONParser.h`](/source/fairmq/JSONParser.h/#L27) | `JSONParser()`, `SuboptParser()` or `PtreeParser()` gets an empty device id. The config plugin does not call the parser without an id. |
| [`Channel::ChannelConfigurationError`](/api/fair/mq/Channel/ChannelConfigurationError/) | [`Channel.h`](/source/fairmq/Channel.h/#L89) | `Channel::Validate()` finds an invalid type, an invalid method, a negative buffer or kernel size, or a negative `rateLogging`. |
| [`PollerError`](/api/fair/mq/PollerError/) | [`Poller.h`](/source/fairmq/Poller.h/#L37) | A poller gets an unknown channel name (in the constructor, `CheckInput()` or `CheckOutput()`), or the poll fails. |
| [`OngoingTransition`](/api/fair/mq/OngoingTransition/) | [`Device.h`](/source/fairmq/Device.h/#L46) | Not thrown in this version. |
| [`OngoingTransition`](/api/fair/mq/OngoingTransition/) | [`Device.h`](/source/fairmq/Device.h/#L47) | Not thrown in this version. |
| [`RuntimeError`](/api/fair/mq/RuntimeError/) | [`Error.h`](/source/fairmq/Error.h/#L23) | Not thrown in this version. Its constructor joins all arguments to one text. |
### Transports, sockets and messages

View File

@@ -69,7 +69,7 @@ Both files include `fairmq/runDevice.h`. This header defines the function `main(
<Snippet file="fairmq/runDevice.h" start="// to be implemented by the user to return" end="void addCustomOptions" />
The function `main()` makes a [`fair::mq::DeviceRunner`](/api/fair/mq/DeviceRunner/) and calls its [`Run()`](/api/fair/mq/DeviceRunner/#Run). `Run()` does these steps (see [DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L108-L194)):
The function `main()` makes a [`fair::mq::DeviceRunner`](/api/fair/mq/DeviceRunner/) and calls its [`Run()`](/api/fair/mq/DeviceRunner/#Run). `Run()` does these steps (see [DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L109-L196)):
1. It loads the built-in plugins `config` and `control`.
2. It calls `addCustomOptions()`. Your options show in `--help` in the group `Custom options`.

View File

@@ -189,7 +189,7 @@ ctest --test-dir fairmq_build --output-on-failure -R '^Example\.1-1\.'
</Steps>
:::note
The option `--version` of a device prints nothing in this version of FairMQ. This is a known defect. Read the version from `fairmq/Version.h`, or from the CMake variable `FairMQ_VERSION`.
The option `--version` of a device prints the version of FairMQ and the version of the device.
:::
The installation directory contains these files:

View File

@@ -115,7 +115,7 @@ fairmq-sink --id sink1 \
## Change the log at run time
`DeviceRunner` subscribes to the log properties ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L82-L101)).
`DeviceRunner` subscribes to the log properties ([DeviceRunner.cxx](/source/fairmq/DeviceRunner.cxx/#L83-L102)).
When a plugin or the device changes one of them, the new value applies at once:
| Property | Type | Effect of a change |
@@ -253,14 +253,9 @@ Rules for a sink:
- `fairmq-shmmonitor` has its own option `--severity` (default `info`), and no colors. See
[fairmq-shmmonitor](/tools/shmmonitor/).
- FairMQ prints the output of `--print-channels` and `--version` with `LOGV(info, verylow)`.
`LOGV(severity, verbosity)` is a log macro of FairLogger that also takes a verbosity.
:::caution[Known defect]
For `--print-channels` and `--version`, `DeviceRunner` sets the console severity to `nolog`. Thus both options print
nothing. See the disabled tests `DeviceRunner.DISABLED_PrintChannels` and `DeviceRunner.DISABLED_Version` in
`test/device/_runner.cxx`.
:::
- `--print-channels` and `--version` write their output to the standard output, not to the log. For these two
options, `DeviceRunner` sets the console severity to `nolog`. The tests `DeviceRunner.PrintChannels` and
`DeviceRunner.Version` in `test/device/_runner.cxx` check the output.
## Related pages

View File

@@ -33,6 +33,7 @@
#include <functional>
#include <memory> // unique_ptr
#include <mutex>
#include <ostream>
#include <stdexcept>
#include <string>
#include <unordered_map>
@@ -360,6 +361,15 @@ class Device
return ok;
}
/// @brief Print the registered channels in a machine-readable format
/// @param os output stream, one line per channel: <name>:<min sub-channels>:<max sub-channels>
void PrintRegisteredChannels(std::ostream& os) const
{
for (const auto& c : fChannelRegistry) {
os << c.first << ":" << c.second.first << ":" << c.second.second << "\n";
}
}
void PrintRegisteredChannels()
{
if (fChannelRegistry.empty()) {

View File

@@ -15,6 +15,7 @@
#include <fairlogger/Logger.h>
#include <exception>
#include <iostream>
using namespace std;
using namespace fair::mq;
@@ -154,15 +155,16 @@ auto DeviceRunner::Run() -> int
// Handle --print-channels
fDevice->RegisterChannelEndpoints();
if (fConfig.Count("print-channels")) {
fDevice->PrintRegisteredChannels();
// print to stdout: the console log severity is "nolog" for this option
fDevice->PrintRegisteredChannels(cout);
fDevice->ChangeStateOrThrow(fair::mq::Transition::End);
return 0;
}
// Handle --version
if (fConfig.Count("version")) {
LOGV(info, verylow) << "FairMQ version: " << FAIRMQ_GIT_VERSION;
LOGV(info, verylow) << "User device version: " << fDevice->GetVersion();
cout << "FairMQ version: " << FAIRMQ_GIT_VERSION << '\n'
<< "User device version: " << fDevice->GetVersion() << endl;
fDevice->ChangeStateOrThrow(fair::mq::Transition::End);
return 0;
}

View File

@@ -34,10 +34,7 @@ TEST(DeviceRunner, PrintOptions)
EXPECT_EQ(result.output.find("INITIALIZING DEVICE"), string::npos) << result.output;
}
// Known defect: DeviceRunner sets the console severity to "nolog" for
// --print-channels and --version, and then prints the output with LOGV(info).
// Both options print nothing. Enable these tests with the fix.
TEST(DeviceRunner, DISABLED_PrintChannels)
TEST(DeviceRunner, PrintChannels)
{
auto const results = RunDevices({{kIdleDevice, {}, "--print-channels"}});
auto const& result = results.at(0);
@@ -45,14 +42,14 @@ TEST(DeviceRunner, DISABLED_PrintChannels)
EXPECT_TRUE(ExitedWith(result, 0, "fan:2:4"));
}
TEST(DeviceRunner, DISABLED_Version)
TEST(DeviceRunner, Version)
{
auto const results = RunDevices({{kIdleDevice, {}, "--version"}});
EXPECT_TRUE(ExitedWith(results.at(0), 0, "FairMQ version: "));
EXPECT_TRUE(ExitedWith(results.at(0), 0, "User device version: "));
}
// PrintChannels and Version must not start the device, also while they print nothing.
// PrintChannels and Version must not start the device.
TEST(DeviceRunner, PrintChannelsAndVersionDoNotStartTheDevice)
{
for (auto const* option : {"--print-channels", "--version"}) {