diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f739575..4576ae53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/include/loot/api.h b/include/loot/api.h index 33ba78ce..35b8bd4b 100644 --- a/include/loot/api.h +++ b/include/loot/api.h @@ -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 diff --git a/include/loot/enum/log_verbosity.h b/include/loot/enum/log_verbosity.h new file mode 100644 index 00000000..8208db27 --- /dev/null +++ b/include/loot/enum/log_verbosity.h @@ -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 +. +*/ + +#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 diff --git a/src/api/api.cpp b/src/api/api.cpp index 5d2a2736..e49201b2 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -26,6 +26,11 @@ #include #include +#include +#include +#include +#include +#include #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 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))