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 <vector>
21
22#include "absl/strings/string_view.h"
23
26
27namespace cuttlefish {
28
29constexpr const char* kConsoleSeverityEnvVar = "CF_CONSOLE_SEVERITY";
30constexpr const char* kFileSeverityEnvVar = "CF_FILE_SEVERITY";
31
32enum class LogSeverity : int {
33 Verbose = 0,
34 Debug,
35 Info,
36 Warning,
37 Error,
38 Fatal,
39};
40
41std::string FromSeverity(LogSeverity severity);
42Result<LogSeverity> ToSeverity(absl::string_view value);
43
44std::string StripColorCodes(const std::string& str);
45
48
50
55
57 const std::string& path,
60
64};
65
66// Set the new logging destinations, replacing existing ones.
67void SetLoggers(std::vector<SeverityTarget> destinations,
68 const std::string& log_prefix = "");
69
70// Configure the process to only log to stderr.
71void LogToStderr(const std::string& log_prefix = "",
73 std::optional<LogSeverity> severity = std::nullopt);
74
75// Configure process to log to a list of files. Logs of all severities are
76// always written in full.
77void LogToFiles(const std::vector<std::string>& files,
78 const std::string& log_prefix = "");
79
80// Configure the process to log to stderr and some files. Only the severity and
81// metadata for the stderr logger can be configured, full logs will be written
82// to the files.
84 const std::vector<std::string>& files, const std::string& log_prefix = "",
86 std::optional<LogSeverity> stderr_severity = std::nullopt);
87
88class LogSink;
89// Adds an extra destination for this process's logs for the duration of the
90// lifetime of this logger. Existing logging destinations are not affected.
92 public:
93 ScopedLogger(SeverityTarget target, const std::string& prefix);
95
96 private:
97 std::unique_ptr<LogSink> log_sink_;
98};
99
100} // namespace cuttlefish
Definition: tee_logging.cpp:211
Definition: tee_logging.h:91
~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:97
Definition: shared_fd.h:125
absl::string_view prefix
Definition: kernel_log_server.cc:45
Definition: alloc_driver.h:21
MetadataLevel
Definition: tee_logging.h:49
constexpr const char * kFileSeverityEnvVar
Definition: tee_logging.h:30
std::string StripColorCodes(const std::string &str)
Definition: tee_logging.cpp:176
LogSeverity
Definition: tee_logging.h:32
Result< LogSeverity > ToSeverity(absl::string_view value)
Definition: tee_logging.cpp:282
std::string FromSeverity(LogSeverity severity)
Definition: tee_logging.cpp:264
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:360
void LogToStderr(const std::string &log_prefix, MetadataLevel metadata_level, std::optional< LogSeverity > severity)
Definition: tee_logging.cpp:350
void SetLoggers(std::vector< SeverityTarget > destinations, const std::string &log_prefix)
Definition: tee_logging.cpp:324
void LogToFiles(const std::vector< std::string > &files, const std::string &log_prefix)
Definition: tee_logging.cpp:355
LogSeverity ConsoleSeverity()
Definition: tee_logging.cpp:311
constexpr const char * kConsoleSeverityEnvVar
Definition: tee_logging.h:29
LogSeverity LogFileSeverity()
Definition: tee_logging.cpp:315
Definition: tee_logging.h:51
LogSeverity severity
Definition: tee_logging.h:52
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:54
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:53