Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ std::aligned_storage<sizeof(char), alignof(char)>::type data;
int main() { }
" HAVE_ALIGNED_STORAGE)

check_cxx_source_compiles ("
#include <atomic>
std::atomic<int> i;
int main() { }
" HAVE_CXX11_ATOMIC)

if (WITH_TLS)
# Cygwin does not support the thread attribute. Don't bother.
if (HAVE_GCC_TLS)
Expand Down
3 changes: 3 additions & 0 deletions src/config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@
/* Check whether aligned_storage and alignof present */
#cmakedefine HAVE_ALIGNED_STORAGE ${HAVE_ALIGNED_STORAGE}

/* Check whether C++11 atomic is available */
#cmakedefine HAVE_CXX11_ATOMIC ${HAVE_CXX11_ATOMIC}

/* Version number of package */
#cmakedefine VERSION

Expand Down
100 changes: 96 additions & 4 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
#include <gflags/gflags.h>
#endif

#if HAVE_CXX11_ATOMIC
#include <atomic>
#elif defined(OS_WINDOWS)
#include <Windows.h>
#endif

@ac_google_start_namespace@

#if @ac_cv_have_uint16_t@ // the C99 format
Expand Down Expand Up @@ -896,8 +902,9 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
#define LOG_OCCURRENCES LOG_EVERY_N_VARNAME(occurrences_, __LINE__)
#define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__)

#ifdef HAVE_CXX11_ATOMIC
#define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
static std::atomic<int> LOG_OCCURRENCES(0), LOG_OCCURRENCES_MOD_N(0); \
++LOG_OCCURRENCES; \
if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
if (LOG_OCCURRENCES_MOD_N == 1) \
Expand All @@ -906,7 +913,7 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
&what_to_do).stream()

#define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
static std::atomic<int> LOG_OCCURRENCES(0), LOG_OCCURRENCES_MOD_N(0); \
++LOG_OCCURRENCES; \
if (condition && \
((LOG_OCCURRENCES_MOD_N=(LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n))) \
Expand All @@ -915,7 +922,7 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
&what_to_do).stream()

#define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
static std::atomic<int> LOG_OCCURRENCES(0), LOG_OCCURRENCES_MOD_N(0); \
++LOG_OCCURRENCES; \
if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
if (LOG_OCCURRENCES_MOD_N == 1) \
Expand All @@ -924,14 +931,99 @@ PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
&what_to_do).stream()

#define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0; \
static std::atomic<int> LOG_OCCURRENCES(0); \
if (LOG_OCCURRENCES <= n) \
++LOG_OCCURRENCES; \
if (LOG_OCCURRENCES <= n) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#elif defined(OS_WINDOWS)

#define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
InterlockedIncrement(&LOG_OCCURRENCES); \
if (InterlockedIncrement(&LOG_OCCURRENCES_MOD_N) > n) \
InterlockedExchangeSubtract(&LOG_OCCURRENCES_MOD_N, n); \
if (LOG_OCCURRENCES_MOD_N == 1) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
InterlockedIncrement(&LOG_OCCURRENCES); \
if (condition && \
(InterlockedIncrement(&LOG_OCCURRENCES_MOD_N) || true) && \
((LOG_OCCURRENCES_MOD_N >= n && InterlockedExchangeAdd(&LOG_OCCURRENCES_MOD_N, n)) || true) && \
LOG_OCCURRENCES_MOD_N == (1 % n)) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
InterlockedIncrement(&LOG_OCCURRENCES); \
if (InterlockedIncrement(&LOG_OCCURRENCES_MOD_N) > n) \
InterlockedExchangeSubtract(&LOG_OCCURRENCES_MOD_N, n); \
if (LOG_OCCURRENCES_MOD_N == 1) \
@ac_google_namespace@::ErrnoLogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0; \
if (LOG_OCCURRENCES <= n) \
InterlockedIncrement(&LOG_OCCURRENCES); \
if (LOG_OCCURRENCES <= n) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#else

#define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
__sync_add_and_fetch(&LOG_OCCURRENCES, 1); \
if (__sync_add_and_fetch(&LOG_OCCURRENCES_MOD_N, 1) > n) \
__sync_sub_and_fetch(&LOG_OCCURRENCES_MOD_N, n); \
if (LOG_OCCURRENCES_MOD_N == 1) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
__sync_add_and_fetch(&LOG_OCCURRENCES, 1); \
if (condition && \
(__sync_add_and_fetch(&LOG_OCCURRENCES_MOD_N, 1) || true) && \
((LOG_OCCURRENCES_MOD_N >= n && __sync_sub_and_fetch(&LOG_OCCURRENCES_MOD_N, n)) || true) && \
LOG_OCCURRENCES_MOD_N == (1 % n)) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
__sync_add_and_fetch(&LOG_OCCURRENCES, 1); \
if (__sync_add_and_fetch(&LOG_OCCURRENCES_MOD_N, 1) > n) \
__sync_sub_and_fetch(&LOG_OCCURRENCES_MOD_N, n); \
if (LOG_OCCURRENCES_MOD_N == 1) \
@ac_google_namespace@::ErrnoLogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()

#define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0; \
if (LOG_OCCURRENCES <= n) \
__sync_add_and_fetch(&LOG_OCCURRENCES, 1); \
if (LOG_OCCURRENCES <= n) \
@ac_google_namespace@::LogMessage( \
__FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
&what_to_do).stream()
#endif

namespace glog_internal_namespace_ {
template <bool>
struct CompileAssert {
Expand Down