From b4a229e26f195ad4f31a6b2096f88c9523c008de Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 8 Nov 2020 06:47:47 +0100 Subject: [PATCH] util/logging: Add logging utility Also contains the function signature and name macros. --- CMakeLists.txt | 2 ++ source/util/util-logging.cpp | 44 +++++++++++++++++++++++++++ source/util/util-logging.hpp | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 source/util/util-logging.cpp create mode 100644 source/util/util-logging.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 08ba167c..9eef7903 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -987,6 +987,8 @@ list(APPEND PROJECT_PRIVATE_SOURCE "source/util/util-event.hpp" "source/util/util-library.cpp" "source/util/util-library.hpp" + "source/util/util-logging.cpp" + "source/util/util-logging.hpp" "source/util/util-threadpool.cpp" "source/util/util-threadpool.hpp" "source/gfx/gfx-source-texture.hpp" diff --git a/source/util/util-logging.cpp b/source/util/util-logging.cpp new file mode 100644 index 00000000..3a1b0942 --- /dev/null +++ b/source/util/util-logging.cpp @@ -0,0 +1,44 @@ +/* + * Modern effects for a modern Streamer + * Copyright (C) 2020 Michael Fabian Dirks + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#include "util-logging.hpp" +#include "common.hpp" +#include + +void util::logging::log(level lvl, const char* format, ...) +{ + const static std::map level_map = { + {level::LEVEL_DEBUG, LOG_DEBUG}, + {level::LEVEL_INFO, LOG_INFO}, + {level::LEVEL_WARN, LOG_WARNING}, + {level::LEVEL_ERROR, LOG_ERROR}, + }; + thread_local static std::vector buffer; + + va_list vargs; + va_start(vargs, format); + + int32_t ret = vsnprintf(buffer.data(), buffer.size(), format, vargs); + buffer.resize(ret + 1); + ret = vsnprintf(buffer.data(), buffer.size(), format, vargs); + + va_end(vargs); + + blog(level_map.at(lvl), "[StreamFX] %s", buffer.data()); +} diff --git a/source/util/util-logging.hpp b/source/util/util-logging.hpp new file mode 100644 index 00000000..be1c20dd --- /dev/null +++ b/source/util/util-logging.hpp @@ -0,0 +1,58 @@ +/* + * Modern effects for a modern Streamer + * Copyright (C) 2020 Michael Fabian Dirks + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#pragma once +#include +#include + +#define P_LOG(...) util::logging::log(__VA_ARGS__); +#define P_LOG_ERROR(...) P_LOG(util::logging::level::LEVEL_ERROR, __VA_ARGS__) +#define P_LOG_WARN(...) P_LOG(util::logging::level::LEVEL_WARN, __VA_ARGS__) +#define P_LOG_INFO(...) P_LOG(util::logging::level::LEVEL_INFO, __VA_ARGS__) +#ifdef _DEBUG +#define P_LOG_DEBUG(...) P_LOG(util::logging::level::LEVEL_DEBUG, __VA_ARGS__) +#else +#define P_LOG_DEBUG(...) +#endif + +// Function/Class/Struct Name +#ifdef _MSC_VER +// Microsoft Visual Studio +#define __FUNCTION_SIG__ __FUNCSIG__ +#define __FUNCTION_NAME__ __func__ +#elif defined(__GNUC__) || defined(__MINGW32__) +// GCC and MinGW +#define __FUNCTION_SIG__ __PRETTY_FUNCTION__ +#define __FUNCTION_NAME__ __func__ +#else +// Any other compiler +#define __FUNCTION_SIG__ __func__ +#define __FUNCTION_NAME__ __func__ +#endif + +namespace util::logging { + enum class level { + LEVEL_DEBUG, // Debug information, which is not necessary to know at runtime. + LEVEL_INFO, // Runtime information, which may or may not be needed for support. + LEVEL_WARN, // Warnings, which should be respected and fixed. + LEVEL_ERROR, // Errors that must be fixed. + }; + + void log(level lvl, const char* format, ...); +} // namespace util::logging