summaryrefslogtreecommitdiff
path: root/src/other_tools/just_mr/launch.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/other_tools/just_mr/launch.cpp')
-rw-r--r--src/other_tools/just_mr/launch.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/other_tools/just_mr/launch.cpp b/src/other_tools/just_mr/launch.cpp
index 7714711b..8744ffa3 100644
--- a/src/other_tools/just_mr/launch.cpp
+++ b/src/other_tools/just_mr/launch.cpp
@@ -24,6 +24,7 @@
#include <cerrno> // for errno
#include <cstdlib>
#include <cstring> // for strerror()
+#include <ctime>
#include <exception>
#include <filesystem>
#include <functional>
@@ -34,12 +35,16 @@
#include <unordered_map>
#include <vector>
+#include "fmt/chrono.h"
+#include "fmt/core.h"
#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/expression/configuration.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/buildtool/build_engine/expression/expression_ptr.hpp"
#include "src/buildtool/common/clidefaults.hpp"
#include "src/buildtool/common/user_structs.hpp"
+#include "src/buildtool/execution_api/common/ids.hpp"
+#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/storage/config.hpp"
@@ -50,8 +55,10 @@
#include "src/other_tools/just_mr/setup_utils.hpp"
#include "src/other_tools/just_mr/utils.hpp"
#include "src/utils/cpp/file_locking.hpp"
+#include "src/utils/cpp/path.hpp"
auto CallJust(std::optional<std::filesystem::path> const& config_file,
+ InvocationLogArguments const& invocation_log,
MultiRepoCommonArguments const& common_args,
MultiRepoSetupArguments const& setup_args,
MultiRepoJustSubCmdsArguments const& just_cmd_args,
@@ -253,6 +260,40 @@ auto CallJust(std::optional<std::filesystem::path> const& config_file,
cmd.emplace_back(subcmd_arg);
}
}
+ std::optional<std::filesystem::path> log_dir = std::nullopt;
+ auto invocation_time = std::time(nullptr);
+
+ // Check invocation logging
+ if (invocation_log.directory) {
+ auto dir = *invocation_log.directory;
+ if (invocation_log.project_id) {
+ if (not IsValidFileName(*invocation_log.project_id)) {
+ Logger::Log(LogLevel::Error,
+ "Invalid file name for poject id: {}",
+ nlohmann::json(*invocation_log.project_id).dump());
+ std::exit(kExitClargsError);
+ }
+ dir = dir / *invocation_log.project_id;
+ }
+ else {
+ dir = dir / "unknown";
+ }
+ std::string uuid = CreateUUID();
+ auto invocation_id = fmt::format(
+ "{:%Y-%m-%d-%H:%M}-{}", fmt::gmtime(invocation_time), uuid);
+ dir = dir / invocation_id;
+ if (FileSystemManager::CreateDirectoryExclusive(dir)) {
+ Logger::Log(
+ LogLevel::Info, "Invocation logged at {}", dir.string());
+ log_dir = dir;
+ }
+ else {
+ Logger::Log(LogLevel::Warning,
+ "Failed to create directory {} for invocation logging",
+ nlohmann::json(dir.string()).dump());
+ }
+ }
+
// add (remaining) args given by user as clargs
for (auto it = just_cmd_args.additional_just_args.begin() +
additional_args_offset;
@@ -261,6 +302,28 @@ auto CallJust(std::optional<std::filesystem::path> const& config_file,
cmd.emplace_back(*it);
}
+ // Write invocation metadata, if requested
+ if (log_dir and invocation_log.metadata) {
+ if (not IsValidFileName(*invocation_log.metadata)) {
+ Logger::Log(LogLevel::Error,
+ "Invlaid file name for metadata file: {}",
+ nlohmann::json(*invocation_log.metadata).dump());
+ std::exit(kExitClargsError);
+ }
+
+ auto meta = nlohmann::json::object();
+ meta["time"] = invocation_time;
+ meta["cmdline"] = cmd;
+ // "configuration" -- the blob-identifier of the multi-repo
+ // configuration
+ auto file_name = *log_dir / *invocation_log.metadata;
+ if (not FileSystemManager::WriteFile(meta.dump(2), file_name)) {
+ Logger::Log(LogLevel::Warning,
+ "Failed to write metadata file {}.",
+ nlohmann::json(file_name).dump());
+ }
+ }
+
Logger::Log(
LogLevel::Info, "Setup finished, exec {}", nlohmann::json(cmd).dump());