aboutsummaryrefslogtreecommitdiff
path: root/absl/log/log.h
diff options
context:
space:
mode:
Diffstat (limited to 'absl/log/log.h')
-rw-r--r--absl/log/log.h260
1 files changed, 84 insertions, 176 deletions
diff --git a/absl/log/log.h b/absl/log/log.h
index 5331fdbf..e060a0b6 100644
--- a/absl/log/log.h
+++ b/absl/log/log.h
@@ -132,10 +132,45 @@
// as they would be if streamed into a `std::ostream`, however it should be
// noted that their actual type is unspecified.
//
-// To implement a custom formatting operator for a type you own, define
+// To implement a custom formatting operator for a type you own, there are two
+// options: `AbslStringify()` or `std::ostream& operator<<(std::ostream&, ...)`.
+// It is recommended that users make their types loggable through
+// `AbslStringify()` as it is a universal stringification extension that also
+// enables `absl::StrFormat` and `absl::StrCat` support. If both
+// `AbslStringify()` and `std::ostream& operator<<(std::ostream&, ...)` are
+// defined, `AbslStringify()` will be used.
+//
+// To use the `AbslStringify()` API, define a friend function template in your
+// type's namespace with the following signature:
+//
+// template <typename Sink>
+// void AbslStringify(Sink& sink, const UserDefinedType& value);
+//
+// `Sink` has the same interface as `absl::FormatSink`, but without
+// `PutPaddedString()`.
+//
+// Example:
+//
+// struct Point {
+// template <typename Sink>
+// friend void AbslStringify(Sink& sink, const Point& p) {
+// absl::Format(&sink, "(%v, %v)", p.x, p.y);
+// }
+//
+// int x;
+// int y;
+// };
+//
+// To use `std::ostream& operator<<(std::ostream&, ...)`, define
// `std::ostream& operator<<(std::ostream&, ...)` in your type's namespace (for
// ADL) just as you would to stream it to `std::cout`.
//
+// Currently `AbslStringify()` ignores output manipulators but this is not
+// guaranteed behavior and may be subject to change in the future. If you would
+// like guaranteed behavior regarding output manipulators, please use
+// `std::ostream& operator<<(std::ostream&, ...)` to make custom types loggable
+// instead.
+//
// Those macros that support streaming honor output manipulators and `fmtflag`
// changes that output data (e.g. `std::ends`) or control formatting of data
// (e.g. `std::hex` and `std::fixed`), however flushing such a stream is
@@ -152,9 +187,7 @@
#ifndef ABSL_LOG_LOG_H_
#define ABSL_LOG_LOG_H_
-#include "absl/log/internal/conditions.h"
-#include "absl/log/internal/log_message.h"
-#include "absl/log/internal/strip.h"
+#include "absl/log/internal/log_impl.h"
// LOG()
//
@@ -163,56 +196,29 @@
// Example:
//
// LOG(INFO) << "Found " << num_cookies << " cookies";
-#define LOG(severity) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, true) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
+#define LOG(severity) ABSL_LOG_IMPL(_##severity)
// PLOG()
//
// `PLOG` behaves like `LOG` except that a description of the current state of
// `errno` is appended to the streamed message.
-#define PLOG(severity) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, true) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
+#define PLOG(severity) ABSL_PLOG_IMPL(_##severity)
// DLOG()
//
// `DLOG` behaves like `LOG` in debug mode (i.e. `#ifndef NDEBUG`). Otherwise
// it compiles away and does nothing. Note that `DLOG(FATAL)` does not
// terminate the program if `NDEBUG` is defined.
-#ifndef NDEBUG
-#define DLOG(severity) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, true) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-#else
-#define DLOG(severity) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, false) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-#endif
+#define DLOG(severity) ABSL_DLOG_IMPL(_##severity)
// `LOG_IF` and friends add a second argument which specifies a condition. If
// the condition is false, nothing is logged.
// Example:
//
// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
-#define LOG_IF(severity, condition) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, condition) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-#define PLOG_IF(severity, condition) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, condition) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#ifndef NDEBUG
-#define DLOG_IF(severity, condition) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, condition) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-#else
-#define DLOG_IF(severity, condition) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, false && (condition)) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-#endif
+#define LOG_IF(severity, condition) ABSL_LOG_IF_IMPL(_##severity, condition)
+#define PLOG_IF(severity, condition) ABSL_PLOG_IF_IMPL(_##severity, condition)
+#define DLOG_IF(severity, condition) ABSL_DLOG_IF_IMPL(_##severity, condition)
// LOG_EVERY_N
//
@@ -225,27 +231,21 @@
//
// LOG_EVERY_N(WARNING, 1000) << "Got a packet with a bad CRC (" << COUNTER
// << " total)";
-#define LOG_EVERY_N(severity, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
+#define LOG_EVERY_N(severity, n) ABSL_LOG_EVERY_N_IMPL(_##severity, n)
// LOG_FIRST_N
//
// `LOG_FIRST_N` behaves like `LOG_EVERY_N` except that the specified message is
// logged when the counter's value is less than `n`. `LOG_FIRST_N` is
// thread-safe.
-#define LOG_FIRST_N(severity, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(FirstN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
+#define LOG_FIRST_N(severity, n) ABSL_LOG_FIRST_N_IMPL(_##severity, n)
// LOG_EVERY_POW_2
//
// `LOG_EVERY_POW_2` behaves like `LOG_EVERY_N` except that the specified
// message is logged when the counter's value is a power of 2.
// `LOG_EVERY_POW_2` is thread-safe.
-#define LOG_EVERY_POW_2(severity) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryPow2) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
+#define LOG_EVERY_POW_2(severity) ABSL_LOG_EVERY_POW_2_IMPL(_##severity)
// LOG_EVERY_N_SEC
//
@@ -256,64 +256,20 @@
// Example:
//
// LOG_EVERY_N_SEC(INFO, 2.5) << "Got " << COUNTER << " cookies so far";
-#define LOG_EVERY_N_SEC(severity, n_seconds) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryNSec, n_seconds) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define PLOG_EVERY_N(severity, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#define PLOG_FIRST_N(severity, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(FirstN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#define PLOG_EVERY_POW_2(severity) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryPow2) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#define PLOG_EVERY_N_SEC(severity, n_seconds) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryNSec, n_seconds) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#ifndef NDEBUG
-#define DLOG_EVERY_N(severity, n) \
- ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \
- (EveryN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_FIRST_N(severity, n) \
- ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \
- (FirstN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_EVERY_POW_2(severity) \
- ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \
- (EveryPow2) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
+#define LOG_EVERY_N_SEC(severity, n_seconds) \
+ ABSL_LOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
-#define DLOG_EVERY_N_SEC(severity, n_seconds) \
- ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \
- (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
+#define PLOG_EVERY_N(severity, n) ABSL_PLOG_EVERY_N_IMPL(_##severity, n)
+#define PLOG_FIRST_N(severity, n) ABSL_PLOG_FIRST_N_IMPL(_##severity, n)
+#define PLOG_EVERY_POW_2(severity) ABSL_PLOG_EVERY_POW_2_IMPL(_##severity)
+#define PLOG_EVERY_N_SEC(severity, n_seconds) \
+ ABSL_PLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
-#else // def NDEBUG
-#define DLOG_EVERY_N(severity, n) \
- ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \
- (EveryN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_FIRST_N(severity, n) \
- ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \
- (FirstN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_EVERY_POW_2(severity) \
- ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \
- (EveryPow2) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_EVERY_N_SEC(severity, n_seconds) \
- ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \
- (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-#endif // def NDEBUG
+#define DLOG_EVERY_N(severity, n) ABSL_DLOG_EVERY_N_IMPL(_##severity, n)
+#define DLOG_FIRST_N(severity, n) ABSL_DLOG_FIRST_N_IMPL(_##severity, n)
+#define DLOG_EVERY_POW_2(severity) ABSL_DLOG_EVERY_POW_2_IMPL(_##severity)
+#define DLOG_EVERY_N_SEC(severity, n_seconds) \
+ ABSL_DLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
// `LOG_IF_EVERY_N` and friends behave as the corresponding `LOG_EVERY_N`
// but neither increment a counter nor log a message if condition is false (as
@@ -322,79 +278,31 @@
//
// LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << COUNTER
// << "th big cookie";
-#define LOG_IF_EVERY_N(severity, condition, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define LOG_IF_FIRST_N(severity, condition, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(FirstN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define LOG_IF_EVERY_POW_2(severity, condition) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryPow2) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define LOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryNSec, \
- n_seconds) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define PLOG_IF_EVERY_N(severity, condition, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#define PLOG_IF_FIRST_N(severity, condition, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(FirstN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#define PLOG_IF_EVERY_POW_2(severity, condition) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryPow2) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#define PLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryNSec, \
- n_seconds) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \
- .WithPerror()
-
-#ifndef NDEBUG
-#define DLOG_IF_EVERY_N(severity, condition, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_IF_FIRST_N(severity, condition, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(FirstN, n) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_IF_EVERY_POW_2(severity, condition) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryPow2) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryNSec, \
- n_seconds) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#else // def NDEBUG
-#define DLOG_IF_EVERY_N(severity, condition, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, false && (condition))( \
- EveryN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_IF_FIRST_N(severity, condition, n) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, false && (condition))( \
- FirstN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_IF_EVERY_POW_2(severity, condition) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, false && (condition))( \
- EveryPow2) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-
-#define DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
- ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, false && (condition))( \
- EveryNSec, n_seconds) \
- ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream()
-#endif // def NDEBUG
+#define LOG_IF_EVERY_N(severity, condition, n) \
+ ABSL_LOG_IF_EVERY_N_IMPL(_##severity, condition, n)
+#define LOG_IF_FIRST_N(severity, condition, n) \
+ ABSL_LOG_IF_FIRST_N_IMPL(_##severity, condition, n)
+#define LOG_IF_EVERY_POW_2(severity, condition) \
+ ABSL_LOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
+#define LOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
+ ABSL_LOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
+
+#define PLOG_IF_EVERY_N(severity, condition, n) \
+ ABSL_PLOG_IF_EVERY_N_IMPL(_##severity, condition, n)
+#define PLOG_IF_FIRST_N(severity, condition, n) \
+ ABSL_PLOG_IF_FIRST_N_IMPL(_##severity, condition, n)
+#define PLOG_IF_EVERY_POW_2(severity, condition) \
+ ABSL_PLOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
+#define PLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
+ ABSL_PLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
+
+#define DLOG_IF_EVERY_N(severity, condition, n) \
+ ABSL_DLOG_IF_EVERY_N_IMPL(_##severity, condition, n)
+#define DLOG_IF_FIRST_N(severity, condition, n) \
+ ABSL_DLOG_IF_FIRST_N_IMPL(_##severity, condition, n)
+#define DLOG_IF_EVERY_POW_2(severity, condition) \
+ ABSL_DLOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
+#define DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
+ ABSL_DLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
#endif // ABSL_LOG_LOG_H_