Skip to content

Commit

Permalink
Add logger module
Browse files Browse the repository at this point in the history
Implementing separated logger module.
  • Loading branch information
winterheart committed Sep 18, 2024
1 parent 4f4ab3b commit dc0cd88
Show file tree
Hide file tree
Showing 28 changed files with 138 additions and 71 deletions.
2 changes: 1 addition & 1 deletion 2dlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ set(CPPS
add_library(2dlib STATIC ${HEADERS} ${CPPS})
target_link_libraries(2dlib PRIVATE
cfile
logger
mem
misc
plog::plog
)
target_include_directories(2dlib PUBLIC
$<BUILD_INTERFACE:
Expand Down
2 changes: 1 addition & 1 deletion AudioEncode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(CPPS
add_library(AudioEncode STATIC ${HEADERS} ${CPPS})
target_link_libraries(AudioEncode PRIVATE
libacm
plog::plog
logger
)
target_include_directories(AudioEncode PUBLIC
$<BUILD_INTERFACE:
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ add_subdirectory(linux)
add_subdirectory(ddio)
add_subdirectory(dd_video)
add_subdirectory(fix)
add_subdirectory(logger)
add_subdirectory(manage)
add_subdirectory(grtext)
add_subdirectory(mem)
Expand Down
33 changes: 30 additions & 3 deletions Descent3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,36 @@ file(GLOB_RECURSE INCS "../lib/*.h")

add_executable(Descent3 WIN32 MACOSX_BUNDLE ${D3Icon} ${HEADERS} ${CPPS} ${INCS} ${MANIFEST} ${RC_FILE})
target_link_libraries(Descent3 PRIVATE
2dlib AudioEncode bitmap cfile dd_video ddebug ddio libmve libacm
fix grtext manage mem misc model module stream_audio linux SDL2::SDL2 plog::plog
music networking physics renderer rtperformance sndlib ui unzip vecmat md5
SDL2::SDL2
2dlib
AudioEncode
bitmap
cfile
dd_video
ddebug
ddio
fix
grtext
libmve
libacm
linux
logger
manage
mem
md5
misc
model
module
music
networking
physics
renderer
rtperformance
sndlib
stream_audio
ui
unzip
vecmat
${PLATFORM_LIBS})
target_include_directories(Descent3 PRIVATE ${PROJECT_BINARY_DIR}/lib)
target_link_options(Descent3 PRIVATE $<$<PLATFORM_ID:Windows>:/DEBUG:FULL>)
Expand Down
44 changes: 1 addition & 43 deletions Descent3/sdlmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,17 @@
#include <csignal>
#endif

#ifdef WIN32
#include <cstdio>
#include <windows.h>
#include "debug.h"
#endif

#include <SDL.h>
// We use direct plog includes instead of log.h for logger instance initialization
#include <plog/Log.h>
#include <plog/Appenders/ColorConsoleAppender.h>
#include <plog/Initializers/RollingFileInitializer.h>

#include "appdatabase.h"
#include "ddio.h"
#include "application.h"
#include "args.h"
#include "d3_version.h"
#include "ddio.h"
#include "descent.h"
#include "dedicated_server.h"
#include "init.h"
#include "log.h"

#ifdef WIN32
#include "debug.h"
Expand Down Expand Up @@ -216,38 +206,6 @@ int SDLCALL d3SDLEventFilter(void *userdata, SDL_Event *event) {
return (1);
}

/**
* Initialize logger facility.
* @param log_level desired log level (for example, plog::debug)
* @param enable_filelog enable logging into Descent.log
* @param enable_win_console enable console windows for WIN32 (no-op for POSIX systems)
*/
void InitLog(plog::Severity log_level, bool enable_filelog, bool enable_win_console) {
std::filesystem::path log_file = "Descent3.log";
static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
static plog::RollingFileAppender<plog::TxtFormatter> fileAppender(log_file.u8string().c_str());

#ifdef WIN32
if (enable_win_console) {
// Open console window
AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
#endif

plog::init(log_level, &consoleAppender);
if (enable_filelog) {
if (std::filesystem::is_regular_file(log_file)) {
// Delete old log
std::error_code ec;
std::filesystem::remove(log_file, ec);
plog::get()->addAppender(&fileAppender);
}
}
}

// ---------------------------------------------------------------------------
// Main
// creates all the OS objects and then runs Descent 3.
Expand Down
2 changes: 1 addition & 1 deletion bitmap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ target_link_libraries(bitmap PRIVATE
cfile
ddebug
ddio
logger
mem
misc
plog::plog
stb
)
target_include_directories(bitmap PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion cfile/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ set(CPPS
add_library(cfile STATIC ${HEADERS} ${CPPS})
target_link_libraries(cfile PRIVATE
ddio
logger
mem
misc
plog::plog
)
target_include_directories(cfile PUBLIC
$<BUILD_INTERFACE:
Expand Down
2 changes: 1 addition & 1 deletion dd_grwin32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ set(CPPS
add_library(dd_grwin32 STATIC ${HEADERS} ${CPPS})
target_link_libraries(dd_grwin32 PRIVATE
2dlib
logger
misc
plog::plog
)
target_compile_definitions(dd_grwin32 PRIVATE DX_APP)
target_link_libraries(dd_grwin32 PRIVATE ${DDRAW_LIBRARY})
2 changes: 1 addition & 1 deletion ddio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ set(CPPS
add_library(ddio STATIC ${HEADERS} ${CPPS})
target_link_libraries(ddio PRIVATE
SDL2::SDL2
plog::plog
ddebug
logger
mem
misc
)
Expand Down
2 changes: 1 addition & 1 deletion grtext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ set(CPPS
add_library(grtext STATIC ${HEADERS} ${CPPS})
target_link_libraries(grtext PRIVATE
ddio
logger
mem
misc
plog::plog
)
target_include_directories(grtext PUBLIC
$<BUILD_INTERFACE:
Expand Down
2 changes: 1 addition & 1 deletion linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(CPPS
add_library(linux STATIC ${HEADERS} ${CPPS})
target_link_libraries(linux PRIVATE
cfile
plog::plog
logger
)
target_include_directories(linux PUBLIC
$<BUILD_INTERFACE:
Expand Down
16 changes: 16 additions & 0 deletions logger/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set(HEADERS
log.h
)
set(CPPS
log.cpp
)

add_library(logger STATIC ${HEADERS} ${CPPS})
target_link_libraries(logger PUBLIC
plog::plog
)
target_include_directories(logger PUBLIC
$<BUILD_INTERFACE:
${PROJECT_SOURCE_DIR}/logger
>
)
56 changes: 56 additions & 0 deletions logger/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Descent 3
* Copyright (C) 2024 Descent Developers
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include <filesystem>
#include <plog/Log.h>
#include <plog/Appenders/ColorConsoleAppender.h>
#include <plog/Initializers/RollingFileInitializer.h>

#ifdef WIN32
#include <cstdio>
#include <windows.h>
#include "debug.h"
#endif

#include "log.h"

void InitLog(plog::Severity log_level, bool enable_filelog, bool enable_win_console) {
std::filesystem::path log_file = "Descent3.log";
static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
static plog::RollingFileAppender<plog::TxtFormatter> fileAppender(log_file.u8string().c_str());

#ifdef WIN32
if (enable_win_console) {
// Open console window
AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
#endif

plog::init(log_level, &consoleAppender);
if (enable_filelog) {
if (std::filesystem::is_regular_file(log_file)) {
// Delete old log
std::error_code ec;
std::filesystem::remove(log_file, ec);
}
plog::get()->addAppender(&fileAppender);
}
}
8 changes: 8 additions & 0 deletions lib/log.h → logger/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ In case of swapping to another solution here should be redefined following macro
#define LOG_FATAL PLOG(plog::fatal)
*/

/**
* Initialize logger facility.
* @param log_level desired log level (for example, plog::debug)
* @param enable_filelog enable logging into Descent.log
* @param enable_win_console enable console windows for WIN32 (no-op for POSIX systems)
*/
void InitLog(plog::Severity log_level, bool enable_filelog, bool enable_win_console);
2 changes: 1 addition & 1 deletion manage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ add_library(manage STATIC ${HEADERS} ${CPPS})
target_link_libraries(manage PRIVATE
cfile
ddio
logger
mem
misc
model
sndlib
stream_audio
plog::plog
)
2 changes: 1 addition & 1 deletion mem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ target_compile_definitions(mem PUBLIC
$<$<BOOL:${ENABLE_MEM_RTL}>:MEM_USE_RTL>
)
target_link_libraries(mem PRIVATE
logger
misc
plog::plog
)
target_include_directories(mem PUBLIC
$<BUILD_INTERFACE:
Expand Down
2 changes: 1 addition & 1 deletion model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ set(CPPS
add_library(model STATIC ${HEADERS} ${CPPS})
target_link_libraries(model PRIVATE
cfile
logger
mem
misc
physics
plog::plog
)
target_include_directories(model PUBLIC
$<BUILD_INTERFACE:
Expand Down
1 change: 1 addition & 0 deletions module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(CPPS
add_library(module STATIC ${HEADERS} ${CPPS})
target_link_libraries(module PRIVATE
ddebug
logger
)
target_include_directories(module PUBLIC
$<BUILD_INTERFACE:
Expand Down
10 changes: 5 additions & 5 deletions module/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
#endif

#include "crossplat.h"
#include "log.h"
#include "module.h"
#include "mono.h"

/**
* Returns fixed case file name to actual case on disk for case-sensitive filesystems (Linux).
Expand Down Expand Up @@ -231,18 +231,18 @@ bool mod_LoadModule(module *handle, const std::filesystem::path &imodfilename, i
std::filesystem::path new_filename = mod_FindRealFileNameCaseInsensitive(parent_path, modfilename.filename());

if (new_filename.empty()) {
mprintf(0, "Module Load Err: %s\n", dlerror());
LOG_ERROR.printf("Module Load Err: %s", dlerror());
ModLastError = MODERR_MODNOTFOUND;
return false;
}

// ok we have a different filename
mprintf(0, "MOD: Attempting to open %s instead of %s\n", new_filename.u8string().c_str(),
modfilename.u8string().c_str());
LOG_DEBUG.printf("MOD: Attempting to open %s instead of %s", new_filename.u8string().c_str(),
modfilename.u8string().c_str());
modfilename = parent_path / new_filename;
handle->handle = dlopen(modfilename.u8string().c_str(), f);
if (!handle->handle) {
mprintf(0, "Module Load Err: %s\n", dlerror());
LOG_ERROR.printf("Module Load Err: %s", dlerror());
ModLastError = MODERR_MODNOTFOUND;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion music/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ target_link_libraries(music
AudioEncode
cfile
ddio
logger
mem
misc
sndlib
stream_audio
plog::plog
)
target_include_directories(music PUBLIC
$<BUILD_INTERFACE:
Expand Down
2 changes: 1 addition & 1 deletion networking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ endif()
add_library(networking STATIC ${HEADERS} ${CPPS})
target_link_libraries(networking PRIVATE
ddio
logger
mem
misc
module
${PLATFORM_LIBS}
plog::plog
)
2 changes: 1 addition & 1 deletion physics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ set(CPPS
add_library(physics STATIC ${HEADERS} ${CPPS})
target_link_libraries(physics PRIVATE
ddio
logger
mem
misc
model
module
rtperformance
sndlib
plog::plog
)
target_include_directories(physics PUBLIC
$<BUILD_INTERFACE:
Expand Down
Loading

0 comments on commit dc0cd88

Please sign in to comment.