blob: c323810b348c9394362dd40fba95b5c57642d9b6 [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
btolsch6d3d0bc2018-12-10 18:59:28 -080025// The following functions must be implemented by the platform.
26void LogInit(const char* filename);
btolsch9d6900c2018-05-30 18:22:53 -070027void SetLogLevel(LogLevel level, int verbose_level = 0);
28void LogWithLevel(LogLevel level,
29 int verbose_level,
30 const char* file,
31 int line,
32 const char* msg);
33void Break();
34//
35// END PLATFORM IMPLEMENTATION
36//
37
38// The stream-based logging macros below are adapted from Chromium's
39// base/logging.h.
40class LogMessage {
41 public:
42 LogMessage(LogLevel level, int verbose_level, const char* file, int line);
43 ~LogMessage();
44
45 std::ostream& stream() { return stream_; }
46
47 private:
48 const LogLevel level_;
49 const int verbose_level_;
50 const char* const file_;
51 const int line_;
52 std::ostringstream stream_;
53};
54
btolschfc647682018-11-13 23:43:33 -080055#define OSP_VLOG(l) \
btolsch9d6900c2018-05-30 18:22:53 -070056 ::openscreen::platform::LogMessage( \
57 ::openscreen::platform::LogLevel::kVerbose, l, __FILE__, __LINE__) \
58 .stream()
btolschfc647682018-11-13 23:43:33 -080059#define OSP_LOG_INFO \
btolsch9d6900c2018-05-30 18:22:53 -070060 ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kInfo, \
61 0, __FILE__, __LINE__) \
62 .stream()
btolschfc647682018-11-13 23:43:33 -080063#define OSP_LOG_WARN \
btolsch9d6900c2018-05-30 18:22:53 -070064 ::openscreen::platform::LogMessage( \
65 ::openscreen::platform::LogLevel::kWarning, 0, __FILE__, __LINE__) \
66 .stream()
btolschfc647682018-11-13 23:43:33 -080067#define OSP_LOG_ERROR \
btolsch9d6900c2018-05-30 18:22:53 -070068 ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kError, \
69 0, __FILE__, __LINE__) \
70 .stream()
btolschfc647682018-11-13 23:43:33 -080071#define OSP_LOG_FATAL \
btolsch9d6900c2018-05-30 18:22:53 -070072 ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kFatal, \
73 0, __FILE__, __LINE__) \
74 .stream()
75
76namespace detail {
77
78class Voidify {
79 public:
80 Voidify() = default;
81 void operator&(std::ostream&) {}
82};
83
84} // namespace detail
85
btolschfc647682018-11-13 23:43:33 -080086#define OSP_LAZY_STREAM(stream, condition) \
btolsch9d6900c2018-05-30 18:22:53 -070087 !(condition) ? (void)0 : ::openscreen::platform::detail::Voidify() & (stream)
btolschfc647682018-11-13 23:43:33 -080088#define OSP_EAT_STREAM OSP_LAZY_STREAM(OSP_LOG_INFO, false)
89#define OSP_LOG_IF(level, condition) \
90 OSP_LAZY_STREAM(OSP_LOG_##level, (condition))
91#define OSP_VLOG_IF(level, condition) \
92 OSP_LAZY_STREAM(OSP_VLOG(level), (condition))
btolsch9d6900c2018-05-30 18:22:53 -070093
btolschfc647682018-11-13 23:43:33 -080094// TODO(btolsch): Add tests for (D)OSP_CHECK and possibly logging.
95#define OSP_CHECK(condition) \
96 OSP_LAZY_STREAM(OSP_LOG_FATAL, !(condition)) \
97 << "OSP_CHECK(" << #condition << ") failed: "
btolsch9d6900c2018-05-30 18:22:53 -070098
btolschfc647682018-11-13 23:43:33 -080099#define OSP_CHECK_EQ(a, b) OSP_CHECK((a) == (b)) << a << " vs. " << b << ": "
100#define OSP_CHECK_NE(a, b) OSP_CHECK((a) != (b)) << a << " vs. " << b << ": "
101#define OSP_CHECK_LT(a, b) OSP_CHECK((a) < (b)) << a << " vs. " << b << ": "
102#define OSP_CHECK_LE(a, b) OSP_CHECK((a) <= (b)) << a << " vs. " << b << ": "
103#define OSP_CHECK_GT(a, b) OSP_CHECK((a) > (b)) << a << " vs. " << b << ": "
104#define OSP_CHECK_GE(a, b) OSP_CHECK((a) >= (b)) << a << " vs. " << b << ": "
btolsch834accf2018-06-13 23:10:28 -0700105
btolschfc647682018-11-13 23:43:33 -0800106#if defined(_DEBUG) || defined(OSP_DCHECK_ALWAYS_ON)
107#define OSP_DCHECK_IS_ON() 1
108#define OSP_DCHECK(condition) OSP_CHECK(condition)
109#define OSP_DCHECK_EQ(a, b) OSP_CHECK_EQ(a, b)
110#define OSP_DCHECK_NE(a, b) OSP_CHECK_NE(a, b)
111#define OSP_DCHECK_LT(a, b) OSP_CHECK_LT(a, b)
112#define OSP_DCHECK_LE(a, b) OSP_CHECK_LE(a, b)
113#define OSP_DCHECK_GT(a, b) OSP_CHECK_GT(a, b)
114#define OSP_DCHECK_GE(a, b) OSP_CHECK_GE(a, b)
btolsch9d6900c2018-05-30 18:22:53 -0700115#else
btolschfc647682018-11-13 23:43:33 -0800116#define OSP_DCHECK_IS_ON() 0
117#define OSP_DCHECK(condition) OSP_EAT_STREAM
118#define OSP_DCHECK_EQ(a, b) OSP_EAT_STREAM
119#define OSP_DCHECK_NE(a, b) OSP_EAT_STREAM
120#define OSP_DCHECK_LT(a, b) OSP_EAT_STREAM
121#define OSP_DCHECK_LE(a, b) OSP_EAT_STREAM
122#define OSP_DCHECK_GT(a, b) OSP_EAT_STREAM
123#define OSP_DCHECK_GE(a, b) OSP_EAT_STREAM
btolsch9d6900c2018-05-30 18:22:53 -0700124#endif
125
btolschfc647682018-11-13 23:43:33 -0800126#define OSP_DLOG_INFO OSP_LOG_IF(INFO, OSP_DCHECK_IS_ON())
127#define OSP_DLOG_WARN OSP_LOG_IF(WARN, OSP_DCHECK_IS_ON())
128#define OSP_DLOG_ERROR OSP_LOG_IF(ERROR, OSP_DCHECK_IS_ON())
129#define OSP_DLOG_FATAL OSP_LOG_IF(FATAL, OSP_DCHECK_IS_ON())
130#define OSP_DLOG_IF(level, condition) \
131 OSP_LOG_IF(level, OSP_DCHECK_IS_ON() && (condition))
132#define OSP_DVLOG(l) OSP_VLOG_IF(l, OSP_DCHECK_IS_ON())
133#define OSP_DVLOG_IF(l, condition) \
134 OSP_VLOG_IF(l, OSP_DCHECK_IS_ON() && (condition))
btolsch834accf2018-06-13 23:10:28 -0700135
btolschfc647682018-11-13 23:43:33 -0800136#define OSP_UNIMPLEMENTED() OSP_LOG_ERROR << __func__ << ": unimplemented"
btolsch4b68dc92018-07-26 00:26:54 -0700137
btolsch9d6900c2018-05-30 18:22:53 -0700138} // namespace platform
139} // namespace openscreen
140
btolscha21e8ed2018-08-30 15:13:48 -0700141#endif // PLATFORM_API_LOGGING_H_