Android-cuttlefish cvd tool
tee_logging.h
Go to the documentation of this file.
1//
2// Copyright (C) 2020 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <memory>
19#include <optional>
20#include <string_view>
21#include <vector>
22
25
26namespace cuttlefish {
27
28constexpr const char* kConsoleSeverityEnvVar = "CF_CONSOLE_SEVERITY";
29constexpr const char* kFileSeverityEnvVar = "CF_FILE_SEVERITY";
30
31enum class LogSeverity : int {
32 Verbose = 0,
33 Debug,
34 Info,
35 Warning,
36 Error,
37 Fatal,
38};
39
40std::string FromSeverity(LogSeverity severity);
41Result<LogSeverity> ToSeverity(std::string_view value);
42
43std::string StripColorCodes(const std::string& str);
44
47
49
54
56 const std::string& path,
59
63};
64
65// Set the new logging destinations, replacing existing ones.
66void SetLoggers(std::vector<SeverityTarget> destinations,
67 const std::string& log_prefix = "");
68
69// Configure the process to only log to stderr.
70void LogToStderr(const std::string& log_prefix = "",
72 std::optional<LogSeverity> severity = std::nullopt);
73
74// Configure process to log to a list of files. Logs of all severities are
75// always written in full.
76void LogToFiles(const std::vector<std::string>& files,
77 const std::string& log_prefix = "");
78
79// Configure the process to log to stderr and some files. Only the severity and
80// metadata for the stderr logger can be configured, full logs will be written
81// to the files.
83 const std::vector<std::string>& files, const std::string& log_prefix = "",
85 std::optional<LogSeverity> stderr_severity = std::nullopt);
86
87class LogSink;
88// Adds an extra destination for this process's logs for the duration of the
89// lifetime of this logger. Existing logging destinations are not affected.
91 public:
92 ScopedLogger(SeverityTarget target, const std::string& prefix);
94
95 private:
96 std::unique_ptr<LogSink> log_sink_;
97};
98
99} // namespace cuttlefish
Definition: tee_logging.cpp:211
Definition: tee_logging.h:90
~ScopedLogger()
Definition: tee_logging.cpp:377
ScopedLogger(SeverityTarget target, const std::string &prefix)
Definition: tee_logging.cpp:372
std::unique_ptr< LogSink > log_sink_
Definition: tee_logging.h:96
Definition: shared_fd.h:124
std::string_view prefix
Definition: kernel_log_server.cc:44
Definition: alloc_driver.h:20
MetadataLevel
Definition: tee_logging.h:48
constexpr const char * kFileSeverityEnvVar
Definition: tee_logging.h:29
std::string StripColorCodes(const std::string &str)
Definition: tee_logging.cpp:176
LogSeverity
Definition: tee_logging.h:31
std::string FromSeverity(LogSeverity severity)
Definition: tee_logging.cpp:265
void LogToStderrAndFiles(const std::vector< std::string > &files, const std::string &log_prefix, MetadataLevel stderr_level, std::optional< LogSeverity > stderr_severity)
Definition: tee_logging.cpp:361
void LogToStderr(const std::string &log_prefix, MetadataLevel metadata_level, std::optional< LogSeverity > severity)
Definition: tee_logging.cpp:351
void SetLoggers(std::vector< SeverityTarget > destinations, const std::string &log_prefix)
Definition: tee_logging.cpp:325
void LogToFiles(const std::vector< std::string > &files, const std::string &log_prefix)
Definition: tee_logging.cpp:356
LogSeverity ConsoleSeverity()
Definition: tee_logging.cpp:312
constexpr const char * kConsoleSeverityEnvVar
Definition: tee_logging.h:28
Result< LogSeverity > ToSeverity(std::string_view value)
Definition: tee_logging.cpp:283
LogSeverity LogFileSeverity()
Definition: tee_logging.cpp:316
constexpr const char * str
Definition: utils.h:180
Definition: tee_logging.h:50
LogSeverity severity
Definition: tee_logging.h:51
static SeverityTarget FromFd(SharedFD fd, MetadataLevel metadata_level=MetadataLevel::FULL, LogSeverity severity=LogSeverity::Verbose)
Definition: tee_logging.cpp:206
MetadataLevel metadata_level
Definition: tee_logging.h:53
static SeverityTarget FromFile(const std::string &path, MetadataLevel metadata_level=MetadataLevel::FULL, LogSeverity severity=LogSeverity::Verbose)
Definition: tee_logging.cpp:193
SharedFD target
Definition: tee_logging.h:52