Skip to content
Snippets Groups Projects
Commit a061c858 authored by Gigg, Martyn Anthony's avatar Gigg, Martyn Anthony
Browse files

Simplify log file handling

For cases where file logging is enabled then the framework simply uses the
specified path as is or defaults to the appdata directory on all
platforms. This has the advantage of moving it outside if the install
directory.
parent a059de2f
No related branches found
No related tags found
No related merge requests found
...@@ -382,88 +382,26 @@ void ConfigServiceImpl::registerLoggingFilterChannel( ...@@ -382,88 +382,26 @@ void ConfigServiceImpl::registerLoggingFilterChannel(
* *
*/ */
void ConfigServiceImpl::configureLogging() { void ConfigServiceImpl::configureLogging() {
try { // Undocumented way to override the mantid.log path
// Ensure that the logging directory exists if (Poco::Environment::has("MANTIDLOGPATH")) {
m_logFilePath = getString("logging.channels.fileChannel.path"); auto logpath = Poco::Path(Poco::Environment::get("MANTIDLOGPATH"));
Poco::Path logpath(m_logFilePath);
// Undocumented way to override the mantid.log path
if (Poco::Environment::has("MANTIDLOGPATH")) {
logpath = Poco::Path(Poco::Environment::get("MANTIDLOGPATH"));
logpath = logpath.absolute();
m_logFilePath = logpath.toString();
}
// An absolute path makes things simpler
logpath = logpath.absolute(); logpath = logpath.absolute();
m_logFilePath = logpath.toString();
// First, try the logpath given
if (!m_logFilePath.empty()) {
try {
// Save it for later
m_logFilePath = logpath.toString();
// make this path point to the parent directory and create it if it does
// not exist
Poco::Path parent = logpath;
parent.makeParent();
Poco::File(parent).createDirectories();
// Try to create or append to the file. If it fails, use the default
FILE *fp = fopen(m_logFilePath.c_str(), "a+");
if (fp == nullptr) {
std::cerr
<< "Error writing to log file path given in properties file: \""
<< m_logFilePath << "\". Will use a default path instead.\n";
// Clear the path; this will make it use the default
m_logFilePath = "";
} else
fclose(fp);
} catch (std::exception &) {
std::cerr
<< "Error writing to log file path given in properties file: \""
<< m_logFilePath << "\". Will use a default path instead.\n";
// ERROR! Maybe the file is not writable!
// Clear the path; this will make it use the default
m_logFilePath = "";
}
}
// The path given was invalid somehow? Use a default
if (m_logFilePath.empty()) {
m_logFilePath = getUserPropertiesDir() + "mantid.log";
// Check whether the file can be written. The Poco::File::canWrite method
// does not work
// for files that don't exist, it throws an exception. It also can't be
// used to check for
// directory access as the Windows API doesn't return this information
// correctly for
// directories.
FILE *fp = fopen(m_logFilePath.c_str(), "a+");
if (!fp) {
// if we cannot write to the default directory then set use the system
// temp
logpath = Poco::Path::temp() + "mantid.log";
m_logFilePath = logpath.toString();
std::cerr << "Error writing to log file path to default location: \""
<< m_logFilePath
<< "\". Will use a system temp path instead: \""
<< m_logFilePath << "\"\n";
} else
fclose(fp);
}
// Set the line in the configuration properties. // Set the line in the configuration properties.
// this'll be picked up by LoggingConfigurator (somehow)
m_pConf->setString("logging.channels.fileChannel.path", m_logFilePath); m_pConf->setString("logging.channels.fileChannel.path", m_logFilePath);
} else {
// make this path point to the parent directory and create it if it does not m_logFilePath = getString("logging.channels.fileChannel.path");
// exist if (m_logFilePath.empty()) {
logpath.makeParent(); // Default to appdata/mantid.log
if (!logpath.toString().empty()) { Poco::Path path(getAppDataDir());
Poco::File(logpath) path.append("mantid.log");
.createDirectories(); // Also creates all necessary directories m_logFilePath = path.toString();
// Set the line in the configuration properties.
m_pConf->setString("logging.channels.fileChannel.path", m_logFilePath);
} }
}
try {
// Configure the logging framework // Configure the logging framework
Poco::Util::LoggingConfigurator configurator; Poco::Util::LoggingConfigurator configurator;
configurator.configure(m_pConf); configurator.configure(m_pConf);
...@@ -471,7 +409,6 @@ void ConfigServiceImpl::configureLogging() { ...@@ -471,7 +409,6 @@ void ConfigServiceImpl::configureLogging() {
std::cerr << "Trouble configuring the logging framework " << e.what() std::cerr << "Trouble configuring the logging framework " << e.what()
<< '\n'; << '\n';
} }
// register the filter channels - the order here is important // register the filter channels - the order here is important
registerLoggingFilterChannel("fileFilterChannel", nullptr); registerLoggingFilterChannel("fileFilterChannel", nullptr);
registerLoggingFilterChannel("consoleFilterChannel", nullptr); registerLoggingFilterChannel("consoleFilterChannel", nullptr);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment