-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamin Huth
committed
Dec 4, 2024
1 parent
797c3c3
commit 470882f
Showing
8 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
Plugins/Json/include/Acts/Plugins/Json/StructuredLoggerJson.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// This file is part of the ACTS project. | ||
// | ||
// Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Utilities/Logger.hpp" | ||
|
||
#include <filesystem> | ||
#include <mutex> | ||
|
||
#include <nlohmann/json.hpp> | ||
|
||
namespace Acts { | ||
|
||
class StructuredLoggerJson : public StructuredLoggerBase { | ||
public: | ||
StructuredLoggerJson(std::filesystem::path outputFile); | ||
~StructuredLoggerJson(); | ||
|
||
void log(const Logger &logger, std::string_view msg) override; | ||
void log(std::string_view tag, std::string_view name, | ||
const Eigen::MatrixXd &m) override; | ||
void log(std::string_view tag, std::string_view name, | ||
const std::span<double> &s) override; | ||
|
||
private: | ||
std::mutex m_mutex; | ||
nlohmann::json m_json; | ||
std::filesystem::path m_path; | ||
}; | ||
|
||
} // namespace Acts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// This file is part of the ACTS project. | ||
// | ||
// Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include <Acts/Plugins/Json/StructuredLoggerJson.hpp> | ||
|
||
#include <fstream> | ||
|
||
namespace Acts { | ||
|
||
StructuredLoggerJson::StructuredLoggerJson(std::filesystem::path outputFile) | ||
: m_path(outputFile) { | ||
m_json = nlohmann::json::array(); | ||
} | ||
|
||
StructuredLoggerJson::~StructuredLoggerJson() { | ||
std::cout << "Dump json to " << m_path << std::endl; | ||
std::ofstream os(m_path); | ||
os << m_json.dump(2); | ||
} | ||
|
||
void StructuredLoggerJson::log(const Logger &logger, std::string_view msg) { | ||
nlohmann::json j; | ||
j["tag"] = logger.name(); | ||
j["level"] = static_cast<int>(logger.level()); | ||
j["type"] = "message"; | ||
j["key"] = ""; | ||
j["val"] = msg; | ||
|
||
std::lock_guard<std::mutex> guard(m_mutex); | ||
m_json.push_back(j); | ||
std::cout << "log msg to json" << std::endl; | ||
} | ||
|
||
void StructuredLoggerJson::log(std::string_view tag, std::string_view name, | ||
const Eigen::MatrixXd &m) { | ||
nlohmann::json j; | ||
j["tag"] = tag; | ||
j["level"] = "STRUCTURED"; | ||
j["key"] = name; | ||
|
||
if (m.rows() == 1) { | ||
j["type"] = "vector"; | ||
j["val"] = m.row(0); | ||
} else { | ||
j["type"] = "matrix"; | ||
j["val"] = nlohmann::json::array(); | ||
for (const auto &row : m.rowwise()) { | ||
j["val"].push_back(row); | ||
} | ||
} | ||
|
||
std::lock_guard<std::mutex> guard(m_mutex); | ||
m_json.push_back(j); | ||
} | ||
|
||
void StructuredLoggerJson::log(std::string_view tag, std::string_view name, | ||
const std::span<double> &s) { | ||
nlohmann::json j; | ||
j["tag"] = tag; | ||
j["level"] = "STRUCTURED"; | ||
j["type"] = "vector"; | ||
j["key"] = name; | ||
j["val"] = s; | ||
|
||
std::lock_guard<std::mutex> guard(m_mutex); | ||
m_json.push_back(j); | ||
} | ||
|
||
} // namespace Acts |