blob: a958ce5a06a45a3b988be527a534628024ce162f [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
Yuri Wiitala75ea15d2019-12-03 16:01:48 -08008#include <sstream>
Jordan Baylesd08f8d22019-01-17 15:06:02 -08009
btolsch9d6900c2018-05-30 18:22:53 -070010namespace openscreen {
btolsch9d6900c2018-05-30 18:22:53 -070011
12enum class LogLevel {
Yuri Wiitalab7979032019-11-08 15:17:20 -080013 // Very detailed information, often used for evaluating performance or
14 // debugging production issues in-the-wild.
btolsch9d6900c2018-05-30 18:22:53 -070015 kVerbose = 0,
Yuri Wiitalab7979032019-11-08 15:17:20 -080016
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,
btolsch9d6900c2018-05-30 18:22:53 -070034};
35
Yuri Wiitalab7979032019-11-08 15:17:20 -080036// 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 Wiitala75ea15d2019-12-03 16:01:48 -080038bool IsLoggingOn(LogLevel level, const char* file);
btolsch9d6900c2018-05-30 18:22:53 -070039
Yuri Wiitalab7979032019-11-08 15:17:20 -080040// 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 Wiitala75ea15d2019-12-03 16:01:48 -080045//
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.
btolsch9d6900c2018-05-30 18:22:53 -070048void LogWithLevel(LogLevel level,
Yuri Wiitala75ea15d2019-12-03 16:01:48 -080049 const char* file,
btolsch9d6900c2018-05-30 18:22:53 -070050 int line,
Yuri Wiitala75ea15d2019-12-03 16:01:48 -080051 std::stringstream message);
Yuri Wiitalab7979032019-11-08 15:17:20 -080052
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 Bayles759af7f2020-11-24 12:09:04 -080058[[noreturn]] void Break();
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080059
btolsch9d6900c2018-05-30 18:22:53 -070060} // namespace openscreen
61
btolscha21e8ed2018-08-30 15:13:48 -070062#endif // PLATFORM_API_LOGGING_H_