Skip to content

Commit

Permalink
SDL: Simplify implementation of createLogFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
ccawley2011 authored and bluegr committed Aug 5, 2019
1 parent d10c69d commit 8b8fb6d
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 114 deletions.
8 changes: 8 additions & 0 deletions backends/platform/samsungtv/samsungtv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ void OSystem_SDL_SamsungTV::fatalError() {
for (;;) {}
}

Common::String OSystem_SDL_SamsungTV::getDefaultLogFileName() {
if (!Posix::assureDirectoryExists("/mtd_ram", nullptr)) {
return Common::String();
}

return "/mtd_ram/scummvm.log";
}

#endif
3 changes: 3 additions & 0 deletions backends/platform/samsungtv/samsungtv.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class OSystem_SDL_SamsungTV : public OSystem_POSIX {
virtual void initBackend();
virtual void quit();
virtual void fatalError();

protected:
virtual Common::String getDefaultLogFileName();
};

#endif
Expand Down
14 changes: 14 additions & 0 deletions backends/platform/sdl/macosx/macosx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "backends/taskbar/macosx/macosx-taskbar.h"
#include "backends/dialogs/macosx/macosx-dialogs.h"
#include "backends/platform/sdl/macosx/macosx_wrapper.h"
#include "backends/fs/posix/posix-fs.h"

#include "common/archive.h"
#include "common/config-manager.h"
Expand Down Expand Up @@ -198,6 +199,19 @@ Common::String OSystem_MacOSX::getSystemLanguage() const {
#endif // USE_DETECTLANG
}

Common::String OSystem_MacOSX::getDefaultLogFileName() {
const char *prefix = getenv("HOME");
if (prefix == nullptr) {
return Common::String();
}

if (!Posix::assureDirectoryExists("Library/Logs", prefix)) {
return Common::String();
}

return Common::String(prefix) + "/Library/Logs/scummvm.log";
}

Common::String OSystem_MacOSX::getScreenshotsPath() {
Common::String path = ConfMan.get("screenshotpath");
if (path.empty())
Expand Down
2 changes: 2 additions & 0 deletions backends/platform/sdl/macosx/macosx.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class OSystem_MacOSX : public OSystem_POSIX {
virtual Common::String getScreenshotsPath();

protected:
virtual Common::String getDefaultLogFileName();

// Override createAudioCDManager() to get our Mac-specific
// version.
virtual AudioCDManager *createAudioCDManager();
Expand Down
36 changes: 5 additions & 31 deletions backends/platform/sdl/posix/posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,55 +269,29 @@ void OSystem_POSIX::addSysArchivesToSearchSet(Common::SearchSet &s, int priority
OSystem_SDL::addSysArchivesToSearchSet(s, priority);
}

Common::WriteStream *OSystem_POSIX::createLogFile() {
// Start out by resetting _logFilePath, so that in case
// of a failure, we know that no log file is open.
_logFilePath.clear();

const char *prefix = nullptr;
Common::String OSystem_POSIX::getDefaultLogFileName() {
Common::String logFile;
#ifdef MACOSX
prefix = getenv("HOME");
if (prefix == nullptr) {
return 0;
}

logFile = "Library/Logs";
#elif SAMSUNGTV
prefix = nullptr;
logFile = "/mtd_ram";
#else
// On POSIX systems we follow the XDG Base Directory Specification for
// where to store files. The version we based our code upon can be found
// over here: http://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
prefix = getenv("XDG_CACHE_HOME");
const char *prefix = getenv("XDG_CACHE_HOME");
if (prefix == nullptr || !*prefix) {
prefix = getenv("HOME");
if (prefix == nullptr) {
return 0;
return Common::String();
}

logFile = ".cache/";
}

logFile += "scummvm/logs";
#endif

if (!Posix::assureDirectoryExists(logFile, prefix)) {
return 0;
return Common::String();
}

if (prefix) {
logFile = Common::String::format("%s/%s", prefix, logFile.c_str());
}

logFile += "/scummvm.log";

Common::FSNode file(logFile);
Common::WriteStream *stream = file.createWriteStream();
if (stream)
_logFilePath = logFile;
return stream;
return Common::String::format("%s/%s/scummvm.log", prefix, logFile.c_str());
}

bool OSystem_POSIX::displayLogFile() {
Expand Down
13 changes: 1 addition & 12 deletions backends/platform/sdl/posix/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,8 @@ class OSystem_POSIX : public OSystem_SDL {
*/
Common::String _baseConfigName;

/**
* The path of the currently open log file, if any.
*
* @note This is currently a string and not an FSNode for simplicity;
* e.g. we don't need to include fs.h here, and currently the
* only use of this value is to use it to open the log file in an
* editor; for that, we need it only as a string anyway.
*/
Common::String _logFilePath;

virtual Common::String getDefaultConfigFileName();

virtual Common::WriteStream *createLogFile();
virtual Common::String getDefaultLogFileName();

Common::String getXdgUserDir(const char *name);

Expand Down
5 changes: 2 additions & 3 deletions backends/platform/sdl/ps3/ps3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ Common::String OSystem_PS3::getDefaultConfigFileName() {
return PREFIX "/" + _baseConfigName;
}

Common::WriteStream *OSystem_PS3::createLogFile() {
Common::FSNode file(PREFIX "/scummvm.log");
return file.createWriteStream();
Common::String OSystem_PS3::getDefaultLogFileName() {
return PREFIX "/scummvm.log";
}
3 changes: 1 addition & 2 deletions backends/platform/sdl/ps3/ps3.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class OSystem_PS3 : public OSystem_SDL {
Common::String _baseConfigName;

virtual Common::String getDefaultConfigFileName();

virtual Common::WriteStream *createLogFile();
virtual Common::String getDefaultLogFileName();
};

#endif
5 changes: 2 additions & 3 deletions backends/platform/sdl/psp2/psp2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ Common::String OSystem_PSP2::getDefaultConfigFileName() {
return "ux0:data/scummvm/" + _baseConfigName;
}

Common::WriteStream *OSystem_PSP2::createLogFile() {
Common::FSNode file("ux0:data/scummvm/scummvm.log");
return file.createWriteStream();
Common::String OSystem_PSP2::getDefaultLogFileName() {
return "ux0:data/scummvm/scummvm.log";
}
3 changes: 1 addition & 2 deletions backends/platform/sdl/psp2/psp2.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class OSystem_PSP2 : public OSystem_SDL {
Common::String _baseConfigName;

virtual Common::String getDefaultConfigFileName() override;

virtual Common::WriteStream *createLogFile() override;
virtual Common::String getDefaultLogFileName() override;
};

#endif
16 changes: 3 additions & 13 deletions backends/platform/sdl/riscos/riscos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,14 @@ Common::String OSystem_RISCOS::getDefaultConfigFileName() {
return "/<Choices$Write>/ScummVM/scummvmrc";
}

Common::WriteStream *OSystem_RISCOS::createLogFile() {
// Start out by resetting _logFilePath, so that in case
// of a failure, we know that no log file is open.
_logFilePath.clear();

Common::String OSystem_RISCOS::getDefaultLogFileName() {
Common::String logFile = "/<Choices$Write>/ScummVM/Logs";

if (!Riscos::assureDirectoryExists(logFile)) {
return 0;
return Common::String();
}

logFile += "/scummvm";

Common::FSNode file(logFile);
Common::WriteStream *stream = file.createWriteStream();
if (stream)
_logFilePath = logFile;
return stream;
return logFile + "/scummvm";
}

#endif
Expand Down
12 changes: 1 addition & 11 deletions backends/platform/sdl/riscos/riscos.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,8 @@ class OSystem_RISCOS : public OSystem_SDL {
virtual void logMessage(LogMessageType::Type type, const char *message);

protected:
/**
* The path of the currently open log file, if any.
*
* @note This is currently a string and not an FSNode for simplicity;
* e.g. we don't need to include fs.h here, and currently the
* only use of this value is to use it to open the log file in an
* editor; for that, we need it only as a string anyway.
*/
Common::String _logFilePath;

virtual Common::String getDefaultConfigFileName();
virtual Common::WriteStream *createLogFile();
virtual Common::String getDefaultLogFileName();
};

#endif
16 changes: 16 additions & 0 deletions backends/platform/sdl/sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,22 @@ void OSystem_SDL::logMessage(LogMessageType::Type type, const char *message) {
_logger->print(message);
}

Common::WriteStream *OSystem_SDL::createLogFile() {
// Start out by resetting _logFilePath, so that in case
// of a failure, we know that no log file is open.
_logFilePath.clear();

Common::String logFile = getDefaultLogFileName();
if (logFile.empty())
return nullptr;

Common::FSNode file(logFile);
Common::WriteStream *stream = file.createWriteStream();
if (stream)
_logFilePath = logFile;
return stream;
}

Common::String OSystem_SDL::getSystemLanguage() const {
#if defined(USE_DETECTLANG) && !defined(WIN32)
// Activating current locale settings
Expand Down
13 changes: 12 additions & 1 deletion backends/platform/sdl/sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ class OSystem_SDL : public ModularBackend {
bool _initedSDLnet;
#endif

/**
* The path of the currently open log file, if any.
*
* @note This is currently a string and not an FSNode for simplicity;
* e.g. we don't need to include fs.h here, and currently the
* only use of this value is to use it to open the log file in an
* editor; for that, we need it only as a string anyway.
*/
Common::String _logFilePath;

/**
* Mixer manager that configures and setups SDL for
* the wrapped Audio::Mixer, the true mixer.
Expand Down Expand Up @@ -122,7 +132,8 @@ class OSystem_SDL : public ModularBackend {
virtual AudioCDManager *createAudioCDManager();

// Logging
virtual Common::WriteStream *createLogFile() { return 0; }
virtual Common::String getDefaultLogFileName() { return Common::String(); }
virtual Common::WriteStream *createLogFile();
Backends::Log::Log *_logger;

#ifdef USE_OPENGL
Expand Down
5 changes: 2 additions & 3 deletions backends/platform/sdl/switch/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ Common::String OSystem_Switch::getDefaultConfigFileName() {
return _baseConfigName;
}

Common::WriteStream *OSystem_Switch::createLogFile() {
Common::FSNode file("scummvm.log");
return file.createWriteStream();
Common::String OSystem_Switch::getDefaultLogFileName() {
return "scummvm.log";
}
3 changes: 1 addition & 2 deletions backends/platform/sdl/switch/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class OSystem_Switch : public OSystem_SDL {
Common::String _baseConfigName;

virtual Common::String getDefaultConfigFileName() override;

virtual Common::WriteStream *createLogFile() override;
virtual Common::String getDefaultLogFileName() override;
};

#endif
Expand Down
31 changes: 11 additions & 20 deletions backends/platform/sdl/win32/win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,31 +278,22 @@ Common::String OSystem_Win32::getDefaultConfigFileName() {
return configFile;
}

Common::WriteStream *OSystem_Win32::createLogFile() {
// Start out by resetting _logFilePath, so that in case
// of a failure, we know that no log file is open.
_logFilePath.clear();

Common::String OSystem_Win32::getDefaultLogFileName() {
char logFile[MAXPATHLEN];

// Use the Application Data directory of the user profile.
if (SHGetFolderPathFunc(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, logFile) == S_OK) {
strcat(logFile, "\\ScummVM");
CreateDirectory(logFile, NULL);
strcat(logFile, "\\Logs");
CreateDirectory(logFile, NULL);
strcat(logFile, "\\scummvm.log");

Common::FSNode file(logFile);
Common::WriteStream *stream = file.createWriteStream();
if (stream)
_logFilePath= logFile;

return stream;
} else {
if (SHGetFolderPathFunc(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, logFile) != S_OK) {
warning("Unable to access application data directory");
return 0;
return Common::String();
}

strcat(logFile, "\\ScummVM");
CreateDirectory(logFile, NULL);
strcat(logFile, "\\Logs");
CreateDirectory(logFile, NULL);
strcat(logFile, "\\scummvm.log");

return logFile;
}

namespace {
Expand Down
12 changes: 1 addition & 11 deletions backends/platform/sdl/win32/win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,8 @@ class OSystem_Win32 : public OSystem_SDL {
virtual Common::String getScreenshotsPath();

protected:
/**
* The path of the currently open log file, if any.
*
* @note This is currently a string and not an FSNode for simplicity;
* e.g. we don't need to include fs.h here, and currently the
* only use of this value is to use it to open the log file in an
* editor; for that, we need it only as a string anyway.
*/
Common::String _logFilePath;

virtual Common::String getDefaultConfigFileName();
virtual Common::WriteStream *createLogFile();
virtual Common::String getDefaultLogFileName();

// Override createAudioCDManager() to get our Mac-specific
// version.
Expand Down

0 comments on commit 8b8fb6d

Please sign in to comment.