btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 1 | // 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 | |
btolsch | 2f2e7fa | 2019-03-29 16:48:49 -0700 | [diff] [blame] | 8 | #include "absl/strings/string_view.h" |
Jordan Bayles | d08f8d2 | 2019-01-17 15:06:02 -0800 | [diff] [blame] | 9 | |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 10 | namespace openscreen { |
| 11 | namespace platform { |
| 12 | |
| 13 | enum class LogLevel { |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame^] | 14 | // Very detailed information, often used for evaluating performance or |
| 15 | // debugging production issues in-the-wild. |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 16 | kVerbose = 0, |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame^] | 17 | |
| 18 | // Used occasionally to note events of interest, but not for indicating any |
| 19 | // problems. This is also used for general console messaging in Open Screen's |
| 20 | // standalone executables. |
| 21 | kInfo = 1, |
| 22 | |
| 23 | // Indicates a problem that may or may not lead to an operational failure. |
| 24 | kWarning = 2, |
| 25 | |
| 26 | // Indicates an operational failure that may or may not cause a component to |
| 27 | // stop working. |
| 28 | kError = 3, |
| 29 | |
| 30 | // Indicates a logic flaw, corruption, impossible/unanticipated situation, or |
| 31 | // operational failure so serious that Open Screen will soon call Break() to |
| 32 | // abort the current process. Examples: security/privacy risks, memory |
| 33 | // management issues, API contract violations. |
| 34 | kFatal = 4, |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame^] | 37 | // Returns true if |level| is at or above the level where the embedder will |
| 38 | // record/emit log entries from the code in |file|. |
| 39 | bool IsLoggingOn(LogLevel level, absl::string_view file); |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 40 | |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame^] | 41 | // Record a log entry, consisting of its logging level, location and message. |
| 42 | // The embedder may filter-out entries according to its own policy, but this |
| 43 | // function will not be called if IsLoggingOn(level, file) returns false. |
| 44 | // Whenever |level| is kFatal, Open Screen will call Break() immediately after |
| 45 | // this returns. |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 46 | void LogWithLevel(LogLevel level, |
Jordan Bayles | d08f8d2 | 2019-01-17 15:06:02 -0800 | [diff] [blame] | 47 | absl::string_view file, |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 48 | int line, |
Jordan Bayles | d08f8d2 | 2019-01-17 15:06:02 -0800 | [diff] [blame] | 49 | absl::string_view msg); |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame^] | 50 | |
| 51 | // Breaks into the debugger, if one is present. Otherwise, aborts the current |
| 52 | // process (i.e., this function should not return). In production builds, an |
| 53 | // embedder could invoke its infrastructure for performing "dumps," consisting |
| 54 | // of thread stack traces and other relevant process state information, before |
| 55 | // aborting the process. |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 56 | void Break(); |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 57 | |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 58 | } // namespace platform |
| 59 | } // namespace openscreen |
| 60 | |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame^] | 61 | // Convenience macros and inline code that Open Screen code uses to invoke the |
| 62 | // above embedder-implemented functions. |
| 63 | // |
| 64 | // TODO(crbug.com/openscreen/77): Remove this, move logging_macros.h to |
| 65 | // util/logging.h, and search-and-replace all #includes throughout the code |
| 66 | // base. |
| 67 | #include "platform/api/internal/logging_macros.h" // NOLINT |
| 68 | |
btolsch | a21e8ed | 2018-08-30 15:13:48 -0700 | [diff] [blame] | 69 | #endif // PLATFORM_API_LOGGING_H_ |