added gmock support

This commit is contained in:
Sergiu Deitsch 2021-10-07 18:24:45 +02:00
parent 831a6f8232
commit 8474b7854b
9 changed files with 204 additions and 74 deletions

View File

@ -17,18 +17,41 @@ set (CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set (CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) set (CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set (CPACK_PACKAGE_VERSION ${PROJECT_VERSION}) set (CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include (CheckCXXCompilerFlag)
include (CheckCXXSourceCompiles)
include (CheckCXXSourceRuns)
include (CheckCXXSymbolExists)
include (CheckFunctionExists)
include (CheckIncludeFileCXX)
include (CheckLibraryExists)
include (CheckStructHasMember)
include (CheckTypeSize)
include (CMakeDependentOption)
include (CMakePackageConfigHelpers)
include (CMakePushCheckState)
include (CPack)
include (CTest)
include (DetermineGflagsNamespace)
include (GenerateExportHeader)
include (GetCacheVariables)
include (GNUInstallDirs)
option (BUILD_SHARED_LIBS "Build shared libraries" ON) option (BUILD_SHARED_LIBS "Build shared libraries" ON)
option (PRINT_UNSYMBOLIZED_STACK_TRACES option (PRINT_UNSYMBOLIZED_STACK_TRACES
"Print file offsets in traces instead of symbolizing" OFF) "Print file offsets in traces instead of symbolizing" OFF)
option (WITH_CUSTOM_PREFIX "Enable support for user-generated message prefixes" OFF) option (WITH_CUSTOM_PREFIX "Enable support for user-generated message prefixes" OFF)
option (WITH_GFLAGS "Use gflags" ON) option (WITH_GFLAGS "Use gflags" ON)
option (WITH_GTEST "Use googletest" ON) option (WITH_GTEST "Use Google Test" ON)
option (WITH_PKGCONFIG "Enable pkg-config support" ON) option (WITH_PKGCONFIG "Enable pkg-config support" ON)
option (WITH_SYMBOLIZE "Enable symbolize module" ON) option (WITH_SYMBOLIZE "Enable symbolize module" ON)
option (WITH_THREADS "Enable multithreading support" ON) option (WITH_THREADS "Enable multithreading support" ON)
option (WITH_TLS "Enable Thread Local Storage (TLS) support" ON) option (WITH_TLS "Enable Thread Local Storage (TLS) support" ON)
option (WITH_UNWIND "Enable libunwind support" ON) option (WITH_UNWIND "Enable libunwind support" ON)
cmake_dependent_option (WITH_GMOCK "Use Google Mock" ON WITH_GTEST OFF)
if (NOT WITH_UNWIND) if (NOT WITH_UNWIND)
set (CMAKE_DISABLE_FIND_PACKAGE_Unwind ON) set (CMAKE_DISABLE_FIND_PACKAGE_Unwind ON)
endif (NOT WITH_UNWIND) endif (NOT WITH_UNWIND)
@ -46,35 +69,19 @@ set (CMAKE_CXX_VISIBILITY_PRESET hidden)
set (CMAKE_POSITION_INDEPENDENT_CODE ON) set (CMAKE_POSITION_INDEPENDENT_CODE ON)
set (CMAKE_VISIBILITY_INLINES_HIDDEN ON) set (CMAKE_VISIBILITY_INLINES_HIDDEN ON)
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include (CheckCXXCompilerFlag)
include (CheckCXXSourceCompiles)
include (CheckCXXSourceRuns)
include (CheckCXXSymbolExists)
include (CheckFunctionExists)
include (CheckIncludeFileCXX)
include (CheckLibraryExists)
include (CheckStructHasMember)
include (CheckTypeSize)
include (CMakePackageConfigHelpers)
include (CMakePushCheckState)
include (CPack)
include (CTest)
include (DetermineGflagsNamespace)
include (GenerateExportHeader)
include (GetCacheVariables)
include (GNUInstallDirs)
set (CMAKE_DEBUG_POSTFIX d) set (CMAKE_DEBUG_POSTFIX d)
set (CMAKE_THREAD_PREFER_PTHREAD 1) set (CMAKE_THREAD_PREFER_PTHREAD 1)
find_package (GTest) find_package (GTest NO_MODULE)
if (GTest_FOUND) if (GTest_FOUND)
set (HAVE_LIB_GTEST 1) set (HAVE_LIB_GTEST 1)
endif (GTest_FOUND) endif (GTest_FOUND)
if (WITH_GMOCK AND TARGET GTest::gmock)
set (HAVE_LIB_GMOCK 1)
endif (WITH_GMOCK AND TARGET GTest::gmock)
if (WITH_GFLAGS) if (WITH_GFLAGS)
find_package (gflags 2.2.0) find_package (gflags 2.2.0)
@ -615,9 +622,20 @@ if (_glog_CMake_MODULES)
) )
endif (_glog_CMake_MODULES) endif (_glog_CMake_MODULES)
add_library (glogbase OBJECT
src/base.h
src/base.cc
)
target_include_directories (glogbase PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_BINARY_DIR}
)
add_library (glog add_library (glog
${GLOG_SRCS} $<TARGET_OBJECTS:glogbase>
${_glog_BINARY_CMake_MODULES} ${_glog_BINARY_CMake_MODULES}
${GLOG_SRCS}
) )
add_library (glog::glog ALIAS glog) add_library (glog::glog ALIAS glog)
@ -657,6 +675,7 @@ set_target_properties (glog PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties (glog PROPERTIES SOVERSION 1) set_target_properties (glog PROPERTIES SOVERSION 1)
if (CYGWIN OR WIN32) if (CYGWIN OR WIN32)
target_compile_definitions (glogbase PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES)
target_compile_definitions (glog PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES) target_compile_definitions (glog PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES)
endif (CYGWIN OR WIN32) endif (CYGWIN OR WIN32)
@ -674,6 +693,10 @@ target_include_directories (glog BEFORE PUBLIC
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
if (CYGWIN OR WIN32) if (CYGWIN OR WIN32)
target_include_directories (glogbase PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
target_include_directories (glog PUBLIC target_include_directories (glog PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows) PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
@ -691,10 +714,15 @@ if (BUILD_TESTING)
set (_GLOG_TEST_LIBS glog::glog) set (_GLOG_TEST_LIBS glog::glog)
if (HAVE_LIB_GTEST) if (HAVE_LIB_GTEST)
list (APPEND _GLOG_TEST_LIBS GTest::GTest) list (APPEND _GLOG_TEST_LIBS GTest::gtest)
endif (HAVE_LIB_GTEST) endif (HAVE_LIB_GTEST)
if (HAVE_LIB_GMOCK)
list (APPEND _GLOG_TEST_LIBS GTest::gmock)
endif (HAVE_LIB_GMOCK)
add_executable (logging_unittest add_executable (logging_unittest
$<TARGET_OBJECTS:glogbase>
src/logging_unittest.cc src/logging_unittest.cc
) )

View File

@ -78,6 +78,8 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
] ]
linux_or_darwin_copts = wasm_copts + [ linux_or_darwin_copts = wasm_copts + [
# Symbols explicitly marked as not being exported
"-DGLOG_NO_EXPORT=__attribute__((visibility(\\\"hidden\\\")))",
# For src/utilities.cc. # For src/utilities.cc.
"-DHAVE_SYS_SYSCALL_H", "-DHAVE_SYS_SYSCALL_H",
# For src/logging.cc to create symlinks. # For src/logging.cc to create symlinks.
@ -98,6 +100,7 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
windows_only_copts = [ windows_only_copts = [
"-DGLOG_NO_ABBREVIATED_SEVERITIES", "-DGLOG_NO_ABBREVIATED_SEVERITIES",
"-DGLOG_NO_EXPORT=",
"-DHAVE_SNPRINTF", "-DHAVE_SNPRINTF",
"-I" + src_windows, "-I" + src_windows,
] ]
@ -116,6 +119,8 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
srcs = [ srcs = [
":config_h", ":config_h",
"src/base.cc",
"src/base.h",
"src/base/commandlineflags.h", "src/base/commandlineflags.h",
"src/base/googleinit.h", "src/base/googleinit.h",
"src/base/mutex.h", "src/base/mutex.h",

81
src/base.cc Normal file
View File

@ -0,0 +1,81 @@
// Copyright (c) 1999, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstring>
#include <glog/platform.h>
#include "base.h"
namespace google {
Mutex log_mutex;
bool exit_on_dfatal = true;
namespace base {
namespace internal {
bool GetExitOnDFatal() {
MutexLock l(&log_mutex);
return exit_on_dfatal;
}
// Determines whether we exit the program for a LOG(DFATAL) message in
// debug mode. It does this by skipping the call to Fail/FailQuietly.
// This is intended for testing only.
//
// This can have some effects on LOG(FATAL) as well. Failure messages
// are always allocated (rather than sharing a buffer), the crash
// reason is not recorded, the "gwq" status message is not updated,
// and the stack trace is not recorded. The LOG(FATAL) *will* still
// exit the program. Since this function is used only in testing,
// these differences are acceptable.
void SetExitOnDFatal(bool value) {
MutexLock l(&log_mutex);
exit_on_dfatal = value;
}
} // namespace internal
} // namespace base
namespace glog_internal_namespace_ {
const char* const_basename(const char* filepath) {
const char* base = std::strrchr(filepath, '/');
#ifdef GLOG_OS_WINDOWS // Look for either path separator in Windows
if (!base)
base = std::strrchr(filepath, '\\');
#endif
return base ? (base+1) : filepath;
}
} // namespace glog_internal_namespace_
} // namespace google

63
src/base.h Normal file
View File

@ -0,0 +1,63 @@
// Copyright (c) 1999, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <glog/logging.h>
#include "base/mutex.h"
namespace google {
// A mutex that allows only one thread to log at a time, to keep things from
// getting jumbled. Some other very uncommon logging operations (like
// changing the destination file for log messages of a given severity) also
// lock this mutex. Please be sure that anybody who might possibly need to
// lock it does so.
GLOG_NO_EXPORT extern Mutex log_mutex;
// Has the user called SetExitOnDFatal(true)?
GLOG_NO_EXPORT extern bool exit_on_dfatal;
namespace base {
namespace internal {
GLOG_NO_EXPORT bool GetExitOnDFatal();
GLOG_NO_EXPORT void SetExitOnDFatal(bool value);
} // namespace internal
} // namespace base
namespace glog_internal_namespace_ {
// Get the part of filepath after the last path separator.
// (Doesn't modify filepath, contrary to basename() in libgen.h.)
GLOG_NO_EXPORT const char* const_basename(const char* filepath);
} // namespace glog_internal_namespace_
} // namespace google

View File

@ -29,6 +29,7 @@
#define _GNU_SOURCE 1 // needed for O_NOFOLLOW and pread()/pwrite() #define _GNU_SOURCE 1 // needed for O_NOFOLLOW and pread()/pwrite()
#include "base.h"
#include "utilities.h" #include "utilities.h"
#include <algorithm> #include <algorithm>
@ -379,13 +380,6 @@ struct LogMessage::LogMessageData {
void operator=(const LogMessageData&); void operator=(const LogMessageData&);
}; };
// A mutex that allows only one thread to log at a time, to keep things from
// getting jumbled. Some other very uncommon logging operations (like
// changing the destination file for log messages of a given severity) also
// lock this mutex. Please be sure that anybody who might possibly need to
// lock it does so.
static Mutex log_mutex;
// Number of messages sent at each severity. Under log_mutex. // Number of messages sent at each severity. Under log_mutex.
int64 LogMessage::num_messages_[NUM_SEVERITIES] = {0, 0, 0, 0}; int64 LogMessage::num_messages_[NUM_SEVERITIES] = {0, 0, 0, 0};
@ -396,9 +390,6 @@ const char*const LogSeverityNames[NUM_SEVERITIES] = {
"INFO", "WARNING", "ERROR", "FATAL" "INFO", "WARNING", "ERROR", "FATAL"
}; };
// Has the user called SetExitOnDFatal(true)?
static bool exit_on_dfatal = true;
const char* GetLogSeverityName(LogSeverity severity) { const char* GetLogSeverityName(LogSeverity severity) {
return LogSeverityNames[severity]; return LogSeverityNames[severity];
} }
@ -2079,33 +2070,6 @@ void LogToStderr() {
LogDestination::LogToStderr(); LogDestination::LogToStderr();
} }
namespace base {
namespace internal {
bool GetExitOnDFatal();
bool GetExitOnDFatal() {
MutexLock l(&log_mutex);
return exit_on_dfatal;
}
// Determines whether we exit the program for a LOG(DFATAL) message in
// debug mode. It does this by skipping the call to Fail/FailQuietly.
// This is intended for testing only.
//
// This can have some effects on LOG(FATAL) as well. Failure messages
// are always allocated (rather than sharing a buffer), the crash
// reason is not recorded, the "gwq" status message is not updated,
// and the stack trace is not recorded. The LOG(FATAL) *will* still
// exit the program. Since this function is used only in testing,
// these differences are acceptable.
void SetExitOnDFatal(bool value);
void SetExitOnDFatal(bool value) {
MutexLock l(&log_mutex);
exit_on_dfatal = value;
}
} // namespace internal
} // namespace base
// Shell-escaping as we need to shell out ot /bin/mail. // Shell-escaping as we need to shell out ot /bin/mail.
static const char kDontNeedShellEscapeChars[] = static const char kDontNeedShellEscapeChars[] =

View File

@ -29,6 +29,7 @@
// //
// Author: Ray Sidney // Author: Ray Sidney
#include "base.h"
#include "config.h" #include "config.h"
#include "utilities.h" #include "utilities.h"

View File

@ -31,6 +31,7 @@
// //
// logging_unittest.cc covers the functionality herein // logging_unittest.cc covers the functionality herein
#include "base.h"
#include "utilities.h" #include "utilities.h"
#include <stdarg.h> #include <stdarg.h>

View File

@ -292,15 +292,6 @@ pid_t GetTID() {
#endif #endif
} }
const char* const_basename(const char* filepath) {
const char* base = strrchr(filepath, '/');
#ifdef GLOG_OS_WINDOWS // Look for either path separator in Windows
if (!base)
base = strrchr(filepath, '\\');
#endif
return base ? (base+1) : filepath;
}
static string g_my_user_name; static string g_my_user_name;
const string& MyUserName() { const string& MyUserName() {
return g_my_user_name; return g_my_user_name;

View File

@ -158,10 +158,6 @@ pid_t GetTID();
const std::string& MyUserName(); const std::string& MyUserName();
// Get the part of filepath after the last path separator.
// (Doesn't modify filepath, contrary to basename() in libgen.h.)
const char* const_basename(const char* filepath);
// Wrapper of __sync_val_compare_and_swap. If the GCC extension isn't // Wrapper of __sync_val_compare_and_swap. If the GCC extension isn't
// defined, we try the CPU specific logics (we only support x86 and // defined, we try the CPU specific logics (we only support x86 and
// x86_64 for now) first, then use a naive implementation, which has a // x86_64 for now) first, then use a naive implementation, which has a