From bb6995725733ed31c925e057ad72fa7ed087b8a0 Mon Sep 17 00:00:00 2001 From: ynput Date: Thu, 12 Dec 2024 15:31:42 +0100 Subject: [PATCH] updates to the constructor to handle logging input better --- src/AyonCppApi/AyonCppApi.cpp | 35 ++++++++++++++++++++++++----------- src/AyonCppApi/AyonCppApi.h | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/AyonCppApi/AyonCppApi.cpp b/src/AyonCppApi/AyonCppApi.cpp index 231be7f..05835d4 100644 --- a/src/AyonCppApi/AyonCppApi.cpp +++ b/src/AyonCppApi/AyonCppApi.cpp @@ -33,9 +33,11 @@ #include "perfPrinter.h" // TODO implement the better Crash hanlder +// TODO this implementation dose not check for empty or invalid values. (if sideID is empty we get an http::500 while +// the error is not at the server) backward::StackTrace st; -AyonApi::AyonApi(const std::string &logFilePos, +AyonApi::AyonApi(const std::optional &logFilePos, const std::string &authKey, const std::string &serverUrl, const std::string &ayonProjectName, @@ -49,18 +51,29 @@ AyonApi::AyonApi(const std::string &logFilePos, PerfTimer("AyonApi::AyonApi"); // ----------- Init m_Logger - std::filesystem::path logFileName = "logFile.json"; - std::filesystem::path basePath = logFilePos; - std::filesystem::path logFilePath = std::filesystem::absolute(basePath) / logFileName; - if (std::filesystem::exists(logFilePath)) { - logFilePath = std::filesystem::canonical(logFilePath); - } - else { - std::filesystem::create_directories(logFilePath.parent_path()); - } + std::filesystem::path logPath; + if (logFilePos.has_value()) { + std::filesystem::path inPath(logFilePos.value()); + + if (inPath.is_relative()) { + logPath = std::filesystem::weakly_canonical(inPath); + } + if (!inPath.has_parent_path()) { + // if the input path is just an filename we will just throw it into tmp + logPath = std::filesystem::temp_directory_path() / inPath; + } + // we allways want the data to be a json, so we just enforce it. + logPath.replace_extension(".json"); - m_Log = std::make_shared(AyonLogger::getInstance(logFilePath.string())); + if (std::filesystem::exists(logPath)) { + logPath = std::filesystem::canonical(logPath); + } + else { + std::filesystem::create_directories(logPath.parent_path()); + } + } + m_Log = std::make_shared(AyonLogger::getInstance(logPath.string())); m_Log->LogLevlWarn(); m_Log->info(m_Log->key("AyonApi"), "Init AyonServer httplib::Client"); diff --git a/src/AyonCppApi/AyonCppApi.h b/src/AyonCppApi/AyonCppApi.h index 37c2e70..e8f6340 100644 --- a/src/AyonCppApi/AyonCppApi.h +++ b/src/AyonCppApi/AyonCppApi.h @@ -25,7 +25,7 @@ class AyonApi { /** * @brief constructor */ - AyonApi(const std::string &logFilePos, + AyonApi(const std::optional &logFilePos, const std::string &authKey, const std::string &serverUrl, const std::string &ayonProjectName,