Skip to content

Commit

Permalink
added gmock support
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud committed Oct 7, 2021
1 parent 5652319 commit ead9375
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 54 deletions.
34 changes: 30 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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}
$<TARGET_OBJECTS:glogbase>
${_glog_BINARY_CMake_MODULES}
${GLOG_SRCS}
)

add_library (glog::glog ALIAS glog)
Expand Down Expand Up @@ -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)

Expand All @@ -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
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)

target_include_directories (glog PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
Expand All @@ -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
$<TARGET_OBJECTS:glogbase>
src/logging_unittest.cc
)

Expand Down
5 changes: 5 additions & 0 deletions bazel/glog.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
]
Expand All @@ -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",
Expand Down
81 changes: 81 additions & 0 deletions src/base.cc
Original file line number Diff line number Diff line change
@@ -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 changes: 63 additions & 0 deletions src/base.h
Original file line number Diff line number Diff line change
@@ -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
38 changes: 1 addition & 37 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#define _GNU_SOURCE 1 // needed for O_NOFOLLOW and pread()/pwrite()

#include "base.h"
#include "utilities.h"

#include <algorithm>
Expand Down Expand Up @@ -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};

Expand All @@ -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];
}
Expand Down Expand Up @@ -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[] =
Expand Down
1 change: 1 addition & 0 deletions src/logging_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
//
// Author: Ray Sidney

#include "base.h"
#include "config.h"
#include "utilities.h"

Expand Down
1 change: 1 addition & 0 deletions src/raw_logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//
// logging_unittest.cc covers the functionality herein

#include "base.h"
#include "utilities.h"

#include <stdarg.h>
Expand Down
9 changes: 0 additions & 9 deletions src/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ead9375

Please sign in to comment.