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