Commit | Line | Data |
---|---|---|
5d7e8359 PP |
1 | /* |
2 | * SPDX-License-Identifier: GPL-2.0-only | |
3 | * | |
4 | * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com> | |
5 | */ | |
6 | ||
ba33b664 | 7 | #include <glib.h> |
c802cacb | 8 | #include <stdlib.h> |
5d7e8359 | 9 | #include <string.h> |
c802cacb | 10 | |
5d7e8359 | 11 | #include <babeltrace2/babeltrace.h> |
5d7e8359 PP |
12 | |
13 | #include "common/assert.h" | |
6e3150f4 | 14 | #include "cpp-common/vendor/fmt/core.h" |
78c888d1 | 15 | #include "cpp-common/vendor/nlohmann/json.hpp" |
c802cacb | 16 | |
c802cacb | 17 | #include "utils.hpp" |
5d7e8359 | 18 | |
5d7e57e8 SM |
19 | CondTrigger::CondTrigger(const Type type, const std::string& condId, |
20 | const bt2s::optional<std::string>& nameSuffix) noexcept : | |
21 | _mType {type}, | |
22 | _mCondId {fmt::format("{}:{}", type == Type::PRE ? "pre" : "post", condId)}, | |
23 | _mName { | |
24 | fmt::format("{}{}{}", condId, nameSuffix ? "-" : "", nameSuffix ? nameSuffix->data() : "")} | |
5d7e8359 | 25 | { |
5d7e8359 PP |
26 | } |
27 | ||
5d7e57e8 SM |
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)} | |
5d7e8359 | 33 | { |
5d7e57e8 | 34 | } |
5d7e8359 | 35 | |
5d7e57e8 | 36 | namespace { |
5d7e8359 | 37 | |
5d7e57e8 SM |
38 | void listCondTriggers(const CondTriggers condTriggers) noexcept |
39 | { | |
40 | auto condTriggerArray = nlohmann::json::array(); | |
5d7e8359 | 41 | |
5d7e57e8 SM |
42 | for (const auto condTrigger : condTriggers) { |
43 | condTriggerArray.push_back(nlohmann::json { | |
44 | {"cond-id", condTrigger->condId()}, | |
45 | {"name", condTrigger->name()}, | |
46 | }); | |
486428e8 | 47 | } |
5d7e8359 | 48 | |
5d7e57e8 | 49 | fmt::println("{}", condTriggerArray.dump()); |
5d7e8359 PP |
50 | } |
51 | ||
6e3150f4 SM |
52 | } /* namespace */ |
53 | ||
5d7e57e8 | 54 | void condMain(const int argc, const char ** const argv, const CondTriggers condTriggers) noexcept |
5d7e8359 | 55 | { |
486428e8 SM |
56 | BT_ASSERT(argc >= 2); |
57 | ||
58 | if (strcmp(argv[1], "list") == 0) { | |
5d7e57e8 | 59 | listCondTriggers(condTriggers); |
486428e8 | 60 | } else if (strcmp(argv[1], "run") == 0) { |
ba33b664 PP |
61 | /* |
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. | |
65 | */ | |
66 | g_unsetenv("BABELTRACE_EXEC_ON_ABORT"); | |
67 | ||
68 | /* Call the trigger */ | |
486428e8 | 69 | BT_ASSERT(argc >= 3); |
5d7e57e8 SM |
70 | |
71 | const auto index = atoi(argv[2]); | |
72 | ||
73 | BT_ASSERT(index >= 0 && index < condTriggers.size()); | |
74 | (*condTriggers[index])(); | |
486428e8 | 75 | } |
5d7e8359 | 76 | } |