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 <string>
19 #include <vector>
20 
21 #include <android-base/logging.h>
22 
23 #include "common/libs/fs/shared_fd.h"
24 #include "common/libs/utils/result.h"
25 
26 namespace cuttlefish {
27 
28 std::string FromSeverity(const android::base::LogSeverity severity);
29 Result<android::base::LogSeverity> ToSeverity(const std::string& value);
30 
31 std::string StderrOutputGenerator(const struct tm& now, int pid, uint64_t tid,
32                                   android::base::LogSeverity severity,
33                                   const char* tag, const char* file,
34                                   unsigned int line, const char* message);
35 
36 android::base::LogSeverity ConsoleSeverity();
37 android::base::LogSeverity LogFileSeverity();
38 
39 enum class MetadataLevel {
40   FULL,
41   ONLY_MESSAGE,
42   TAG_AND_MESSAGE
43 };
44 
45 struct SeverityTarget {
46   android::base::LogSeverity severity;
47   SharedFD target;
48   MetadataLevel metadata_level;
49 };
50 
51 class TeeLogger {
52 private:
53   std::vector<SeverityTarget> destinations_;
54 public:
55  TeeLogger(const std::vector<SeverityTarget>& destinations,
56            const std::string& log_prefix = "");
57  ~TeeLogger() = default;
58 
59  void operator()(android::base::LogId log_id,
60                  android::base::LogSeverity severity, const char* tag,
61                  const char* file, unsigned int line, const char* message);
62 
63 private:
64  std::string prefix_;
65 };
66 
67 TeeLogger LogToFiles(const std::vector<std::string>& files,
68                      const std::string& log_prefix = "");
69 TeeLogger LogToStderrAndFiles(const std::vector<std::string>& files,
70                               const std::string& log_prefix = "",
71                               MetadataLevel stderr_level = MetadataLevel::ONLY_MESSAGE);
72 
73 std::string StripColorCodes(const std::string& str);
74 
75 } // namespace cuttlefish
76