obs-StreamFX/source/util/util-logging.cpp
William Pettersson 3eacc47085 util/logging: Don't reuse vargs (#632)
As vargs may be modified by some functions, we should not reuse it and instead create a copy of it. This fixes a segfault on logging calls happening with GCC, and potentially may fix other compilers and platforms as well.
2023-04-05 18:51:14 +02:00

47 lines
1.5 KiB
C++

/*
* 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 <stdarg.h>
void streamfx::util::logging::log(level lvl, const char* format, ...)
{
const static std::map<level, int32_t> 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<char> buffer;
va_list vargs;
va_start(vargs, format);
va_list vargs_copy;
va_copy(vargs_copy, vargs);
int32_t ret = vsnprintf(buffer.data(), buffer.size(), format, vargs);
buffer.resize(ret + 1);
ret = vsnprintf(buffer.data(), buffer.size(), format, vargs_copy);
va_end(vargs);
va_end(vargs_copy);
blog(level_map.at(lvl), "[StreamFX] %s", buffer.data());
}