Add logging controls to the API

Control verbosity and choose a file to output to.
This commit is contained in:
Oliver Hamlet
2017-02-06 18:03:17 +00:00
parent 5dae27ce87
commit f353cc7bda
4 changed files with 112 additions and 3 deletions
+1
View File
@@ -200,6 +200,7 @@ set (LOOT_API_HEADERS "${CMAKE_SOURCE_DIR}/include/loot/api.h"
"${CMAKE_SOURCE_DIR}/include/loot/exception/git_state_error.h"
"${CMAKE_SOURCE_DIR}/include/loot/enum/game_type.h"
"${CMAKE_SOURCE_DIR}/include/loot/enum/language_code.h"
"${CMAKE_SOURCE_DIR}/include/loot/enum/log_verbosity.h"
"${CMAKE_SOURCE_DIR}/include/loot/enum/message_type.h"
"${CMAKE_SOURCE_DIR}/include/loot/enum/plugin_cleanliness.h"
"${CMAKE_SOURCE_DIR}/include/loot/game_interface.h"
+24
View File
@@ -37,9 +37,33 @@
#include "loot/exception/game_detection_error.h"
#include "loot/exception/git_state_error.h"
#include "loot/enum/game_type.h"
#include "loot/enum/log_verbosity.h"
#include "loot/loot_version.h"
namespace loot {
/**@}*/
/**********************************************************************//**
* @name Logging Functions
*************************************************************************/
/**@{*/
/**
* @brief Set the API's logging verbosity.
* @details The default is ``LogVerbosity::off``.
* @param verbosity
* The logging verbosity to set.
*/
LOOT_API void SetLoggingVerbosity(LogVerbosity verbosity);
/**
* @brief Set the file path that logging statements are written to.
* @details If no file is set the default behaviour is to print logging
* statements to the console.
* @param path
* The log file path.
*/
LOOT_API void SetLogFile(const std::string& path);
/**@}*/
/**********************************************************************//**
* @name Version Functions
+48
View File
@@ -0,0 +1,48 @@
/* LOOT
A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and
Fallout: New Vegas.
Copyright (C) 2012-2016 WrinklyNinja
This file is part of LOOT.
LOOT is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
LOOT is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LOOT. If not, see
<https://www.gnu.org/licenses/>.
*/
#ifndef LOOT_LOG_VERBOSITY
#define LOOT_LOG_VERBOSITY
/**
* The namespace used by the LOOT API.
*/
namespace loot {
/**
* @brief Codes used to specify the preferred language for messages when
* evaluating masterlists and userlists.
* @details If a message is not available in the preferred language, its English
* string will be used. Note that messages with only one language
* string are assumed to be written in English, but this cannot be
* guaranteed (any violations should be reported as bugs so that they
* can be fixed).
*/
enum struct LogVerbosity : unsigned int {
off,
warning,
trace,
};
}
#endif
+39 -3
View File
@@ -26,6 +26,11 @@
#include <boost/filesystem.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>
#include "api/game/game.h"
@@ -39,6 +44,40 @@ std::string ResolvePath(const std::string& path) {
return fs::read_symlink(path).string();
}
LOOT_API void SetLoggingVerbosity(LogVerbosity verbosity) {
switch (verbosity) {
case LogVerbosity::off:
boost::log::core::get()->set_logging_enabled(false);
break;
case LogVerbosity::warning:
boost::log::core::get()->set_filter(boost::log::trivial::severity > boost::log::trivial::warning);
boost::log::core::get()->set_logging_enabled(true);
break;
case LogVerbosity::trace:
boost::log::core::get()->reset_filter();
boost::log::core::get()->set_logging_enabled(true);
break;
}
}
LOOT_API void SetLogFile(const std::string& path) {
// Set the locale to get UTF-8 conversions working correctly.
std::locale::global(boost::locale::generator().generate(""));
boost::filesystem::path::imbue(std::locale());
boost::log::add_file_log(
boost::log::keywords::file_name = path,
boost::log::keywords::auto_flush = true,
boost::log::keywords::format = (
boost::log::expressions::stream
<< "[" << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%H:%M:%S") << "]"
<< " [" << boost::log::trivial::severity << "]: "
<< boost::log::expressions::smessage
)
);
boost::log::add_common_attributes();
}
LOOT_API bool IsCompatible(const unsigned int versionMajor, const unsigned int versionMinor, const unsigned int versionPatch) {
if (versionMajor > 0)
return versionMajor == loot::LootVersion::major;
@@ -53,9 +92,6 @@ LOOT_API std::shared_ptr<GameInterface> CreateGameHandle(const GameType game,
std::locale::global(boost::locale::generator().generate(""));
boost::filesystem::path::imbue(std::locale());
//Disable logging or else stdout will get overrun.
boost::log::core::get()->set_logging_enabled(false);
// Check for valid paths.
const std::string resolvedGamePath = ResolvePath(gamePath);
if (!gamePath.empty() && !fs::is_directory(resolvedGamePath))