vlog_internal.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_COMMON_VLOG_INTERNAL_H_
  5. #define CARBON_COMMON_VLOG_INTERNAL_H_
  6. #include "common/ostream.h"
  7. namespace Carbon::Internal {
  8. // Wraps a stream and exiting for fatal errors. Should only be used by check.h
  9. // macros.
  10. class VLoggingStream {
  11. public:
  12. // Internal type used in macros to dispatch to the `operator|` overload.
  13. struct Helper {};
  14. explicit VLoggingStream(llvm::raw_ostream* stream)
  15. // Prefix the buffer with the current bug report message.
  16. : stream_(stream) {}
  17. ~VLoggingStream() = default;
  18. // If the bool cast occurs, it's because the condition is false. This supports
  19. // && short-circuiting the creation of ExitingStream.
  20. explicit operator bool() const { return true; }
  21. // Forward output to llvm::errs.
  22. template <typename T>
  23. auto operator<<(const T& message) -> VLoggingStream& {
  24. *stream_ << message;
  25. return *this;
  26. }
  27. // Low-precedence binary operator overload used in vlog.h macros.
  28. friend auto operator|(Helper /*helper*/, VLoggingStream& /*stream*/) -> void {
  29. }
  30. private:
  31. [[noreturn]] auto Done() -> void;
  32. llvm::raw_ostream* stream_;
  33. };
  34. } // namespace Carbon::Internal
  35. // Raw logging stream. This should be used when building forms of vlog
  36. // macros.
  37. #define CARBON_VLOG_INTERNAL_STREAM(stream) \
  38. Carbon::Internal::VLoggingStream::Helper() | \
  39. Carbon::Internal::VLoggingStream(stream)
  40. #endif // CARBON_COMMON_VLOG_INTERNAL_H_