blob: fd54744fdf747232535c0667ef56562e1cea547f [file] [log] [blame]
Andrei Homescu74a54452021-12-10 05:30:21 +00001/*
2 * Copyright (C) 2022 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
17#define TLOG_TAG "libbinder"
18
19#include "android-base/logging.h"
20
21#include <trusty_log.h>
22#include <iostream>
23#include <string>
24
25#include <android-base/macros.h>
26#include <android-base/strings.h>
27
28namespace android {
29namespace base {
30
31static const char* GetFileBasename(const char* file) {
32 const char* last_slash = strrchr(file, '/');
33 if (last_slash != nullptr) {
34 return last_slash + 1;
35 }
36 return file;
37}
38
39// This splits the message up line by line, by calling log_function with a pointer to the start of
40// each line and the size up to the newline character. It sends size = -1 for the final line.
41template <typename F, typename... Args>
42static void SplitByLines(const char* msg, const F& log_function, Args&&... args) {
43 const char* newline;
44 while ((newline = strchr(msg, '\n')) != nullptr) {
45 log_function(msg, newline - msg, args...);
46 msg = newline + 1;
47 }
48
49 log_function(msg, -1, args...);
50}
51
52void DefaultAborter(const char* abort_message) {
53 TLOGC("aborting: %s\n", abort_message);
54 abort();
55}
56
57static void TrustyLogLine(const char* msg, int length, android::base::LogSeverity severity,
58 const char* tag) {
59 switch (severity) {
60 case VERBOSE:
61 case DEBUG:
62 TLOGD("%s: %s\n", tag, msg);
63 break;
64 case INFO:
65 TLOGI("%s: %s\n", tag, msg);
66 break;
67 case WARNING:
68 TLOGW("%s: %s\n", tag, msg);
69 break;
70 case ERROR:
71 TLOGE("%s: %s\n", tag, msg);
72 break;
73 case FATAL_WITHOUT_ABORT:
74 case FATAL:
75 TLOGC("%s: %s\n", tag, msg);
76 break;
77 }
78}
79
80void TrustyLogger(android::base::LogId, android::base::LogSeverity severity, const char* tag,
81 const char*, unsigned int, const char* full_message) {
82 SplitByLines(full_message, TrustyLogLine, severity, tag);
83}
84
85// This indirection greatly reduces the stack impact of having lots of
86// checks/logging in a function.
87class LogMessageData {
88public:
89 LogMessageData(const char* file, unsigned int line, LogSeverity severity, const char* tag,
90 int error)
91 : file_(GetFileBasename(file)),
92 line_number_(line),
93 severity_(severity),
94 tag_(tag),
95 error_(error) {}
96
97 const char* GetFile() const { return file_; }
98
99 unsigned int GetLineNumber() const { return line_number_; }
100
101 LogSeverity GetSeverity() const { return severity_; }
102
103 const char* GetTag() const { return tag_; }
104
105 int GetError() const { return error_; }
106
107 std::ostream& GetBuffer() { return buffer_; }
108
109 std::string ToString() const { return buffer_.str(); }
110
111private:
112 std::ostringstream buffer_;
113 const char* const file_;
114 const unsigned int line_number_;
115 const LogSeverity severity_;
116 const char* const tag_;
117 const int error_;
118
119 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
120};
121
122LogMessage::LogMessage(const char* file, unsigned int line, LogId, LogSeverity severity,
123 const char* tag, int error)
124 : LogMessage(file, line, severity, tag, error) {}
125
126LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, const char* tag,
127 int error)
128 : data_(new LogMessageData(file, line, severity, tag, error)) {}
129
130LogMessage::~LogMessage() {
131 // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
132 if (!WOULD_LOG(data_->GetSeverity())) {
133 return;
134 }
135
136 // Finish constructing the message.
137 if (data_->GetError() != -1) {
138 data_->GetBuffer() << ": " << strerror(data_->GetError());
139 }
140 std::string msg(data_->ToString());
141
142 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), data_->GetTag(),
143 msg.c_str());
144
145 // Abort if necessary.
146 if (data_->GetSeverity() == FATAL) {
147 DefaultAborter(msg.c_str());
148 }
149}
150
151std::ostream& LogMessage::stream() {
152 return data_->GetBuffer();
153}
154
155void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity severity, const char* tag,
156 const char* message) {
157 TrustyLogger(DEFAULT, severity, tag ?: "<unknown>", file, line, message);
158}
159
160bool ShouldLog(LogSeverity severity, const char* tag) {
161 // This is controlled by Trusty's log level.
162 return true;
163}
164
165} // namespace base
166} // namespace android