From ead93759d41d5b9dce6b47ca7f73bb04060590fd Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Thu, 7 Oct 2021 18:24:45 +0200 Subject: [PATCH] added gmock support --- CMakeLists.txt | 34 +++++++++++++++-- bazel/glog.bzl | 5 +++ src/base.cc | 81 +++++++++++++++++++++++++++++++++++++++++ src/base.h | 63 ++++++++++++++++++++++++++++++++ src/logging.cc | 38 +------------------ src/logging_unittest.cc | 1 + src/raw_logging.cc | 1 + src/utilities.cc | 9 ----- src/utilities.h | 4 -- 9 files changed, 182 insertions(+), 54 deletions(-) create mode 100644 src/base.cc create mode 100644 src/base.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e68c0da43..6a25a4da9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,8 @@ option (PRINT_UNSYMBOLIZED_STACK_TRACES "Print file offsets in traces instead of symbolizing" OFF) option (WITH_CUSTOM_PREFIX "Enable support for user-generated message prefixes" OFF) option (WITH_GFLAGS "Use gflags" ON) -option (WITH_GTEST "Use googletest" ON) +option (WITH_GMOCK "Use Google Mock" ON) +option (WITH_GTEST "Use Google Test" ON) option (WITH_PKGCONFIG "Enable pkg-config support" ON) option (WITH_SYMBOLIZE "Enable symbolize module" ON) option (WITH_THREADS "Enable multithreading support" ON) @@ -69,12 +70,16 @@ include (GNUInstallDirs) set (CMAKE_DEBUG_POSTFIX d) set (CMAKE_THREAD_PREFER_PTHREAD 1) -find_package (GTest) +find_package (GTest NO_MODULE) if (GTest_FOUND) set (HAVE_LIB_GTEST 1) 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) find_package (gflags 2.2.0) @@ -615,9 +620,20 @@ if (_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 - ${GLOG_SRCS} + $ ${_glog_BINARY_CMake_MODULES} + ${GLOG_SRCS} ) add_library (glog::glog ALIAS glog) @@ -657,6 +673,7 @@ set_target_properties (glog PROPERTIES VERSION ${PROJECT_VERSION}) set_target_properties (glog PROPERTIES SOVERSION 1) if (CYGWIN OR WIN32) + target_compile_definitions (glogbase PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES) target_compile_definitions (glog PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES) endif (CYGWIN OR WIN32) @@ -674,6 +691,10 @@ target_include_directories (glog BEFORE PUBLIC PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) if (CYGWIN OR WIN32) + target_include_directories (glogbase PUBLIC + "$" + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows) + target_include_directories (glog PUBLIC "$" PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows) @@ -691,10 +712,15 @@ if (BUILD_TESTING) set (_GLOG_TEST_LIBS glog::glog) if (HAVE_LIB_GTEST) - list (APPEND _GLOG_TEST_LIBS GTest::GTest) + list (APPEND _GLOG_TEST_LIBS GTest::gtest) endif (HAVE_LIB_GTEST) + if (HAVE_LIB_GMOCK) + list (APPEND _GLOG_TEST_LIBS GTest::gmock) + endif (HAVE_LIB_GMOCK) + add_executable (logging_unittest + $ src/logging_unittest.cc ) diff --git a/bazel/glog.bzl b/bazel/glog.bzl index 75f1f53ac..146df4c61 100644 --- a/bazel/glog.bzl +++ b/bazel/glog.bzl @@ -78,6 +78,8 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs): ] linux_or_darwin_copts = wasm_copts + [ + # Symbols explicitly marked as not being exported + "-DGLOG_NO_EXPORT=__attribute__((visibility(\\\"hidden\\\")))", # For src/utilities.cc. "-DHAVE_SYS_SYSCALL_H", # For src/logging.cc to create symlinks. @@ -96,6 +98,7 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs): windows_only_copts = [ "-DGLOG_NO_ABBREVIATED_SEVERITIES", + "-DGLOG_NO_EXPORT=", "-DHAVE_SNPRINTF", "-I" + src_windows, ] @@ -114,6 +117,8 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs): visibility = ["//visibility:public"], srcs = [ ":config_h", + "src/base.cc", + "src/base.h", "src/base/commandlineflags.h", "src/base/googleinit.h", "src/base/mutex.h", diff --git a/src/base.cc b/src/base.cc new file mode 100644 index 000000000..abe83d011 --- /dev/null +++ b/src/base.cc @@ -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 + +#include + +#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 diff --git a/src/base.h b/src/base.h new file mode 100644 index 000000000..68f88ff35 --- /dev/null +++ b/src/base.h @@ -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 + +#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 diff --git a/src/logging.cc b/src/logging.cc index fe3d609cd..e652cfdad 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -29,6 +29,7 @@ #define _GNU_SOURCE 1 // needed for O_NOFOLLOW and pread()/pwrite() +#include "base.h" #include "utilities.h" #include @@ -379,13 +380,6 @@ struct LogMessage::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. int64 LogMessage::num_messages_[NUM_SEVERITIES] = {0, 0, 0, 0}; @@ -396,9 +390,6 @@ const char*const LogSeverityNames[NUM_SEVERITIES] = { "INFO", "WARNING", "ERROR", "FATAL" }; -// Has the user called SetExitOnDFatal(true)? -static bool exit_on_dfatal = true; - const char* GetLogSeverityName(LogSeverity severity) { return LogSeverityNames[severity]; } @@ -2079,33 +2070,6 @@ void 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. static const char kDontNeedShellEscapeChars[] = diff --git a/src/logging_unittest.cc b/src/logging_unittest.cc index 62101ba59..c53901013 100644 --- a/src/logging_unittest.cc +++ b/src/logging_unittest.cc @@ -29,6 +29,7 @@ // // Author: Ray Sidney +#include "base.h" #include "config.h" #include "utilities.h" diff --git a/src/raw_logging.cc b/src/raw_logging.cc index 8cedabd0f..de80b9687 100644 --- a/src/raw_logging.cc +++ b/src/raw_logging.cc @@ -31,6 +31,7 @@ // // logging_unittest.cc covers the functionality herein +#include "base.h" #include "utilities.h" #include diff --git a/src/utilities.cc b/src/utilities.cc index 020e7b3b6..b3771d952 100644 --- a/src/utilities.cc +++ b/src/utilities.cc @@ -292,15 +292,6 @@ pid_t GetTID() { #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; const string& MyUserName() { return g_my_user_name; diff --git a/src/utilities.h b/src/utilities.h index 258643ddb..b8e1aec3d 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -158,10 +158,6 @@ pid_t GetTID(); 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 // 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