blob: 7a68a8ff56ea7213d33e13b87f4822efe6f04c61 [file] [log] [blame]
Erik Klineb31fd692018-06-06 20:50:11 +09001/*
2 * Copyright (C) 2018 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#ifndef NETUTILS_LOG_H
18#define NETUTILS_LOG_H
19
20#include <chrono>
21#include <deque>
22#include <shared_mutex>
23#include <string>
24
25#include <android-base/stringprintf.h>
26#include <android-base/thread_annotations.h>
27
28#include <netdutils/Status.h>
29
30namespace android {
31namespace netdutils {
32
33class LogEntry {
34 public:
35 LogEntry() = default;
36 LogEntry(const LogEntry&) = default;
37 LogEntry(LogEntry&&) = default;
38 ~LogEntry() = default;
39 LogEntry& operator=(const LogEntry&) = default;
40 LogEntry& operator=(LogEntry&&) = default;
41
42 std::string toString() const;
43
44 ///
45 // Helper methods that make it easy to build up a LogEntry message.
46 // If performance becomes a factor the implementations could be inlined.
47 ///
48 LogEntry& message(const std::string& message);
49
50 // For calling with __FUNCTION__.
51 LogEntry& function(const std::string& function_name);
52 // For calling with __PRETTY_FUNCTION__.
53 LogEntry& prettyFunction(const std::string& pretty_function);
54
55 // Convenience methods for each of the common types of function arguments.
56 LogEntry& arg(const std::string& val);
57 LogEntry& arg(const std::vector<int32_t>& val);
58 LogEntry& arg(const std::vector<uint8_t>& val);
59 LogEntry& arg(const std::vector<std::string>& val);
60 LogEntry& arg(bool val);
61 template <class T>
62 LogEntry& arg(T val) {
63 mArgs.push_back(std::to_string(val));
64 return *this;
65 }
66
67 // Some things can return more than one value, or have multiple output
68 // parameters, so each of these adds to the mReturns vector.
69 LogEntry& returns(const std::string& rval);
70 LogEntry& returns(const Status& status);
71 LogEntry& returns(bool rval);
72 template <class T>
73 LogEntry& returns(T val) {
74 mReturns.push_back(std::to_string(val));
75 return *this;
76 }
77
78 LogEntry& withUid(uid_t uid);
79
80 // Append the duration computed since the creation of this instance.
81 LogEntry& withAutomaticDuration();
82 // Append the string-ified duration computed by some other means.
83 LogEntry& withDuration(const std::string& duration);
84
85 private:
86 std::chrono::steady_clock::time_point mStart = std::chrono::steady_clock::now();
87 std::string mMsg{};
88 std::string mFunc{};
89 std::vector<std::string> mArgs{};
90 std::vector<std::string> mReturns{};
91 std::string mUid{};
92 std::string mDuration{};
93};
94
95class Log {
96 public:
97 Log() = delete;
98 Log(const std::string& tag) : Log(tag, MAX_ENTRIES) {}
99 Log(const std::string& tag, size_t maxEntries) : mTag(tag), mMaxEntries(maxEntries) {}
100 Log(const Log&) = delete;
101 Log(Log&&) = delete;
102 ~Log();
103 Log& operator=(const Log&) = delete;
104 Log& operator=(Log&&) = delete;
105
106 LogEntry newEntry() const { return LogEntry(); }
107
108 // Record a log entry in internal storage only.
109 void log(const std::string& entry) { record(Level::LOG, entry); }
110 template <size_t n>
111 void log(const char entry[n]) { log(std::string(entry)); }
112 void log(const LogEntry& entry) { log(entry.toString()); }
113 void log(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
114 using ::android::base::StringAppendV;
115 std::string result;
116 va_list ap;
117 va_start(ap, fmt);
118 StringAppendV(&result, fmt, ap);
119 va_end(ap);
120 log(result);
121 }
122
123 // Record a log entry in internal storage and to ALOGI as well.
124 void info(const std::string& entry) { record(Level::INFO, entry); }
125 template <size_t n>
126 void info(const char entry[n]) { info(std::string(entry)); }
127 void info(const LogEntry& entry) { info(entry.toString()); }
128 void info(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
129 using ::android::base::StringAppendV;
130 std::string result;
131 va_list ap;
132 va_start(ap, fmt);
133 StringAppendV(&result, fmt, ap);
134 va_end(ap);
135 info(result);
136 }
137
138 // Record a log entry in internal storage and to ALOGW as well.
139 void warn(const std::string& entry) { record(Level::WARN, entry); }
140 template <size_t n>
141 void warn(const char entry[n]) { warn(std::string(entry)); }
142 void warn(const LogEntry& entry) { warn(entry.toString()); }
143 void warn(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
144 using ::android::base::StringAppendV;
145 std::string result;
146 va_list ap;
147 va_start(ap, fmt);
148 StringAppendV(&result, fmt, ap);
149 va_end(ap);
150 warn(result);
151 }
152
153 // Record a log entry in internal storage and to ALOGE as well.
154 void error(const std::string& entry) { record(Level::ERROR, entry); }
155 template <size_t n>
156 void error(const char entry[n]) { error(std::string(entry)); }
157 void error(const LogEntry& entry) { error(entry.toString()); }
158 void error(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
159 using ::android::base::StringAppendV;
160 std::string result;
161 va_list ap;
162 va_start(ap, fmt);
163 StringAppendV(&result, fmt, ap);
164 va_end(ap);
165 error(result);
166 }
167
168 // Iterates over every entry in the log in chronological order. Operates
169 // on a copy of the log entries, and so perEntryFn may itself call one of
170 // the logging functions if needed.
171 void forEachEntry(const std::function<void(const std::string&)>& perEntryFn) const;
172
173 private:
174 static constexpr const size_t MAX_ENTRIES = 750U;
175 const std::string mTag;
176 const size_t mMaxEntries;
177
178 // The LOG level adds an entry to mEntries but does not output the message
179 // to the system log. All other levels append to mEntries and output to the
180 // the system log.
181 enum class Level {
182 LOG,
183 INFO,
184 WARN,
185 ERROR,
186 };
187
188 void record(Level lvl, const std::string& entry);
189
190 mutable std::shared_mutex mLock;
191 std::deque<const std::string> mEntries; // GUARDED_BY(mLock), when supported
192};
193
194} // namespace netdutils
195} // namespace android
196
197#endif /* NETUTILS_LOG_H */