Skip to content

Commit

Permalink
Update to not crash if not writable
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskr97 committed Jul 16, 2021
1 parent c071278 commit d227edd
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/Core/Misc/PlogLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,38 @@ class EtternaFormatter {
};

PlogLogger::PlogLogger() {
// Console Appender. One for windows, and another for other operating systems.
#ifdef _WIN32
static plog::WindowsAppender<EtternaFormatter> consoleAppender;
#else
static plog::ColorConsoleAppender<EtternaFormatter> consoleAppender;
#endif
plog::init(plog::Severity::info, &consoleAppender);


// Get current time for log file name. Format: YYYY_MM_DD-HH_MM_SS.log
char timeString[20]; // Date and time portion only
std::time_t t = std::time(nullptr);
std::strftime(timeString, sizeof(timeString), "%Y_%m_%d-%H_%M_%S", std::localtime(&t));
auto logDirectory = Core::Platform::getAppDirectory() / "Logs";

// Ensure log directory exists before initializing logger.
if(!ghc::filesystem::exists(logDirectory))
ghc::filesystem::create_directory(logDirectory);
namespace fs = ghc::filesystem;
// Ensure log directory exists and is writable before initializing logger.
auto appDirPerms = fs::status(Core::Platform::getAppDirectory()).permissions();
auto writable = (appDirPerms & (fs::perms::owner_write )) != fs::perms::none;

// If not writable, only output to console.
if(!writable)
return;

if(!fs::exists(logDirectory))
fs::create_directory(logDirectory);

auto logFilePath = logDirectory / fmt::format(FMT_STRING("{}.log"), timeString);

// File Appender
static plog::RollingFileAppender<EtternaFormatter, plog::UTF8Converter> rollingFileAppender{logFilePath.c_str()};
plog::init(plog::Severity::info, &rollingFileAppender);

// Console Appender. One for windows, and another for other operating systems.
#ifdef _WIN32
static plog::WindowsAppender<EtternaFormatter> consoleAppender;
#else
static plog::ColorConsoleAppender<EtternaFormatter> consoleAppender;
#endif
plog::init(plog::Severity::info, &consoleAppender);
}

void PlogLogger::log(Core::ILogger::Severity logLevel, const std::string_view message) {
Expand Down

0 comments on commit d227edd

Please sign in to comment.