Skip to content

Commit

Permalink
Created class LoggerGlog which is an implementation of ILogger. #137
Browse files Browse the repository at this point in the history
  • Loading branch information
end2endzone committed Dec 3, 2023
1 parent afa8af9 commit 2d2b43f
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions src/logger/glog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<INSTALL_INTERFACE:${SHELLANYTHING_INSTALL_INCLUDE_DIR}> # 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}
)
74 changes: 74 additions & 0 deletions src/logger/glog/LoggerGlog.cpp
Original file line number Diff line number Diff line change
@@ -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 <glog/logging.h>
#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
68 changes: 68 additions & 0 deletions src/logger/glog/LoggerGlog.h
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Abstract logger class.
/// </summary>
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:

/// <summary>
/// Send a message to this logger.
/// </summary>
/// <param name="filename">The originating source code file name.</param>
/// <param name="line">The line number that producing this message.</param>
/// <param name="level">The log level of the message.</param>
/// <param name="message">The actual message.</param>
virtual void LogMessage(const char* filename, int line, const ILogger::LOG_LEVEL & level, const char* message);

/// <summary>
/// Send a message to this logger.
/// </summary>
/// <param name="level">The log level of the message.</param>
/// <param name="message">The actual message.</param>
virtual void LogMessage(const ILogger::LOG_LEVEL & level, const char* message);

};

} //namespace shellanything

#endif //SA_LOGGER_GLOG_H
3 changes: 3 additions & 0 deletions src/shellextension/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ 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.
target_link_libraries(sa.shellextension
PRIVATE
sa.core
sa.shared
sa.logger.glog
rapidassist
glog::glog
libmagic
Expand Down
3 changes: 3 additions & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -140,6 +142,7 @@ target_link_libraries(sa.tests
sa.core
sa.api
sa.shellextension
sa.logger.glog
${PTHREAD_LIBRARIES}
${GTEST_LIBRARIES}
rapidassist
Expand Down
10 changes: 10 additions & 0 deletions src/tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 2d2b43f

Please sign in to comment.