blob: 7cec6064fb0c324728598664917aecc7a717b768 [file] [log] [blame]
btolsch9d6900c2018-05-30 18:22:53 -07001// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef PLATFORM_API_LOGGING_H_
6#define PLATFORM_API_LOGGING_H_
7
8#include <sstream>
9
10namespace openscreen {
11namespace platform {
12
13enum class LogLevel {
14 kVerbose = 0,
15 kInfo,
16 kWarning,
17 kError,
18 kFatal,
19};
20
21std::string LogLevelToString(LogLevel level);
22
23//
24// PLATFORM IMPLEMENTATION
25// The follow functions must be implemented by the platform.
26void SetLogLevel(LogLevel level, int verbose_level = 0);
27void LogWithLevel(LogLevel level,
28 int verbose_level,
29 const char* file,
30 int line,
31 const char* msg);
32void Break();
33//
34// END PLATFORM IMPLEMENTATION
35//
36
37// The stream-based logging macros below are adapted from Chromium's
38// base/logging.h.
39class LogMessage {
40 public:
41 LogMessage(LogLevel level, int verbose_level, const char* file, int line);
42 ~LogMessage();
43
44 std::ostream& stream() { return stream_; }
45
46 private:
47 const LogLevel level_;
48 const int verbose_level_;
49 const char* const file_;
50 const int line_;
51 std::ostringstream stream_;
52};
53
btolschfc647682018-11-13 23:43:33 -080054#define OSP_VLOG(l) \
btolsch9d6900c2018-05-30 18:22:53 -070055 ::openscreen::platform::LogMessage( \
56 ::openscreen::platform::LogLevel::kVerbose, l, __FILE__, __LINE__) \
57 .stream()
btolschfc647682018-11-13 23:43:33 -080058#define OSP_LOG_INFO \
btolsch9d6900c2018-05-30 18:22:53 -070059 ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kInfo, \
60 0, __FILE__, __LINE__) \
61 .stream()
btolschfc647682018-11-13 23:43:33 -080062#define OSP_LOG_WARN \
btolsch9d6900c2018-05-30 18:22:53 -070063 ::openscreen::platform::LogMessage( \
64 ::openscreen::platform::LogLevel::kWarning, 0, __FILE__, __LINE__) \
65 .stream()
btolschfc647682018-11-13 23:43:33 -080066#define OSP_LOG_ERROR \
btolsch9d6900c2018-05-30 18:22:53 -070067 ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kError, \
68 0, __FILE__, __LINE__) \
69 .stream()
btolschfc647682018-11-13 23:43:33 -080070#define OSP_LOG_FATAL \
btolsch9d6900c2018-05-30 18:22:53 -070071 ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kFatal, \
72 0, __FILE__, __LINE__) \
73 .stream()
74
75namespace detail {
76
77class Voidify {
78 public:
79 Voidify() = default;
80 void operator&(std::ostream&) {}
81};
82
83} // namespace detail
84
btolschfc647682018-11-13 23:43:33 -080085#define OSP_LAZY_STREAM(stream, condition) \
btolsch9d6900c2018-05-30 18:22:53 -070086 !(condition) ? (void)0 : ::openscreen::platform::detail::Voidify() & (stream)
btolschfc647682018-11-13 23:43:33 -080087#define OSP_EAT_STREAM OSP_LAZY_STREAM(OSP_LOG_INFO, false)
88#define OSP_LOG_IF(level, condition) \
89 OSP_LAZY_STREAM(OSP_LOG_##level, (condition))
90#define OSP_VLOG_IF(level, condition) \
91 OSP_LAZY_STREAM(OSP_VLOG(level), (condition))
btolsch9d6900c2018-05-30 18:22:53 -070092
btolschfc647682018-11-13 23:43:33 -080093// TODO(btolsch): Add tests for (D)OSP_CHECK and possibly logging.
94#define OSP_CHECK(condition) \
95 OSP_LAZY_STREAM(OSP_LOG_FATAL, !(condition)) \
96 << "OSP_CHECK(" << #condition << ") failed: "
btolsch9d6900c2018-05-30 18:22:53 -070097
btolschfc647682018-11-13 23:43:33 -080098#define OSP_CHECK_EQ(a, b) OSP_CHECK((a) == (b)) << a << " vs. " << b << ": "
99#define OSP_CHECK_NE(a, b) OSP_CHECK((a) != (b)) << a << " vs. " << b << ": "
100#define OSP_CHECK_LT(a, b) OSP_CHECK((a) < (b)) << a << " vs. " << b << ": "
101#define OSP_CHECK_LE(a, b) OSP_CHECK((a) <= (b)) << a << " vs. " << b << ": "
102#define OSP_CHECK_GT(a, b) OSP_CHECK((a) > (b)) << a << " vs. " << b << ": "
103#define OSP_CHECK_GE(a, b) OSP_CHECK((a) >= (b)) << a << " vs. " << b << ": "
btolsch834accf2018-06-13 23:10:28 -0700104
btolschfc647682018-11-13 23:43:33 -0800105#if defined(_DEBUG) || defined(OSP_DCHECK_ALWAYS_ON)
106#define OSP_DCHECK_IS_ON() 1
107#define OSP_DCHECK(condition) OSP_CHECK(condition)
108#define OSP_DCHECK_EQ(a, b) OSP_CHECK_EQ(a, b)
109#define OSP_DCHECK_NE(a, b) OSP_CHECK_NE(a, b)
110#define OSP_DCHECK_LT(a, b) OSP_CHECK_LT(a, b)
111#define OSP_DCHECK_LE(a, b) OSP_CHECK_LE(a, b)
112#define OSP_DCHECK_GT(a, b) OSP_CHECK_GT(a, b)
113#define OSP_DCHECK_GE(a, b) OSP_CHECK_GE(a, b)
btolsch9d6900c2018-05-30 18:22:53 -0700114#else
btolschfc647682018-11-13 23:43:33 -0800115#define OSP_DCHECK_IS_ON() 0
116#define OSP_DCHECK(condition) OSP_EAT_STREAM
117#define OSP_DCHECK_EQ(a, b) OSP_EAT_STREAM
118#define OSP_DCHECK_NE(a, b) OSP_EAT_STREAM
119#define OSP_DCHECK_LT(a, b) OSP_EAT_STREAM
120#define OSP_DCHECK_LE(a, b) OSP_EAT_STREAM
121#define OSP_DCHECK_GT(a, b) OSP_EAT_STREAM
122#define OSP_DCHECK_GE(a, b) OSP_EAT_STREAM
btolsch9d6900c2018-05-30 18:22:53 -0700123#endif
124
btolschfc647682018-11-13 23:43:33 -0800125#define OSP_DLOG_INFO OSP_LOG_IF(INFO, OSP_DCHECK_IS_ON())
126#define OSP_DLOG_WARN OSP_LOG_IF(WARN, OSP_DCHECK_IS_ON())
127#define OSP_DLOG_ERROR OSP_LOG_IF(ERROR, OSP_DCHECK_IS_ON())
128#define OSP_DLOG_FATAL OSP_LOG_IF(FATAL, OSP_DCHECK_IS_ON())
129#define OSP_DLOG_IF(level, condition) \
130 OSP_LOG_IF(level, OSP_DCHECK_IS_ON() && (condition))
131#define OSP_DVLOG(l) OSP_VLOG_IF(l, OSP_DCHECK_IS_ON())
132#define OSP_DVLOG_IF(l, condition) \
133 OSP_VLOG_IF(l, OSP_DCHECK_IS_ON() && (condition))
btolsch834accf2018-06-13 23:10:28 -0700134
btolschfc647682018-11-13 23:43:33 -0800135#define OSP_UNIMPLEMENTED() OSP_LOG_ERROR << __func__ << ": unimplemented"
btolsch4b68dc92018-07-26 00:26:54 -0700136
btolsch9d6900c2018-05-30 18:22:53 -0700137} // namespace platform
138} // namespace openscreen
139
btolscha21e8ed2018-08-30 15:13:48 -0700140#endif // PLATFORM_API_LOGGING_H_