• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include <sstream>
9 
10 namespace openscreen {
11 
12 enum class LogLevel {
13   // Very detailed information, often used for evaluating performance or
14   // debugging production issues in-the-wild.
15   kVerbose = 0,
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,
34 };
35 
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|.
38 bool IsLoggingOn(LogLevel level, const char* file);
39 
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.
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.
48 void LogWithLevel(LogLevel level,
49                   const char* file,
50                   int line,
51                   std::stringstream message);
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.
58 [[noreturn]] void Break();
59 
60 }  // namespace openscreen
61 
62 #endif  // PLATFORM_API_LOGGING_H_
63