Skip to content

Commit

Permalink
#5231: Move LogFile management to the core module. It has its own Mod…
Browse files Browse the repository at this point in the history
…uleRegistry instance now too.
  • Loading branch information
codereader committed Apr 29, 2020
1 parent b397436 commit 47bde40
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 131 deletions.
72 changes: 72 additions & 0 deletions libs/module/CoreModule.cpp
@@ -0,0 +1,72 @@
#include "CoreModule.h"

#include "DynamicLibrary.h"

namespace module
{

CoreModule::CoreModule(ApplicationContext& context) :
_instance(nullptr)
{
std::string coreModuleFile = std::string("DarkRadiantCore") + MODULE_FILE_EXTENSION;

fs::path coreModulePath = context.getApplicationPath();
coreModulePath /= coreModuleFile;

if (!fs::exists(coreModulePath))
{
throw FailureException("Cannot find the main module " + coreModuleFile);
}

_coreModuleLibrary.reset(new DynamicLibrary(coreModulePath.string()));

if (_coreModuleLibrary->failed())
{
throw FailureException("Cannot load the main module " + _coreModuleLibrary->getName());
}

auto symbol = _coreModuleLibrary->findSymbol(QUOTE(SYMBOL_CREATE_RADIANT));

if (symbol == nullptr)
{
throw FailureException("Main module " + _coreModuleLibrary->getName() +
" doesn't expose the symbol " + QUOTE(SYMBOL_CREATE_RADIANT));
}

auto createFunc = reinterpret_cast<CreateRadiantFunc>(symbol);

_instance = createFunc(context);
}

CoreModule::~CoreModule()
{
destroy();
}

radiant::IRadiant* CoreModule::get()
{
return _instance;
}

void CoreModule::destroy()
{
if (_instance)
{
assert(_coreModuleLibrary);

auto symbol = _coreModuleLibrary->findSymbol(QUOTE(SYMBOL_DESTROY_RADIANT));

if (symbol == nullptr)
{
throw FailureException("Main module " + _coreModuleLibrary->getName() +
" doesn't expose the symbol " + QUOTE(SYMBOL_DESTROY_RADIANT));
}

auto destroyFunc = reinterpret_cast<DestroyRadiantFunc>(symbol);

destroyFunc(_instance);
_instance = nullptr;
}
}

}
70 changes: 7 additions & 63 deletions libs/module/CoreModule.h
Expand Up @@ -4,8 +4,6 @@
#include "iradiant.h"
#include "os/fs.h"

#include "DynamicLibrary.h"

#define SYMBOL_CREATE_RADIANT CreateRadiant
#define SYMBOL_DESTROY_RADIANT DestroyRadiant
#define Q(x) #x
Expand All @@ -14,6 +12,8 @@
namespace module
{

class DynamicLibrary;

class CoreModule
{
private:
Expand All @@ -22,7 +22,7 @@ class CoreModule

radiant::IRadiant* _instance;

DynamicLibraryPtr _coreModuleLibrary;
std::unique_ptr<DynamicLibrary> _coreModuleLibrary;

public:
class FailureException :
Expand All @@ -34,70 +34,14 @@ class CoreModule
{}
};

CoreModule(ApplicationContext& context) :
_instance(nullptr)
{
std::string coreModuleFile = std::string("DarkRadiantCore") + MODULE_FILE_EXTENSION;

fs::path coreModulePath = context.getApplicationPath();
coreModulePath /= coreModuleFile;

if (!fs::exists(coreModulePath))
{
throw FailureException("Cannot find the main module " + coreModuleFile);
}

_coreModuleLibrary = std::make_shared<DynamicLibrary>(coreModulePath.string());

if (_coreModuleLibrary->failed())
{
throw FailureException("Cannot load the main module " + _coreModuleLibrary->getName());
}

auto symbol = _coreModuleLibrary->findSymbol(QUOTE(SYMBOL_CREATE_RADIANT));

if (symbol == nullptr)
{
throw FailureException("Main module " + _coreModuleLibrary->getName() +
" doesn't expose the symbol " + QUOTE(SYMBOL_CREATE_RADIANT));
}
CoreModule(ApplicationContext& context);

auto createFunc = reinterpret_cast<CreateRadiantFunc>(symbol);
~CoreModule();

_instance = createFunc(context);
}

~CoreModule()
{
destroy();
}

radiant::IRadiant* get()
{
return _instance;
}
radiant::IRadiant* get();

private:
void destroy()
{
if (_instance)
{
assert(_coreModuleLibrary);

auto symbol = _coreModuleLibrary->findSymbol(QUOTE(SYMBOL_DESTROY_RADIANT));

if (symbol == nullptr)
{
throw FailureException("Main module " + _coreModuleLibrary->getName() +
" doesn't expose the symbol " + QUOTE(SYMBOL_DESTROY_RADIANT));
}

auto destroyFunc = reinterpret_cast<DestroyRadiantFunc>(symbol);

destroyFunc(_instance);
_instance = nullptr;
}
}
void destroy();
};

}
68 changes: 68 additions & 0 deletions radiant/Radiant.cpp
@@ -1,35 +1,103 @@
#include "iradiant.h"

#include <iomanip>
#include "version.h"

#include "string/convert.h"
#include "module/CoreModule.h"

#include "log/LogStream.h"
#include "log/LogWriter.h"
#include "log/LogFile.h"
#include "modulesystem/ModuleRegistry.h"

#include <wx/version.h>

namespace radiant
{

namespace
{
const char* const TIME_FMT = "%Y-%m-%d %H:%M:%S";
}

class Radiant :
public IRadiant
{
private:
ApplicationContext& _context;

std::unique_ptr<applog::LogFile> _logFile;

std::unique_ptr<module::ModuleRegistry> _moduleRegistry;

public:
Radiant(ApplicationContext& context) :
_context(context)
{
// Set the stream references for rMessage(), redirect std::cout, etc.
applog::LogStream::InitialiseStreams(getLogWriter());

// Attach the logfile to the logwriter
createLogFile();

_moduleRegistry.reset(new module::ModuleRegistry);
_moduleRegistry->setContext(_context);
}

~Radiant()
{
_moduleRegistry.reset();

// Close the log file
if (_logFile)
{
_logFile->close();
getLogWriter().detach(_logFile.get());
_logFile.reset();
}

applog::LogStream::ShutdownStreams();
}

applog::ILogWriter& getLogWriter() override
{
return applog::LogWriter::Instance();
}

private:
void createLogFile()
{
_logFile.reset(new applog::LogFile(_context.getSettingsPath() + "darkradiant.log"));

if (_logFile->isOpen())
{
getLogWriter().attach(_logFile.get());

rMessage() << "Started logging to " << _logFile->getFullPath() << std::endl;

rMessage() << "This is " << RADIANT_APPNAME_FULL() << std::endl;

std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);

// Write timestamp and thread information
rMessage() << "Today is " << std::put_time(&tm, TIME_FMT) << std::endl;

// Output the wxWidgets version to the logfile
std::string wxVersion = string::to_string(wxMAJOR_VERSION) + ".";
wxVersion += string::to_string(wxMINOR_VERSION) + ".";
wxVersion += string::to_string(wxRELEASE_NUMBER);

rMessage() << "wxWidgets Version: " << wxVersion << std::endl;
}
else
{
rConsoleError() << "Failed to create log file '"
<< _logFile->getFullPath() << ", check write permissions in parent directory."
<< std::endl;
}
}
};

}
Expand Down
55 changes: 1 addition & 54 deletions radiant/RadiantApp.cpp
Expand Up @@ -4,7 +4,6 @@
#include "iradiant.h"
#include "version.h"

#include "log/LogFile.h"
#include "log/PIDFile.h"
#include "modulesystem/ModuleRegistry.h"
#include "module/CoreModule.h"
Expand Down Expand Up @@ -35,11 +34,6 @@
// The startup event which will be queued in App::OnInit()
wxDEFINE_EVENT(EV_RadiantStartup, wxCommandEvent);

namespace
{
const char* const TIME_FMT = "%Y-%m-%d %H:%M:%S";
}

bool RadiantApp::OnInit()
{
if (!wxApp::OnInit()) return false;
Expand Down Expand Up @@ -72,9 +66,6 @@ bool RadiantApp::OnInit()
throw ex;
}

// Attach the logfile to the core binary's logwriter
createLogFile();

#ifndef POSIX
// Initialise the language based on the settings in the user settings folder
language::LanguageManager().init(_context);
Expand Down Expand Up @@ -106,56 +97,12 @@ bool RadiantApp::OnInit()
return true;
}

void RadiantApp::createLogFile()
{
_logFile.reset(new applog::LogFile(_context.getSettingsPath() + "darkradiant.log"));

if (_logFile->isOpen())
{
_coreModule->get()->getLogWriter().attach(_logFile.get());

rMessage() << "Started logging to " << _logFile->getFullPath() << std::endl;

rMessage() << "This is " << RADIANT_APPNAME_FULL() << std::endl;

std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);

// Write timestamp and thread information
rMessage() << "Today is " << std::put_time(&tm, TIME_FMT) << std::endl;

// Output the wxWidgets version to the logfile
std::string wxVersion = string::to_string(wxMAJOR_VERSION) + ".";
wxVersion += string::to_string(wxMINOR_VERSION) + ".";
wxVersion += string::to_string(wxRELEASE_NUMBER);

rMessage() << "wxWidgets Version: " << wxVersion << std::endl;
}
else
{
rConsoleError() << "Failed to create log file '"
<< _logFile->getFullPath() << ", check write permissions in parent directory."
<< std::endl;
}
}

int RadiantApp::OnExit()
{
// Issue a shutdown() call to all the modules
module::GlobalModuleRegistry().shutdownModules();

if (_coreModule)
{
// Close the log file
if (_logFile)
{
_logFile->close();
_coreModule->get()->getLogWriter().detach(_logFile.get());
_logFile.reset();
}

_coreModule.reset();
}
_coreModule.reset();

return wxApp::OnExit();
}
Expand Down
4 changes: 0 additions & 4 deletions radiant/RadiantApp.h
Expand Up @@ -3,7 +3,6 @@
#include <wx/app.h>
#include "modulesystem/ApplicationContextImpl.h"
#include "module/CoreModule.h"
#include "log/LogFile.h"

/**
* Main application class required by wxWidgets
Expand All @@ -25,8 +24,6 @@ class RadiantApp :

std::unique_ptr<module::CoreModule> _coreModule;

std::unique_ptr<applog::LogFile> _logFile;

public:
bool OnInit() override;
int OnExit() override;
Expand All @@ -37,6 +34,5 @@ class RadiantApp :
bool OnExceptionInMainLoop() override;

private:
void createLogFile();
void onStartupEvent(wxCommandEvent& ev);
};

0 comments on commit 47bde40

Please sign in to comment.