2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
11 #include <babeltrace2/babeltrace.h>
13 #include "common/assert.h"
14 #include "cpp-common/vendor/fmt/core.h"
15 #include "cpp-common/vendor/nlohmann/json.hpp"
19 CondTrigger::CondTrigger(const Type type
, const std::string
& condId
,
20 const bt2s::optional
<std::string
>& nameSuffix
) noexcept
:
22 _mCondId
{fmt::format("{}:{}", type
== Type::PRE
? "pre" : "post", condId
)},
24 fmt::format("{}{}{}", condId
, nameSuffix
? "-" : "", nameSuffix
? nameSuffix
->data() : "")}
28 SimpleCondTrigger::SimpleCondTrigger(std::function
<void()> func
, const Type type
,
29 const std::string
& condId
,
30 const bt2s::optional
<std::string
>& nameSuffix
) :
31 CondTrigger
{type
, condId
, nameSuffix
},
32 _mFunc
{std::move(func
)}
38 void listCondTriggers(const CondTriggers condTriggers
) noexcept
40 auto condTriggerArray
= nlohmann::json::array();
42 for (const auto condTrigger
: condTriggers
) {
43 condTriggerArray
.push_back(nlohmann::json
{
44 {"cond-id", condTrigger
->condId()},
45 {"name", condTrigger
->name()},
49 fmt::println("{}", condTriggerArray
.dump());
54 void condMain(const int argc
, const char ** const argv
, const CondTriggers condTriggers
) noexcept
58 if (strcmp(argv
[1], "list") == 0) {
59 listCondTriggers(condTriggers
);
60 } else if (strcmp(argv
[1], "run") == 0) {
62 * It's expected that calling `*condTriggers[index]` below
63 * aborts (calls bt_common_abort()). In this testing context, we
64 * don't want any custom abortion command to run.
66 g_unsetenv("BABELTRACE_EXEC_ON_ABORT");
68 /* Call the trigger */
71 const auto index
= atoi(argv
[2]);
73 BT_ASSERT(index
>= 0 && index
< condTriggers
.size());
74 (*condTriggers
[index
])();