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