File: Radiant.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (169 lines) | stat: -rw-r--r-- 3,886 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "Radiant.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 "log/SegFaultHandler.h"
#include "module/StaticModule.h"
#include "messagebus/MessageBus.h"
#include "settings/LanguageManager.h"

namespace radiant
{

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

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

    // Initialise the GlobalErorrHandler() function object, which is used by ASSERT_MESSAGE
    // This is usually a function owned by the UI module to show a popup
    GlobalErrorHandler() = _context.getErrorHandlingFunction();

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

#ifdef POSIX
    applog::SegFaultHandler::Install();
#endif

	_moduleRegistry.reset(new module::ModuleRegistry(_context));

	_languageManager.reset(new language::LanguageManager);
}

Radiant::~Radiant()
{
	_moduleRegistry.reset();

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

	applog::LogStream::ShutdownStreams();
}

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

module::ModuleRegistry& Radiant::getModuleRegistry()
{
	return *_moduleRegistry;
}

radiant::IMessageBus& Radiant::getMessageBus()
{
	return *_messageBus;
}

language::ILanguageManager& Radiant::getLanguageManager()
{
	return *_languageManager;
}

void Radiant::startup()
{
	try
	{
		// Register the modules hosted in this binary
		module::internal::StaticModuleList::RegisterModules();

		module::GlobalModuleRegistry().loadAndInitialiseModules();
	}
	catch (const std::exception& e)
	{
		rError() << "Exception initialising modules: " << e.what() << std::endl;
		throw StartupFailure(e.what()); // translate the exception
	}
}

void Radiant::createLogFile()
{
	_logFile.reset(new applog::LogFile(_context.getCacheDataPath() + "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;
	}
	else
	{
		rConsoleError() << "Failed to create log file '"
			<< _logFile->getFullPath() << ", check write permissions in parent directory."
			<< std::endl;
	}
}

const std::string& Radiant::getName() const
{
	static std::string _name(MODULE_RADIANT_CORE);
	return _name;
}

const StringSet& Radiant::getDependencies() const
{
	static StringSet _dependencies;
	return _dependencies;
}

void Radiant::initialiseModule(const IApplicationContext& ctx)
{}

std::shared_ptr<Radiant>& Radiant::InstancePtr()
{
	static std::shared_ptr<Radiant> _instancePtr;
	return _instancePtr;
}

}

extern "C" DARKRADIANT_DLLEXPORT radiant::IRadiant* SYMBOL_CREATE_RADIANT(IApplicationContext& context)
{
	auto& instancePtr = radiant::Radiant::InstancePtr();

	// Create a new instance, but ensure that this has only been called once
	assert(!instancePtr);

	instancePtr.reset(new radiant::Radiant(context));

	// Add this module to the registry it's holding
	instancePtr->getModuleRegistry().registerModule(instancePtr);
	instancePtr->getModuleRegistry().initialiseCoreModule();

	return instancePtr.get();
}

extern "C" DARKRADIANT_DLLEXPORT void SYMBOL_DESTROY_RADIANT(radiant::IRadiant* radiant)
{
	assert(radiant::Radiant::InstancePtr().get() == radiant);

	radiant::Radiant::InstancePtr().reset();
}