From 2c033d6e8382593a3060c125ddd99d65112d4583 Mon Sep 17 00:00:00 2001 From: Stephen G Date: Fri, 19 Apr 2024 17:59:11 -0700 Subject: [PATCH] Some logging refactoring, plus some work on the subscript-out-of-range crash that has been occurring in GFXDeleteTexture(int handle) --- engine/src/audio/test.cpp | 6 +- engine/src/cmd/basecomputer.cpp | 8 ++- engine/src/cmd/script/director.cpp | 5 +- engine/src/exit_unit_tests.cpp | 6 +- engine/src/gamemenu.cpp | 8 ++- engine/src/gfx/aux_texture.cpp | 16 ++++-- engine/src/gfx/particle.cpp | 12 ++-- engine/src/gfx/sprite.h | 12 ++-- engine/src/gldrv/gl_texture.cpp | 28 ++++++---- engine/src/in_kb.h | 10 +++- engine/src/main.cpp | 12 ++-- engine/src/python/init.cpp | 7 +-- engine/src/savegame.cpp | 5 +- engine/src/star_system.cpp | 6 +- engine/src/vega_py_run.cpp | 3 +- engine/src/vs_logging.cpp | 90 ++++++++++++++++++++++-------- engine/src/vs_logging.h | 51 ++++++++++++----- 17 files changed, 182 insertions(+), 103 deletions(-) diff --git a/engine/src/audio/test.cpp b/engine/src/audio/test.cpp index cc977cd719..09e6be4eb5 100644 --- a/engine/src/audio/test.cpp +++ b/engine/src/audio/test.cpp @@ -1,7 +1,7 @@ /* * test.cpp * - * Copyright (C) 2001-2022 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, * and other Vega Strike contributors * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -15,7 +15,7 @@ * * Vega Strike 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 + * 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 @@ -665,7 +665,7 @@ void closeRenderer() { } void VSExit(int code) { - VegaStrikeLogging::vega_logger()->FlushLogs(); + VegaStrikeLogging::VegaStrikeLogger::instance().FlushLogsProgramExiting(); exit(code); } diff --git a/engine/src/cmd/basecomputer.cpp b/engine/src/cmd/basecomputer.cpp index 63f243cf58..84165c80e7 100644 --- a/engine/src/cmd/basecomputer.cpp +++ b/engine/src/cmd/basecomputer.cpp @@ -1,5 +1,7 @@ /* - * Copyright (C) 2001-2022 Daniel Horn, Mike Byron, pyramid3d, Stephen G. Tuggy, + * basecomputer.cpp + * + * Copyright (C) 2001-2024 Daniel Horn, Mike Byron, pyramid3d, Stephen G. Tuggy, * and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -13,7 +15,7 @@ * * Vega Strike 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 + * 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 @@ -4273,7 +4275,7 @@ void trackPrice(int whichplayer, const Cargo &item, float price, const string &s } } - VegaStrikeLogging::vega_logger()->FlushLogs(); + VegaStrikeLogging::VegaStrikeLogger::instance().FlushLogs(); highest.clear(); highest.resize(recordedHighestPrices.size()); diff --git a/engine/src/cmd/script/director.cpp b/engine/src/cmd/script/director.cpp index f6112ed566..c6a7fad89b 100644 --- a/engine/src/cmd/script/director.cpp +++ b/engine/src/cmd/script/director.cpp @@ -1,7 +1,7 @@ /* * director.cpp * - * Copyright (C) 2001-2023 Daniel Horn, Alexander Rawass, pyramid3d, + * Copyright (C) 2001-2024 Daniel Horn, Alexander Rawass, pyramid3d, * Stephen G. Tuggy, and other Vega Strike contributors * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -10,7 +10,7 @@ * * Vega Strike 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 + * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Vega Strike is distributed in the hope that it will be useful, @@ -94,7 +94,6 @@ void Mission::DirectorLoop() { VS_LOG_AND_FLUSH(fatal, "void Mission::DirectorLoop(): Python error occurred"); PyErr_Print(); PyErr_Clear(); - VegaStrikeLogging::vega_logger()->FlushLogs(); } throw; } diff --git a/engine/src/exit_unit_tests.cpp b/engine/src/exit_unit_tests.cpp index 2636b5cc23..02ce37a4aa 100644 --- a/engine/src/exit_unit_tests.cpp +++ b/engine/src/exit_unit_tests.cpp @@ -3,7 +3,7 @@ * * Implementation of VSExit for unit tests * - * Copyright (C) 2001-2022 Daniel Horn, Stephen G. Tuggy, + * Copyright (C) 2001-2024 Daniel Horn, Stephen G. Tuggy, * and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -17,7 +17,7 @@ * * Vega Strike 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 + * 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 @@ -29,6 +29,6 @@ #include "vs_logging.h" void VSExit(int code) { - VegaStrikeLogging::vega_logger()->FlushLogs(); + VegaStrikeLogging::VegaStrikeLogger::instance().FlushLogsProgramExiting(); exit(code); } diff --git a/engine/src/gamemenu.cpp b/engine/src/gamemenu.cpp index 7e43380187..ab983edaec 100644 --- a/engine/src/gamemenu.cpp +++ b/engine/src/gamemenu.cpp @@ -1,5 +1,7 @@ /* - * Copyright (C) 2001-2022 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * gamemenu.cpp + * + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, * and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -13,7 +15,7 @@ * * Vega Strike 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 + * 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 @@ -459,7 +461,7 @@ bool GameMenu::processMainMenuButton(const EventCommandId &command, Control *con } bool GameMenu::processExitGameButton(const EventCommandId &command, Control *control) { - VegaStrikeLogging::vega_logger()->FlushLogs(); + VegaStrikeLogging::VegaStrikeLogger::instance().FlushLogsProgramExiting(); winsys_exit(0); return true; } diff --git a/engine/src/gfx/aux_texture.cpp b/engine/src/gfx/aux_texture.cpp index b53da486cc..be788cd04d 100644 --- a/engine/src/gfx/aux_texture.cpp +++ b/engine/src/gfx/aux_texture.cpp @@ -1,10 +1,8 @@ -/** +/* * aux_texture.cpp * - * Copyright (C) 2001-2002 Daniel Horn - * Copyright (C) 2020 pyramid3d, Stephen G. Tuggy, Roy Falk, - * and other Vega Strike contributors - * Copyright (C) 2021-2022 Stephen G. Tuggy + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Roy Falk, and other Vega Strike contributors * * https://github.com/vegastrike/Vega-Strike-Engine-Source * @@ -558,7 +556,13 @@ Texture::~Texture() { } else { original->refcount--; if (original->refcount == 0) { - delete original; + if (original == this) { + VS_LOG(debug, "In Texture::~Texture() in aux_texture.cpp: original->refcount == 0, but original == this, so not deleting again. (We are already in the destructor.)"); + } else { + VS_LOG(trace, "In Texture::~Texture() in aux_texture.cpp: original->refcount == 0, and original != this, so deleting original"); + delete original; + original = nullptr; + } } } } diff --git a/engine/src/gfx/particle.cpp b/engine/src/gfx/particle.cpp index 019f011fce..7760c7ac5b 100644 --- a/engine/src/gfx/particle.cpp +++ b/engine/src/gfx/particle.cpp @@ -1,7 +1,7 @@ /* * particle.cpp * - * Copyright (c) 2001-2022 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Copyright (c) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, * and other Vega Strike Contributors * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -10,16 +10,16 @@ * * Vega Strike 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 + * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Vega Strike 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 + * 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 Vega Strike. If not, see . + * along with Vega Strike. If not, see . */ @@ -311,7 +311,7 @@ void ParticleTrail::DrawAndUpdate() { } particleVert.clear(); - particleVert.reserve(particles.size() * (3 + 4)); + particleVert.reserve(particles.size() * (static_cast(3) + 4)); std::back_insert_iterator > v(particleVert); for (size_t i = 0; i < nparticles; ++i) { const QVector &loc = particles[i].location; @@ -395,7 +395,7 @@ void ParticleTrail::DrawAndUpdate() { } particleVert.clear(); - particleVert.reserve(nparticles * vertsPerParticle * (3 + 4 + 2)); + particleVert.reserve(nparticles * vertsPerParticle * (static_cast(3 + 4) + 2)); std::back_insert_iterator > v(particleVert); for (size_t i = 0; i < nparticles; ++i) { SetQuadVertex(particles[i].location, particles[i].color, particles[i].size, pgrow, ptrans, v, kCameraPosition); diff --git a/engine/src/gfx/sprite.h b/engine/src/gfx/sprite.h index 150ec7b166..bf8db8dee1 100644 --- a/engine/src/gfx/sprite.h +++ b/engine/src/gfx/sprite.h @@ -1,5 +1,7 @@ -/* - * Copyright (C) 2001-2023 Daniel Horn, Alan Shieh, klaussfreire, +/* + * sprite.h + * + * Copyright (C) 2001-2024 Daniel Horn, Alan Shieh, klaussfreire, * surfdargent, dan_w, pyramid3d, Stephen G. Tuggy, Benjamen R. Meyer, * and other Vega Strike contributors. * @@ -14,12 +16,14 @@ * * Vega Strike 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 + * 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 Vega Strike. If not, see . */ + + #ifndef VEGA_STRIKE_ENGINE_GFX_SPRITE_H #define VEGA_STRIKE_ENGINE_GFX_SPRITE_H @@ -47,7 +51,7 @@ class VSSprite { bool isAnimation; //For private use only - VSSprite() : surface(0) { + VSSprite() : surface(nullptr), isAnimation(false), rotation(0.0F), maxs(0.0F), maxt(0.0F), heighto2(0.0F), widtho2(0.0F), xcenter(0.0F), ycenter(0.0F) { } public: diff --git a/engine/src/gldrv/gl_texture.cpp b/engine/src/gldrv/gl_texture.cpp index fec050b630..abd11fab78 100644 --- a/engine/src/gldrv/gl_texture.cpp +++ b/engine/src/gldrv/gl_texture.cpp @@ -1,7 +1,7 @@ /* * gl_texture.cpp * - * Copyright (C) 2001-2023 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, * and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -15,7 +15,7 @@ * * Vega Strike 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 + * 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 @@ -463,8 +463,8 @@ static void DownSampleTexture(unsigned char **newbuf, amask >>= 1, tshift++, hshift++; } int tmask = (1 << tshift) - 1; - *newbuf = (unsigned char *) malloc(newheight * newwidth * pixsize * sizeof(unsigned char)); - unsigned int temp[32 * 4]; + *newbuf = (unsigned char*)malloc(static_cast(newheight) * newwidth * pixsize * sizeof(unsigned char)); + unsigned int temp[32 * 4]{}; unsigned char *orow = (*newbuf); const unsigned char *irow = oldbuf; for (i = 0; i < newheight; i++, orow += ostride, irow += rowstride) { @@ -510,7 +510,7 @@ static void DownSampleTexture(unsigned char **newbuf, //Specific purpose downsampler: 2x2 averaging //a) Very little overhead //b) Very common case (mipmap generation) - *newbuf = (unsigned char *) malloc(newheight * newwidth * pixsize * sizeof(unsigned char)); + *newbuf = (unsigned char*)malloc(static_cast(newheight) * newwidth * pixsize * sizeof(unsigned char)); unsigned char *orow = (*newbuf); int ostride = newwidth * pixsize; int istride = width * pixsize; @@ -1093,11 +1093,18 @@ GFXBOOL /*GFXDRVAPI*/ GFXTransferTexture(unsigned char *buffer, } void /*GFXDRVAPI*/ GFXDeleteTexture(int handle) { + if (handle < 0) { + VS_LOG(error, (boost::format("GFXDeleteTexture(int handle) called with invalid, negative handle value %1%") % handle)); + return; + } else if (handle >= textures.size()) { + VS_LOG(error, (boost::format("GFXDeleteTexture(int handle) called with invalid handle value %1%, which is greater than textures.size(): %2%") % handle % textures.size())); + return; + } if (textures[handle].alive) { - glDeleteTextures(1, &textures[handle].name); - for (size_t i = 0; i < sizeof(activetexture) / sizeof(int); ++i) { - if (activetexture[i] == handle) { - activetexture[i] = -1; + glDeleteTextures(1, &textures[handle].name); // Is this correct? - SGT 2024-04-18 + for (int & each_texture : activetexture) { + if (each_texture == handle) { + each_texture = -1; } } } @@ -1110,7 +1117,7 @@ void /*GFXDRVAPI*/ GFXDeleteTexture(int handle) { void GFXInitTextureManager() { for (size_t handle = 0; handle < textures.size(); ++handle) { - textures[handle].palette = NULL; + textures[handle].palette = nullptr; textures[handle].width = textures[handle].height = textures[handle].iwidth = textures[handle].iheight = 0; textures[handle].texturestage = 0; textures[handle].name = 0; @@ -1123,6 +1130,7 @@ void GFXInitTextureManager() { } void GFXDestroyAllTextures() { + // TODO: There's got to be a more efficient way to do this -- SGT 2024-04-18 for (size_t handle = 0; handle < textures.size(); handle++) { GFXDeleteTexture(handle); } diff --git a/engine/src/in_kb.h b/engine/src/in_kb.h index 5899fbb3e6..4c4c419eff 100644 --- a/engine/src/in_kb.h +++ b/engine/src/in_kb.h @@ -1,5 +1,7 @@ /* - * Copyright (C) 2001-2023 Daniel Horn, pyramid3d, Stephen G. Tuggy, Benjamen R. Meyer, + * in_kb.h + * + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, Benjamen R. Meyer, * and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -13,12 +15,14 @@ * * Vega Strike 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 + * 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 Vega Strike. If not, see . */ + + #ifndef VEGA_STRIKE_ENGINE_IN_KB_H #define VEGA_STRIKE_ENGINE_IN_KB_H @@ -42,7 +46,7 @@ static void DefaultKBHandler(const KBData &, KBSTATE newState) // FIXME ? struct HandlerCall { KBHandler function; KBData data; - KBSTATE state; + KBSTATE state{ KBSTATE::RESET }; // This variable was uninitialized. Is this a good initial value for it? HandlerCall() { function = DefaultKBHandler; diff --git a/engine/src/main.cpp b/engine/src/main.cpp index 12b96fd2f3..2eab4b6dbb 100644 --- a/engine/src/main.cpp +++ b/engine/src/main.cpp @@ -1,8 +1,8 @@ /* * main.cpp * - * Copyright (C) 2001-2023 Daniel Horn, pyramid3d, Stephen G. Tuggy, - * and other Vega Strike contributors. + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Benjamen R. Meyer, and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source * @@ -142,7 +142,7 @@ int readCommandLineOptions(int argc, char **argv); // FIXME: Code should throw exception instead of calling winsys_exit // Should it really? - stephengtuggy 2020-10-25 void VSExit(int code) { Music::CleanupMuzak(); - VegaStrikeLogging::vega_logger()->FlushLogs(); + VegaStrikeLogging::VegaStrikeLogger::instance().FlushLogsProgramExiting(); winsys_exit(code); } @@ -151,7 +151,7 @@ void cleanup(void) { // stephengtuggy 2020-10-30: Output message both to the console and to the logs printf("Thank you for playing!\n"); VS_LOG(info, "Thank you for playing!"); - VegaStrikeLogging::vega_logger()->FlushLogs(); + VegaStrikeLogging::VegaStrikeLogger::instance().FlushLogsProgramExiting(); if (_Universe != NULL) { _Universe->WriteSaveGame(true); } @@ -330,7 +330,7 @@ int main(int argc, char *argv[]) { home_subdir_path = home_path; } - VegaStrikeLogging::vega_logger()->InitLoggingPart2(g_game.vsdebug, home_subdir_path); + VegaStrikeLogging::VegaStrikeLogger::instance().InitLoggingPart2(g_game.vsdebug, home_subdir_path); // can use the vegastrike config variable to read in the default mission if (game_options()->force_client_connect) { @@ -400,8 +400,6 @@ int main(int argc, char *argv[]) { delete _Universe; CleanupUnitTables(); - // Just to be sure -- stephengtuggy 2020-07-27 - VegaStrikeLogging::vega_logger()->FlushLogs(); return 0; } diff --git a/engine/src/python/init.cpp b/engine/src/python/init.cpp index 1f8965406b..29d28b9794 100644 --- a/engine/src/python/init.cpp +++ b/engine/src/python/init.cpp @@ -1,7 +1,7 @@ /* * init.cpp * - * Copyright (C) 2001-2023 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, * and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -15,7 +15,7 @@ * * Vega Strike 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 + * 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 @@ -121,7 +121,6 @@ void Python::reseterrors() { VS_LOG_AND_FLUSH(error, "void Python::reseterrors(): Python error occurred"); PyErr_Print(); PyErr_Clear(); - VegaStrikeLogging::vega_logger()->FlushLogs(); } } @@ -242,7 +241,7 @@ void Python::test() { //vs_config->setVariable("data","test","NULL"); //VSFileSystem::vs_fprintf(stdout, "%s", vs_config->getVariable("data","test", string()).c_str()); //VSFileSystem::vs_fprintf(stdout, "output %s\n", PythonIOString::buffer.str()); - VegaStrikeLogging::vega_logger()->FlushLogs(); + VegaStrikeLogging::VegaStrikeLogger::instance().FlushLogs(); } #endif diff --git a/engine/src/savegame.cpp b/engine/src/savegame.cpp index 9c433a10c7..268d40e284 100644 --- a/engine/src/savegame.cpp +++ b/engine/src/savegame.cpp @@ -1,8 +1,8 @@ /* * savegame.cpp * - * Copyright (C) 2001-2023 Daniel Horn, pyramid3d, Stephen G. Tuggy, - * and other Vega Strike contributors. + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Roy Falk, and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source * @@ -879,7 +879,6 @@ void SaveGame::LoadSavedMissions() { VS_LOG_AND_FLUSH(error, "void SaveGame::LoadSavedMissions(): Python error occurred"); PyErr_Print(); PyErr_Clear(); - VegaStrikeLogging::vega_logger()->FlushLogs(); } else { throw; } diff --git a/engine/src/star_system.cpp b/engine/src/star_system.cpp index 48dad075a2..7bd4fbe8c5 100644 --- a/engine/src/star_system.cpp +++ b/engine/src/star_system.cpp @@ -1,7 +1,7 @@ /* * star_system.cpp * - * Copyright (C) 2001-2023 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, * and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -15,7 +15,7 @@ * * Vega Strike 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 + * 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 @@ -684,7 +684,6 @@ void StarSystem::ExecuteUnitAI() { VS_LOG_AND_FLUSH(fatal, "void StarSystem::ExecuteUnitAI(): Python error occurred"); PyErr_Print(); PyErr_Clear(); - VegaStrikeLogging::vega_logger()->FlushLogs(); } throw; } @@ -928,7 +927,6 @@ void StarSystem::UpdateUnitsPhysics(bool firstframe) { "void StarSystem::UpdateUnitPhysics( bool firstframe ): Msg D: Python error occurred"); PyErr_Print(); PyErr_Clear(); - VegaStrikeLogging::vega_logger()->FlushLogs(); } throw; } diff --git a/engine/src/vega_py_run.cpp b/engine/src/vega_py_run.cpp index 15bf11415f..cf5114361f 100644 --- a/engine/src/vega_py_run.cpp +++ b/engine/src/vega_py_run.cpp @@ -1,7 +1,7 @@ /* * vega_py_run.cpp * - * Copyright (C) 2001-2023 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, * and other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -49,7 +49,6 @@ void VegaPyRunString(char *py_snippet) { VS_LOG_AND_FLUSH(error, "VegaPyRunString: Python error occurred"); PyErr_Print(); PyErr_Clear(); - VegaStrikeLogging::vega_logger()->FlushLogs(); } //#endif } diff --git a/engine/src/vs_logging.cpp b/engine/src/vs_logging.cpp index 0bae2e555d..eab4b6be4a 100644 --- a/engine/src/vs_logging.cpp +++ b/engine/src/vs_logging.cpp @@ -1,5 +1,7 @@ /* - * Copyright (C) 2021-2022 Daniel Horn, Stephen G. Tuggy and + * vs_logging.cpp + * + * Copyright (C) 2001-2024 Daniel Horn, Stephen G. Tuggy and * other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -13,7 +15,7 @@ * * Vega Strike 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 + * 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 @@ -32,7 +34,7 @@ #include #include #include -//#include +#include #include #include #include @@ -45,7 +47,7 @@ namespace VegaStrikeLogging { // void exitProgram(int code) // { // Music::CleanupMuzak(); -// VegaStrikeLogging::vega_logger()->FlushLogs(); +// VegaStrikeLogging::VegaStrikeLogger::instance().FlushLogs(); // winsys_exit(code); // } @@ -72,7 +74,7 @@ void VegaStrikeLogger::InitLoggingPart2(const uint8_t debug_level, break; } - file_log_sink_ = boost::log::add_file_log + boost::shared_ptr file_log_back_end_ = boost::make_shared ( boost::log::keywords::file_name = logging_dir_name + "/" + "vegastrike_%Y-%m-%d_%H_%M_%S.%f.log", /*< file name pattern >*/ @@ -83,53 +85,80 @@ void VegaStrikeLogger::InitLoggingPart2(const uint8_t debug_level, boost::log::keywords::format = "[%TimeStamp%]: %Message%", /*< log record format >*/ boost::log::keywords::auto_flush = - true, /*false,*/ /*< whether to auto flush to the file after every line >*/ + false, /*true,*/ /*< whether to auto flush to the file after every line >*/ boost::log::keywords::min_free_space = 2UL * 1024UL * 1024UL * 1024UL /*< stop boost::log when there's only 2 GiB free space left >*/ ); + file_log_sink_ = boost::make_shared(file_log_back_end_, boost::null_deleter()); + logging_core_->add_sink(file_log_sink_); console_log_sink_->set_filter(severity >= fatal); } void VegaStrikeLogger::FlushLogs() { if (console_log_sink_) { + console_log_sink_->feed_records(); console_log_sink_->flush(); } if (file_log_sink_) { + file_log_sink_->feed_records(); file_log_sink_->flush(); } - fflush(stdout); - fflush(stderr); + std::cout << std::flush; + std::cerr << std::flush; + std::clog << std::flush; } -VegaStrikeLogger::VegaStrikeLogger() { +void VegaStrikeLogger::FlushLogsProgramExiting() { + if (console_log_sink_) { + logging_core_->remove_sink(console_log_sink_); + console_log_sink_->stop(); + console_log_sink_->feed_records(); + console_log_sink_->flush(); + } + if (file_log_sink_) { + logging_core_->remove_sink(file_log_sink_); + file_log_sink_->stop(); + file_log_sink_->feed_records(); + file_log_sink_->flush(); + } + + console_log_sink_.reset(); + file_log_sink_.reset(); + + std::cout << std::flush; + std::cerr << std::flush; + std::clog << std::flush; +} + +VegaStrikeLogger::VegaStrikeLogger() : slg_(), file_log_sink_(nullptr) { logging_core_ = boost::log::core::get(); boost::log::add_common_attributes(); - slg_ = boost::make_shared>(); - console_log_sink_ = boost::log::add_console_log + boost::shared_ptr console_log_back_end_ = boost::make_shared ( - std::cerr, + boost::shared_ptr(&std::cerr, boost::null_deleter()), boost::log::keywords::format = "%Message%", /*< log record format specific to the console >*/ boost::log::keywords::auto_flush = - true /*false*/ /*< whether to do the equivalent of fflush(stdout) after every msg >*/ + false /*true*/ /*< whether to do the equivalent of fflush(stdout) after every msg >*/ ); - file_log_sink_ = nullptr; + console_log_sink_ = boost::make_shared(console_log_back_end_, boost::null_deleter()); + logging_core_->add_sink(console_log_sink_); } VegaStrikeLogger::~VegaStrikeLogger() { - FlushLogs(); + FlushLogsProgramExiting(); } void VegaStrikeLogger::Log(const vega_log_level level, const std::string &message) { - boost::log::record rec = slg_->open_record(boost::log::keywords::severity = level); + boost::log::record rec = slg_.open_record(boost::log::keywords::severity = level); if (rec) { boost::log::record_ostream strm(rec); strm << message; strm.flush(); - slg_->push_record(boost::move(rec)); + slg_.push_record(boost::move(rec)); } } @@ -138,14 +167,20 @@ void VegaStrikeLogger::LogAndFlush(const vega_log_level level, const std::string FlushLogs(); } +void VegaStrikeLogger::LogFlushExit(const vega_log_level level, const std::string& message, const int exit_code = -50) { + Log(level, message); + FlushLogsProgramExiting(); + exit(exit_code); +} + void VegaStrikeLogger::Log(const vega_log_level level, const char *message) { - boost::log::record rec = slg_->open_record(boost::log::keywords::severity = level); + boost::log::record rec = slg_.open_record(boost::log::keywords::severity = level); if (rec) { boost::log::record_ostream strm(rec); strm << message; strm.flush(); - slg_->push_record(boost::move(rec)); + slg_.push_record(boost::move(rec)); } } @@ -154,14 +189,20 @@ void VegaStrikeLogger::LogAndFlush(const vega_log_level level, const char *messa FlushLogs(); } +void VegaStrikeLogger::LogFlushExit(const vega_log_level level, const char* message, const int exit_code = -50) { + Log(level, message); + FlushLogsProgramExiting(); + exit(exit_code); +} + void VegaStrikeLogger::Log(const vega_log_level level, const boost::basic_format &message) { - boost::log::record rec = slg_->open_record(boost::log::keywords::severity = level); + boost::log::record rec = slg_.open_record(boost::log::keywords::severity = level); if (rec) { boost::log::record_ostream strm(rec); strm << message; strm.flush(); - slg_->push_record(boost::move(rec)); + slg_.push_record(boost::move(rec)); } } @@ -170,9 +211,10 @@ void VegaStrikeLogger::LogAndFlush(const vega_log_level level, const boost::basi FlushLogs(); } -boost::shared_ptr vega_logger() { - static const boost::shared_ptr VEGA_LOGGER = boost::make_shared(); - return VEGA_LOGGER; +void VegaStrikeLogger::LogFlushExit(const vega_log_level level, const boost::basic_format& message, const int exit_code = -50) { + Log(level, message); + FlushLogsProgramExiting(); + exit(exit_code); } } // namespace VegaStrikeLogging diff --git a/engine/src/vs_logging.h b/engine/src/vs_logging.h index f61fc00c04..fb1c38f70b 100644 --- a/engine/src/vs_logging.h +++ b/engine/src/vs_logging.h @@ -1,5 +1,7 @@ /* - * Copyright (C) 2021-2023 Daniel Horn, Stephen G. Tuggy, Benjamen R. Meyer, and + * vs_logging.h + * + * Copyright (C) 2021-2024 Daniel Horn, Stephen G. Tuggy, Benjamen R. Meyer, and * other Vega Strike contributors. * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -13,12 +15,14 @@ * * Vega Strike 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 + * 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 Vega Strike. If not, see . */ + + #ifndef VEGA_STRIKE_ENGINE_VS_LOGGING_H #define VEGA_STRIKE_ENGINE_VS_LOGGING_H @@ -26,10 +30,12 @@ #include "boost/move/utility_core.hpp" #include "boost/smart_ptr/shared_ptr.hpp" +#include "boost/smart_ptr/make_shared_object.hpp" #include "boost/format.hpp" //#include "boost/log/sources/logger.hpp" #include "boost/log/sources/severity_logger.hpp" #include "boost/log/sources/record_ostream.hpp" +#include "boost/log/sinks/async_frontend.hpp" #include "boost/log/sinks/sync_frontend.hpp" #include "boost/log/sinks/text_file_backend.hpp" #include "boost/log/sinks/text_ostream_backend.hpp" @@ -50,39 +56,54 @@ enum vega_log_level { BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", vega_log_level) -typedef boost::log::sinks::synchronous_sink ConsoleLogSink; -typedef boost::log::sinks::synchronous_sink FileLogSink; +typedef boost::log::sinks::text_ostream_backend ConsoleLogBackEnd; +typedef boost::log::sinks::text_file_backend FileLogBackEnd; +typedef boost::log::sinks::asynchronous_sink ConsoleLogSink; +typedef boost::log::sinks::asynchronous_sink FileLogSink; -#define VS_LOG(log_level, log_message) \ - do { \ - VegaStrikeLogging::vega_logger()->Log(VegaStrikeLogging::vega_log_level::log_level, (log_message)); \ +#define VS_LOG(log_level, log_message) \ + do { \ + VegaStrikeLogging::VegaStrikeLogger::instance().Log(VegaStrikeLogging::vega_log_level::log_level, (log_message)); \ } while (false) -#define VS_LOG_AND_FLUSH(log_level, log_message) \ - do { \ - VegaStrikeLogging::vega_logger()->LogAndFlush(VegaStrikeLogging::vega_log_level::log_level, (log_message)); \ +#define VS_LOG_AND_FLUSH(log_level, log_message) \ + do { \ + VegaStrikeLogging::VegaStrikeLogger::instance().LogAndFlush(VegaStrikeLogging::vega_log_level::log_level, (log_message)); \ } while (false) +//#define VS_LOG_FLUSH_EXIT(log_level, log_message) -class VegaStrikeLogger { +struct VegaStrikeLogger { private: boost::log::core_ptr logging_core_; - boost::shared_ptr> slg_; + boost::log::sources::severity_logger_mt slg_; boost::shared_ptr console_log_sink_; boost::shared_ptr file_log_sink_; -public: +private: VegaStrikeLogger(); ~VegaStrikeLogger(); + +public: + static VegaStrikeLogger& instance() { + static VegaStrikeLogger logger_instance; + return logger_instance; + } + void InitLoggingPart2(const uint8_t debug_level, const boost::filesystem::path &vega_strike_home_dir); void FlushLogs(); + void FlushLogsProgramExiting(); void Log(const vega_log_level level, const std::string& message); void Log(const vega_log_level level, const char * message); void Log(const vega_log_level level, const boost::basic_format& message); void LogAndFlush(const vega_log_level level, const std::string& message); + void LogFlushExit(const vega_log_level level, const std::string& message, const int exit_code); void LogAndFlush(const vega_log_level level, const char * message); + void LogFlushExit(const vega_log_level level, const char* message, const int exit_code); void LogAndFlush(const vega_log_level level, const boost::basic_format& message); -}; + void LogFlushExit(const vega_log_level level, const boost::basic_format& message, const int exit_code); -extern boost::shared_ptr vega_logger(); + VegaStrikeLogger(const VegaStrikeLogger &) = delete; + VegaStrikeLogger& operator = (const VegaStrikeLogger&) = delete; +}; } // namespace VegaStrikeLogging