Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame^] | 1 | /* |
| 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 | #include "netdutils/Log.h" |
| 18 | |
| 19 | #include <chrono> |
| 20 | #include <ctime> |
| 21 | #include <iomanip> |
| 22 | #include <mutex> |
| 23 | #include <sstream> |
| 24 | |
| 25 | #include <android-base/strings.h> |
| 26 | #include <log/log.h> |
| 27 | |
| 28 | using ::android::base::Join; |
| 29 | using ::android::base::StringPrintf; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace netdutils { |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | std::string makeTimestampedEntry(const std::string& entry) { |
| 37 | using ::std::chrono::duration_cast; |
| 38 | using ::std::chrono::milliseconds; |
| 39 | using ::std::chrono::system_clock; |
| 40 | |
| 41 | std::stringstream tsEntry; |
| 42 | const auto now = system_clock::now(); |
| 43 | const auto time_sec = system_clock::to_time_t(now); |
| 44 | tsEntry << std::put_time(std::localtime(&time_sec), "%m-%d %H:%M:%S.") << std::setw(3) |
| 45 | << std::setfill('0') |
| 46 | << duration_cast<milliseconds>(now - system_clock::from_time_t(time_sec)).count() << " " |
| 47 | << entry; |
| 48 | |
| 49 | return tsEntry.str(); |
| 50 | } |
| 51 | |
| 52 | } // namespace |
| 53 | |
| 54 | std::string LogEntry::toString() const { |
| 55 | std::vector<std::string> text; |
| 56 | |
| 57 | if (!mMsg.empty()) text.push_back(mMsg); |
| 58 | if (!mFunc.empty()) { |
| 59 | text.push_back(StringPrintf("%s(%s)", mFunc.c_str(), Join(mArgs, ", ").c_str())); |
| 60 | } |
| 61 | if (!mReturns.empty()) { |
| 62 | text.push_back("->"); |
| 63 | text.push_back(StringPrintf("(%s)", Join(mReturns, ", ").c_str())); |
| 64 | } |
| 65 | if (!mUid.empty()) text.push_back(mUid); |
| 66 | if (!mDuration.empty()) text.push_back(StringPrintf("(%s)", mDuration.c_str())); |
| 67 | |
| 68 | return Join(text, " "); |
| 69 | } |
| 70 | |
| 71 | LogEntry& LogEntry::message(const std::string& message) { |
| 72 | mMsg = message; |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | LogEntry& LogEntry::function(const std::string& function_name) { |
| 77 | mFunc = function_name; |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | LogEntry& LogEntry::prettyFunction(const std::string& pretty_function) { |
| 82 | // __PRETTY_FUNCTION__ generally seems to be of the form: |
| 83 | // |
| 84 | // qualifed::returnType qualified::function(args...) |
| 85 | // |
| 86 | // where the qualified forms include "(anonymous namespace)" in the |
| 87 | // "::"-delimited list and keywords like "virtual" (where applicable). |
| 88 | // |
| 89 | // Here we try to convert strings like: |
| 90 | // |
| 91 | // virtual binder::Status android::net::NetdNativeService::isAlive(bool *) |
| 92 | // netdutils::LogEntry android::netd::(anonymous namespace)::AAA::BBB::function() |
| 93 | // |
| 94 | // into just "NetdNativeService::isAlive" or "BBB::function". Note that |
| 95 | // without imposing convention, how to easily identify any namespace/class |
| 96 | // name boundary is not obvious. |
| 97 | const size_t endFuncName = pretty_function.rfind('('); |
| 98 | const size_t precedingSpace = pretty_function.rfind(' ', endFuncName); |
| 99 | size_t substrStart = (precedingSpace != std::string::npos) ? precedingSpace + 1 : 0; |
| 100 | |
| 101 | const size_t beginFuncName = pretty_function.rfind("::", endFuncName); |
| 102 | if (beginFuncName != std::string::npos && substrStart < beginFuncName) { |
| 103 | const size_t previousNameBoundary = pretty_function.rfind("::", beginFuncName - 1); |
| 104 | if (previousNameBoundary < beginFuncName && substrStart < previousNameBoundary) { |
| 105 | substrStart = previousNameBoundary + 2; |
| 106 | } else { |
| 107 | substrStart = beginFuncName + 2; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | mFunc = pretty_function.substr(substrStart, endFuncName - substrStart); |
| 112 | return *this; |
| 113 | } |
| 114 | |
| 115 | LogEntry& LogEntry::arg(const std::string& val) { |
| 116 | mArgs.push_back(val.empty() ? "\"\"" : val); |
| 117 | return *this; |
| 118 | } |
| 119 | |
| 120 | LogEntry& LogEntry::arg(bool val) { |
| 121 | mArgs.push_back(val ? "true" : "false"); |
| 122 | return *this; |
| 123 | } |
| 124 | |
| 125 | LogEntry& LogEntry::arg(const std::vector<int32_t>& val) { |
| 126 | mArgs.push_back(StringPrintf("[%s]", Join(val, ", ").c_str())); |
| 127 | return *this; |
| 128 | } |
| 129 | |
| 130 | // LogEntry& LogEntry::arg(const std::vector<uint8_t>& val); |
| 131 | |
| 132 | LogEntry& LogEntry::arg(const std::vector<std::string>& val) { |
| 133 | mArgs.push_back(StringPrintf("[%s]", Join(val, ", ").c_str())); |
| 134 | return *this; |
| 135 | } |
| 136 | |
| 137 | LogEntry& LogEntry::returns(const std::string& rval) { |
| 138 | mReturns.push_back(rval); |
| 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | LogEntry& LogEntry::returns(bool rval) { |
| 143 | mReturns.push_back(rval ? "true" : "false"); |
| 144 | return *this; |
| 145 | } |
| 146 | |
| 147 | LogEntry& LogEntry::returns(const Status& status) { |
| 148 | mReturns.push_back(status.msg()); |
| 149 | return *this; |
| 150 | } |
| 151 | |
| 152 | LogEntry& LogEntry::withUid(uid_t uid) { |
| 153 | mUid = StringPrintf("(uid=%d)", uid); |
| 154 | return *this; |
| 155 | } |
| 156 | |
| 157 | LogEntry& LogEntry::withAutomaticDuration() { |
| 158 | using ms = std::chrono::duration<float, std::ratio<1, 1000>>; |
| 159 | |
| 160 | const std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); |
| 161 | std::stringstream duration; |
| 162 | duration << std::setprecision(1) << std::chrono::duration_cast<ms>(end - mStart).count() |
| 163 | << "ms"; |
| 164 | mDuration = duration.str(); |
| 165 | return *this; |
| 166 | } |
| 167 | |
| 168 | LogEntry& LogEntry::withDuration(const std::string& duration) { |
| 169 | mDuration = duration; |
| 170 | return *this; |
| 171 | } |
| 172 | |
| 173 | Log::~Log() { |
| 174 | // TODO: dump the last N entries to the android log for possible posterity. |
| 175 | info(LogEntry().function(__FUNCTION__)); |
| 176 | } |
| 177 | |
| 178 | void Log::forEachEntry(const std::function<void(const std::string&)>& perEntryFn) const { |
| 179 | // We make a (potentially expensive) copy of the log buffer (including |
| 180 | // all strings), in case the |perEntryFn| takes its sweet time. |
| 181 | std::deque<std::string> entries; |
| 182 | { |
| 183 | std::shared_lock<std::shared_mutex> guard(mLock); |
| 184 | entries.assign(mEntries.cbegin(), mEntries.cend()); |
| 185 | } |
| 186 | |
| 187 | for (const std::string& entry : entries) perEntryFn(entry); |
| 188 | } |
| 189 | |
| 190 | void Log::record(Log::Level lvl, const std::string& entry) { |
| 191 | switch (lvl) { |
| 192 | case Level::LOG: |
| 193 | break; |
| 194 | case Level::INFO: |
| 195 | ALOG(LOG_INFO, mTag.c_str(), "%s", entry.c_str()); |
| 196 | break; |
| 197 | case Level::WARN: |
| 198 | ALOG(LOG_WARN, mTag.c_str(), "%s", entry.c_str()); |
| 199 | break; |
| 200 | case Level::ERROR: |
| 201 | ALOG(LOG_ERROR, mTag.c_str(), "%s", entry.c_str()); |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | std::lock_guard<std::shared_mutex> guard(mLock); |
| 206 | mEntries.push_back(makeTimestampedEntry(entry)); |
| 207 | while (mEntries.size() > mMaxEntries) mEntries.pop_front(); |
| 208 | } |
| 209 | |
| 210 | } // namespace netdutils |
| 211 | } // namespace android |