diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d11147f5..81959d33 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,6 +36,7 @@ add_subdirectory(libexprtk) add_subdirectory(core) add_subdirectory(shellextension) add_subdirectory(api) +add_subdirectory(logger/glog) if(SHELLANYTHING_BUILD_PLUGINS) add_subdirectory(plugins/sa_plugin_process) diff --git a/src/logger/glog/CMakeLists.txt b/src/logger/glog/CMakeLists.txt new file mode 100644 index 00000000..03c05a3f --- /dev/null +++ b/src/logger/glog/CMakeLists.txt @@ -0,0 +1,45 @@ +add_library(sa.logger.glog SHARED + LoggerGlog.cpp + LoggerGlog.h +) + +# Force CMAKE_DEBUG_POSTFIX for executables +set_target_properties(sa.logger.glog PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}) + +set(SA_GLOG_EXPORT_HEADER_FILENAME "sa_glog_export.h") +set(SA_GLOG_EXPORT_HEADER ${CMAKE_BINARY_DIR}/src/logger/glog/${SA_GLOG_EXPORT_HEADER_FILENAME}) +message("Generating ${SA_GLOG_EXPORT_HEADER_FILENAME} for shared library...") +include (GenerateExportHeader) +GENERATE_EXPORT_HEADER( sa.logger.glog + BASE_NAME sa_glog + EXPORT_MACRO_NAME SA_GLOG_EXPORT + EXPORT_FILE_NAME ${SA_GLOG_EXPORT_HEADER} +) + +# Define include directories for the library. +target_include_directories(sa.logger.glog + PUBLIC + $ # for clients using the installed library. + PRIVATE + rapidassist + glog::glog + ${CMAKE_SOURCE_DIR}/src/core + ${CMAKE_BINARY_DIR}/src/logger/glog +) + +# Define linking dependencies. +add_dependencies(sa.logger.glog sa.core) +target_link_libraries(sa.logger.glog + PRIVATE + rapidassist + glog::glog + sa.core +) + +# Define files that will be part of the installation package. +install(TARGETS sa.logger.glog + EXPORT sa-glog-targets + ARCHIVE DESTINATION ${SHELLANYTHING_INSTALL_LIB_DIR} + LIBRARY DESTINATION ${SHELLANYTHING_INSTALL_LIB_DIR} + RUNTIME DESTINATION ${SHELLANYTHING_INSTALL_BIN_DIR} +) diff --git a/src/logger/glog/LoggerGlog.cpp b/src/logger/glog/LoggerGlog.cpp new file mode 100644 index 00000000..70fc2f9f --- /dev/null +++ b/src/logger/glog/LoggerGlog.cpp @@ -0,0 +1,74 @@ +/********************************************************************************** + * MIT License + * + * Copyright (c) 2018 Antoine Beauchamp + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *********************************************************************************/ + +#include "LoggerGlog.h" + +#pragma warning( push ) +#pragma warning( disable: 4355 ) // glog\install_dir\include\glog/logging.h(1167): warning C4355: 'this' : used in base member initializer list +#include +#pragma warning( pop ) + +namespace shellanything +{ + + inline ::google::LogSeverity to_severity(const ILogger::LOG_LEVEL& level) + { + switch (level) + { + default: + case ILogger::LOG_LEVEL_DEBUG: + case ILogger::LOG_LEVEL_INFO: + return ::google::GLOG_INFO; + break; + case ILogger::LOG_LEVEL_WARNING: + return ::google::GLOG_WARNING; + break; + case ILogger::LOG_LEVEL_ERROR: + return ::google::GLOG_ERROR; + break; + case ILogger::LOG_LEVEL_FATAL: + return ::google::GLOG_FATAL; + break; + }; + } + + LoggerGlog::LoggerGlog() + { + } + + LoggerGlog::~LoggerGlog() + { + } + + void LoggerGlog::LogMessage(const char* filename, int line, const ILogger::LOG_LEVEL& level, const char* message) + { + ::google::LogMessage(filename, line, to_severity(level)).stream() << message; + } + + void LoggerGlog::LogMessage(const ILogger::LOG_LEVEL& level, const char* message) + { + ::google::LogMessage(__FILE__, __LINE__, to_severity(level)).stream() << message; + } + +} //namespace shellanything diff --git a/src/logger/glog/LoggerGlog.h b/src/logger/glog/LoggerGlog.h new file mode 100644 index 00000000..80349681 --- /dev/null +++ b/src/logger/glog/LoggerGlog.h @@ -0,0 +1,68 @@ +/********************************************************************************** + * MIT License + * + * Copyright (c) 2018 Antoine Beauchamp + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *********************************************************************************/ + +#ifndef SA_LOGGER_GLOG_H +#define SA_LOGGER_GLOG_H + +#include "sa_glog_export.h" +#include "ILogger.h" + +namespace shellanything +{ + /// + /// Abstract logger class. + /// + class SA_GLOG_EXPORT LoggerGlog : public virtual ILogger + { + public: + LoggerGlog(); + virtual ~LoggerGlog(); + + private: + // Disable and copy constructor, dtor and copy operator + LoggerGlog(const LoggerGlog&); + LoggerGlog& operator=(const LoggerGlog&); + public: + + /// + /// Send a message to this logger. + /// + /// The originating source code file name. + /// The line number that producing this message. + /// The log level of the message. + /// The actual message. + virtual void LogMessage(const char* filename, int line, const ILogger::LOG_LEVEL & level, const char* message); + + /// + /// Send a message to this logger. + /// + /// The log level of the message. + /// The actual message. + virtual void LogMessage(const ILogger::LOG_LEVEL & level, const char* message); + + }; + +} //namespace shellanything + +#endif //SA_LOGGER_GLOG_H diff --git a/src/shellextension/CMakeLists.txt b/src/shellextension/CMakeLists.txt index 50e27d6d..89635787 100644 --- a/src/shellextension/CMakeLists.txt +++ b/src/shellextension/CMakeLists.txt @@ -33,6 +33,8 @@ target_include_directories(sa.shellextension libmagic ${CMAKE_SOURCE_DIR}/src/shared ${CMAKE_SOURCE_DIR}/src/core + ${CMAKE_SOURCE_DIR}/src/logger/glog + ${CMAKE_BINARY_DIR}/src/logger/glog ) # Define linking dependencies. @@ -40,6 +42,7 @@ target_link_libraries(sa.shellextension PRIVATE sa.core sa.shared + sa.logger.glog rapidassist glog::glog libmagic diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 8e666f8d..7438734e 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -130,6 +130,8 @@ target_include_directories(sa.tests ${CMAKE_SOURCE_DIR}/src/libexprtk ${CMAKE_SOURCE_DIR}/src/shared ${CMAKE_SOURCE_DIR}/src/core + ${CMAKE_SOURCE_DIR}/src/logger/glog + ${CMAKE_BINARY_DIR}/src/logger/glog ) # Define linking dependencies. @@ -140,6 +142,7 @@ target_link_libraries(sa.tests sa.core sa.api sa.shellextension + sa.logger.glog ${PTHREAD_LIBRARIES} ${GTEST_LIBRARIES} rapidassist diff --git a/src/tests/main.cpp b/src/tests/main.cpp index aa6e45e4..78d7b398 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -47,6 +47,11 @@ #include "PropertyManager.h" #include "Workspace.h" +#include "ILogger.h" +#include "LoggerManager.h" +#include "LoggerHelper.h" +#include "LoggerGlog.h" + using namespace ra; int SetTestPreferedRootDirectory() @@ -113,6 +118,11 @@ int main(int argc, char** argv) // Initialize Google's logging library. shellanything::InitLogger(); + // Setup an active logger in ShellAnything's core. + shellanything::ILogger* glog_logger = new shellanything::LoggerGlog(); + shellanything::LoggerManager& lm = shellanything::LoggerManager::GetInstance(); + lm.SetLogger(glog_logger); + //Issue #60 - Unit tests cannot execute from installation directory. //Create log directory under the current executable. //When running tests from a developer environment, the log directory is expected to have write access.