errorlogtopic send log messages as json object instead of a fixed string (#3639)

This commit is contained in:
Andreas Engberg
2025-05-14 12:28:46 +02:00
committed by GitHub
parent e33ad09632
commit ee56eb62e8
5 changed files with 176 additions and 43 deletions

View File

@@ -30,6 +30,7 @@ set(HEADER_FILES
include/connection.h
include/connectionpool.h
include/jsonconverters.h
include/logging/notificationlog.h
include/serverinterface.h
include/topics/actionkeybindtopic.h
include/topics/authorizationtopic.h
@@ -61,6 +62,7 @@ set(SOURCE_FILES
src/connection.cpp
src/connectionpool.cpp
src/jsonconverters.cpp
src/logging/notificationlog.cpp
src/serverinterface.cpp
src/topics/actionkeybindtopic.cpp
src/topics/authorizationtopic.cpp

View File

@@ -0,0 +1,76 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2025 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_SERVER___NOTIFICATIONLOG___H__
#define __OPENSPACE_MODULE_SERVER___NOTIFICATIONLOG___H__
#include <ghoul/logging/log.h>
#include <ghoul/misc/profiling.h>
#include <functional>
namespace openspace {
/**
* A concrete subclass of Log that passes logs to the provided callback function. The
* callback is specified using `std::function`. Trying to log messages when the callback
* object has been deleted results in undefined behavior.
*/
class NotificationLog : public ghoul::logging::Log {
public:
/// The type of function that is used as a callback in this log
using CallbackFunction = std::function<void(
std::string_view timeString,
std::string_view dateString,
std::string_view category,
ghoul::logging::LogLevel logLevel,
std::string_view message)>;
/**
* Constructor that calls Log constructor.
*
* \param callbackFunction The callback function that is called for each log message
* \param minimumLogLevel The minimum log level that this logger will accept
*/
NotificationLog(CallbackFunction callbackFunction,
ghoul::logging::LogLevel minimumLogLevel = ghoul::logging::LogLevel::Warning);
/**
* Method that logs a message with a given level and category to the console.
*
* \param level The log level with which the message shall be logged
* \param category The category of this message.
* \param message The message body of the log message
*/
void log(ghoul::logging::LogLevel level, std::string_view category,
std::string_view message) override;
private:
CallbackFunction _callbackFunction;
TracyLockable(std::mutex, _mutex);
};
} // namespace ghoul::logging
#endif // __OPENSPACE_MODULE_SERVER___NOTIFICATIONLOG___H__

View File

@@ -39,6 +39,12 @@ public:
bool isDone() const override;
private:
/**
* Creates a log object and register it to the `LogManager`, does nothing if an active
* log already exists
*/
void createLog(ghoul::logging::LogLevel logLevel);
bool _isSubscribedTo = false;
// Non owning but we remove the log from LogManager on destruction
ghoul::logging::Log* _log = nullptr;

View File

@@ -0,0 +1,52 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2025 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/server/include/logging/notificationlog.h>
namespace openspace {
NotificationLog::NotificationLog(CallbackFunction callbackFunction,
ghoul::logging::LogLevel minimumLogLevel)
: ghoul::logging::Log(
TimeStamping::Yes,
DateStamping::Yes,
CategoryStamping::Yes,
LogLevelStamping::Yes,
minimumLogLevel
)
, _callbackFunction(std::move(callbackFunction))
{}
void NotificationLog::log(ghoul::logging::LogLevel level, std::string_view category,
std::string_view message)
{
ZoneScoped;
const std::lock_guard lock(_mutex);
const std::string timeStamp = timeString();
const std::string dateStamp = dateString();
_callbackFunction(timeStamp, dateStamp, category, level, message);
}
} // namespace openspace

View File

@@ -24,18 +24,15 @@
#include <modules/server/include/topics/errorlogtopic.h>
#include <ghoul/logging/callbacklog.h>
#include <modules/server/include/logging/notificationlog.h>
#include <ghoul/logging/logmanager.h>
namespace {
constexpr std::string_view StartSubscription = "start_subscription";
constexpr std::string_view StopSubscription = "stop_subscription";
constexpr std::string_view UpdateLogLevel = "update_logLevel";
constexpr std::string_view SettingsKey = "settings";
constexpr std::string_view TimeStampingKey = "timeStamping";
constexpr std::string_view DateStampingKey = "dateStamping";
constexpr std::string_view CategoryStampingKey = "categoryStamping";
constexpr std::string_view LogLevelStampingKey = "logLevelStamping";
constexpr std::string_view LogLevelKey = "logLevel";
} // namespace
@@ -53,34 +50,12 @@ void ErrorLogTopic::handleJson(const nlohmann::json& json) {
if (event == StartSubscription) {
// Default settings for logging
auto timeStamping = ghoul::logging::Log::TimeStamping::Yes;
auto dateStamping = ghoul::logging::Log::DateStamping::Yes;
auto categoryStamping = ghoul::logging::Log::CategoryStamping::Yes;
auto logLevelStamping = ghoul::logging::Log::LogLevelStamping::Yes;
auto logLevel = ghoul::logging::LogLevel::AllLogging;
ghoul::logging::LogLevel logLevel = ghoul::logging::LogLevel::AllLogging;
// Check if we got log settings on subscription
auto settings = json.find(SettingsKey);
if (settings != json.end()) {
if (auto ts = settings->find(TimeStampingKey); ts != settings->end()) {
timeStamping = ghoul::logging::Log::TimeStamping(ts->get<bool>()).value;
}
if (auto ds = settings->find(DateStampingKey); ds != settings->end()) {
dateStamping = ghoul::logging::Log::DateStamping(ds->get<bool>()).value;
}
if (auto cs = settings->find(CategoryStampingKey); cs != settings->end()) {
categoryStamping = ghoul::logging::Log::CategoryStamping(
cs->get<bool>()).value;
}
if(auto lls = settings->find(LogLevelStampingKey); lls != settings->end()){
logLevelStamping = ghoul::logging::Log::LogLevelStamping(
lls->get<bool>()).value;
}
if (auto ls = settings->find(LogLevelKey); ls != settings->end()) {
std::string level = ls->get<std::string>();
logLevel = ghoul::from_string<ghoul::logging::LogLevel>(level);
@@ -88,21 +63,7 @@ void ErrorLogTopic::handleJson(const nlohmann::json& json) {
}
auto onLogging = [this](std::string message) {
_connection->sendJson(wrappedPayload(std::move(message)));
};
auto log = std::make_unique<ghoul::logging::CallbackLog>(
onLogging,
timeStamping,
dateStamping,
categoryStamping,
logLevelStamping,
logLevel
);
_log = log.get();
ghoul::logging::LogManager::ref().addLog(std::move(log));
createLog(logLevel);
_isSubscribedTo = true;
}
@@ -112,6 +73,42 @@ void ErrorLogTopic::handleJson(const nlohmann::json& json) {
ghoul::logging::LogManager::ref().removeLog(_log);
_log = nullptr;
}
if (event == UpdateLogLevel) {
ghoul::logging::LogManager::ref().removeLog(_log);
_log = nullptr;
std::string level = json.at(LogLevelKey).get<std::string>();
auto logLevel = ghoul::from_string<ghoul::logging::LogLevel>(level);
createLog(logLevel);
}
}
void ErrorLogTopic::createLog(ghoul::logging::LogLevel logLevel) {
if (_log) {
return;
}
auto onLogging = [this](std::string_view timeStamp, std::string_view dateStamp,
std::string_view category, ghoul::logging::LogLevel level,
std::string_view message)
{
nlohmann::json payload = {
{ "timeStamp", timeStamp },
{ "dateStamp", dateStamp },
{ "category", category },
{ "level", level },
{ "message", message }
};
_connection->sendJson(wrappedPayload(std::move(payload)));
};
auto log = std::make_unique<NotificationLog>(
onLogging,
logLevel
);
_log = log.get();
ghoul::logging::LogManager::ref().addLog(std::move(log));
}
bool ErrorLogTopic::isDone() const {