Move all logging facilities from OSystem directly into the Logger class.

This commit is contained in:
Stephen Anthony 2019-12-22 17:36:06 -03:30
parent 60c9839541
commit 1223958d91
6 changed files with 49 additions and 74 deletions

View File

@ -34,7 +34,7 @@ class FBSurfaceSDL2 : public FBSurface
public:
FBSurfaceSDL2(FrameBufferSDL2& buffer, uInt32 width, uInt32 height,
FrameBuffer::ScalingInterpolation interpolation,
const uInt32* data);
const uInt32* staticData);
virtual ~FBSurfaceSDL2();
// Most of the surface drawing primitives are implemented in FBSurface;

View File

@ -49,23 +49,34 @@ void Logger::debug(const string& message)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Logger::logMessage(const string& message, Level level) const
void Logger::logMessage(const string& message, Level level)
{
if (myLogCallback)
myLogCallback(message, level);
else
if(level == Logger::Level::ERR)
{
cout << message << endl << std::flush;
myLogMessages += message + "\n";
}
else if(static_cast<int>(level) <= myLogLevel)
{
if(myLogToConsole)
cout << message << endl << std::flush;
myLogMessages += message + "\n";
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Logger::setLogCallback(Logger::logCallback callback)
void Logger::setLogParameters(int logLevel, bool logToConsole)
{
myLogCallback = callback;
if(logLevel >= static_cast<int>(Level::MIN) &&
logLevel <= static_cast<int>(Level::MAX))
{
myLogLevel = logLevel;
myLogToConsole = logToConsole;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Logger::clearLogCallback()
void Logger::setLogParameters(Level logLevel, bool logToConsole)
{
myLogCallback = logCallback();
setLogParameters(static_cast<int>(logLevel), logToConsole);
}

View File

@ -34,8 +34,6 @@ class Logger {
MAX = DEBUG
};
using logCallback = std::function<void(const string&, Logger::Level)>;
public:
static Logger& instance();
@ -48,21 +46,23 @@ class Logger {
static void debug(const string& message);
void setLogCallback(logCallback callback);
void setLogParameters(int logLevel, bool logToConsole);
void setLogParameters(Level logLevel, bool logToConsole);
void clearLogCallback();
const string& logMessages() const { return myLogMessages; }
protected:
Logger() = default;
Logger() { setLogParameters(Level::MAX, true); }
private:
int myLogLevel;
bool myLogToConsole;
logCallback myLogCallback;
// The list of log messages
string myLogMessages;
private:
void logMessage(const string& message, Level level) const;
void logMessage(const string& message, Level level);
Logger(const Logger&) = delete;
Logger(Logger&&) = delete;

View File

@ -78,7 +78,6 @@ namespace {
OSystem::OSystem()
: myLauncherUsed(false),
myQuitLoop(false),
mySettingsLoaded(false),
myFpsMeter(FPS_METER_QUEUE_SIZE)
{
// Get built-in features
@ -103,7 +102,6 @@ OSystem::OSystem()
// Get build info
ostringstream info;
info << "Build " << STELLA_BUILD << ", using " << MediaFactory::backendName()
<< " [" << BSPF::ARCH << "]";
myBuildInfo = info.str();
@ -112,15 +110,12 @@ OSystem::OSystem()
myPropSet = make_unique<PropertiesSet>();
Logger::instance().setLogCallback(
std::bind(&OSystem::logMessage, this, std::placeholders::_1, std::placeholders::_2)
);
Logger::instance().setLogParameters(Logger::Level::MAX, false);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
OSystem::~OSystem()
{
Logger::instance().clearLogCallback();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -232,9 +227,11 @@ void OSystem::loadConfig(const Settings::Options& options)
mySettings->setRepository(createSettingsRepository());
Logger::debug("Loading config options ...");
mySettings->load(options);
mySettingsLoaded = true;
Logger::instance().setLogParameters(mySettings->getInt("loglevel"),
mySettings->getBool("logtoconsole"));
Logger::debug("Loading config options ...");
// Get updated paths for all configuration files
setConfigPaths();
@ -505,22 +502,6 @@ string OSystem::getROMInfo(const FilesystemNode& romfile)
return getROMInfo(*console);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystem::logMessage(const string& message, Logger::Level level)
{
if(level == Logger::Level::ERR)
{
cout << message << endl << std::flush;
myLogMessages += message + "\n";
}
else if(int(level) <= uInt8(mySettings->getInt("loglevel")) || !mySettingsLoaded)
{
if(mySettings->getBool("logtoconsole"))
cout << message << endl << std::flush;
myLogMessages += message + "\n";
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystem::resetFps()
{

View File

@ -375,13 +375,6 @@ class OSystem
*/
void quit() { myQuitLoop = true; }
/**
Get the system messages logged up to this point.
@return The list of log messages
*/
const string& logMessages() const { return myLogMessages; }
/**
Reset FPS measurement.
*/
@ -424,16 +417,6 @@ class OSystem
virtual shared_ptr<KeyValueRepository> createSettingsRepository();
/**
Append a message to the internal log
(a newline is automatically added).
@param message The message to be appended
@param level If 0, always output the message, only append when
level is less than or equal to that in 'loglevel'
*/
void logMessage(const string& message, Logger::Level level);
//////////////////////////////////////////////////////////////////////
// The following methods are system-specific and *must* be
// implemented in derived classes.
@ -518,9 +501,6 @@ class OSystem
// Pointer to the TimerManager object
unique_ptr<TimerManager> myTimerManager;
// The list of log messages
string myLogMessages;
// Indicates whether ROM launcher was ever opened during this run
bool myLauncherUsed;
@ -536,8 +516,6 @@ class OSystem
string myDefaultSaveDir;
string myDefaultLoadDir;
bool mySettingsLoaded;
string myCheatFile;
string myConfigFile;
string myPaletteFile;

View File

@ -58,9 +58,9 @@ LoggerDialog::LoggerDialog(OSystem& osystem, DialogContainer& parent,
// Level of logging (how much info to print)
VariantList items;
VarList::push_back(items, "None", int(Logger::Level::ERR));
VarList::push_back(items, "Basic", int(Logger::Level::INFO));
VarList::push_back(items, "Verbose", int(Logger::Level::DEBUG));
VarList::push_back(items, "None", static_cast<int>(Logger::Level::ERR));
VarList::push_back(items, "Basic", static_cast<int>(Logger::Level::INFO));
VarList::push_back(items, "Verbose", static_cast<int>(Logger::Level::DEBUG));
myLogLevel =
new PopUpWidget(this, font, xpos, ypos, font.getStringWidth("Verbose"),
lineHeight, items, "Log level ",
@ -86,21 +86,26 @@ LoggerDialog::LoggerDialog(OSystem& osystem, DialogContainer& parent,
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void LoggerDialog::loadConfig()
{
StringParser parser(instance().logMessages());
StringParser parser(Logger::instance().logMessages());
myLogInfo->setList(parser.stringList());
myLogInfo->setSelected(0);
myLogInfo->scrollToEnd();
myLogLevel->setSelected(instance().settings().getString("loglevel"), int(Logger::Level::INFO));
myLogLevel->setSelected(instance().settings().getString("loglevel"),
static_cast<int>(Logger::Level::INFO));
myLogToConsole->setState(instance().settings().getBool("logtoconsole"));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void LoggerDialog::saveConfig()
{
instance().settings().setValue("loglevel",
myLogLevel->getSelectedTag().toString());
instance().settings().setValue("logtoconsole", myLogToConsole->getState());
int loglevel = myLogLevel->getSelectedTag().toInt();
bool logtoconsole = myLogToConsole->getState();
instance().settings().setValue("loglevel", loglevel);
instance().settings().setValue("logtoconsole", logtoconsole);
Logger::instance().setLogParameters(loglevel, logtoconsole);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -113,7 +118,7 @@ void LoggerDialog::saveLogFile()
ofstream out(node.getPath());
if(out.is_open())
{
out << instance().logMessages();
out << Logger::instance().logMessages();
instance().frameBuffer().showMessage("Saving log file to " + path.str());
}
}