logging.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (C) 2015 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. #if defined(_WIN32)
  17. #include <windows.h>
  18. #endif
  19. #include "android-base/logging.h"
  20. #include <fcntl.h>
  21. #include <inttypes.h>
  22. #include <libgen.h>
  23. #include <time.h>
  24. // For getprogname(3) or program_invocation_short_name.
  25. #if defined(__ANDROID__) || defined(__APPLE__)
  26. #include <stdlib.h>
  27. #elif defined(__GLIBC__)
  28. #include <errno.h>
  29. #endif
  30. #if defined(__linux__)
  31. #include <sys/uio.h>
  32. #endif
  33. #include <iostream>
  34. #include <limits>
  35. #include <mutex>
  36. #include <sstream>
  37. #include <string>
  38. #include <utility>
  39. #include <vector>
  40. // Headers for LogMessage::LogLine.
  41. #ifdef __ANDROID__
  42. #include <android/log.h>
  43. #include <android/set_abort_message.h>
  44. #else
  45. #include <sys/types.h>
  46. #include <unistd.h>
  47. #endif
  48. #include <android-base/file.h>
  49. #include <android-base/macros.h>
  50. #include <android-base/parseint.h>
  51. #include <android-base/strings.h>
  52. #include <android-base/threads.h>
  53. namespace android {
  54. namespace base {
  55. // BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
  56. #if defined(__GLIBC__) || defined(_WIN32)
  57. static const char* getprogname() {
  58. #if defined(__GLIBC__)
  59. return program_invocation_short_name;
  60. #elif defined(_WIN32)
  61. static bool first = true;
  62. static char progname[MAX_PATH] = {};
  63. if (first) {
  64. snprintf(progname, sizeof(progname), "%s",
  65. android::base::Basename(android::base::GetExecutablePath()).c_str());
  66. first = false;
  67. }
  68. return progname;
  69. #endif
  70. }
  71. #endif
  72. static const char* GetFileBasename(const char* file) {
  73. // We can't use basename(3) even on Unix because the Mac doesn't
  74. // have a non-modifying basename.
  75. const char* last_slash = strrchr(file, '/');
  76. if (last_slash != nullptr) {
  77. return last_slash + 1;
  78. }
  79. #if defined(_WIN32)
  80. const char* last_backslash = strrchr(file, '\\');
  81. if (last_backslash != nullptr) {
  82. return last_backslash + 1;
  83. }
  84. #endif
  85. return file;
  86. }
  87. #if defined(__linux__)
  88. static int OpenKmsg() {
  89. #if defined(__ANDROID__)
  90. // pick up 'file w /dev/kmsg' environment from daemon's init rc file
  91. const auto val = getenv("ANDROID_FILE__dev_kmsg");
  92. if (val != nullptr) {
  93. int fd;
  94. if (android::base::ParseInt(val, &fd, 0)) {
  95. auto flags = fcntl(fd, F_GETFL);
  96. if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd;
  97. }
  98. }
  99. #endif
  100. return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
  101. }
  102. #endif
  103. static std::mutex& LoggingLock() {
  104. static auto& logging_lock = *new std::mutex();
  105. return logging_lock;
  106. }
  107. static LogFunction& Logger() {
  108. #ifdef __ANDROID__
  109. static auto& logger = *new LogFunction(LogdLogger());
  110. #else
  111. static auto& logger = *new LogFunction(StderrLogger);
  112. #endif
  113. return logger;
  114. }
  115. static AbortFunction& Aborter() {
  116. static auto& aborter = *new AbortFunction(DefaultAborter);
  117. return aborter;
  118. }
  119. static std::recursive_mutex& TagLock() {
  120. static auto& tag_lock = *new std::recursive_mutex();
  121. return tag_lock;
  122. }
  123. static std::string* gDefaultTag;
  124. std::string GetDefaultTag() {
  125. std::lock_guard<std::recursive_mutex> lock(TagLock());
  126. if (gDefaultTag == nullptr) {
  127. return "";
  128. }
  129. return *gDefaultTag;
  130. }
  131. void SetDefaultTag(const std::string& tag) {
  132. std::lock_guard<std::recursive_mutex> lock(TagLock());
  133. if (gDefaultTag != nullptr) {
  134. delete gDefaultTag;
  135. gDefaultTag = nullptr;
  136. }
  137. if (!tag.empty()) {
  138. gDefaultTag = new std::string(tag);
  139. }
  140. }
  141. static bool gInitialized = false;
  142. static LogSeverity gMinimumLogSeverity = INFO;
  143. #if defined(__linux__)
  144. void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
  145. const char* tag, const char*, unsigned int, const char* msg) {
  146. // clang-format off
  147. static constexpr int kLogSeverityToKernelLogLevel[] = {
  148. [android::base::VERBOSE] = 7, // KERN_DEBUG (there is no verbose kernel log
  149. // level)
  150. [android::base::DEBUG] = 7, // KERN_DEBUG
  151. [android::base::INFO] = 6, // KERN_INFO
  152. [android::base::WARNING] = 4, // KERN_WARNING
  153. [android::base::ERROR] = 3, // KERN_ERROR
  154. [android::base::FATAL_WITHOUT_ABORT] = 2, // KERN_CRIT
  155. [android::base::FATAL] = 2, // KERN_CRIT
  156. };
  157. // clang-format on
  158. static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1,
  159. "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity");
  160. static int klog_fd = OpenKmsg();
  161. if (klog_fd == -1) return;
  162. int level = kLogSeverityToKernelLogLevel[severity];
  163. // The kernel's printk buffer is only 1024 bytes.
  164. // TODO: should we automatically break up long lines into multiple lines?
  165. // Or we could log but with something like "..." at the end?
  166. char buf[1024];
  167. size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg);
  168. if (size > sizeof(buf)) {
  169. size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n",
  170. level, tag, size);
  171. }
  172. iovec iov[1];
  173. iov[0].iov_base = buf;
  174. iov[0].iov_len = size;
  175. TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1));
  176. }
  177. #endif
  178. void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line,
  179. const char* message) {
  180. struct tm now;
  181. time_t t = time(nullptr);
  182. #if defined(_WIN32)
  183. localtime_s(&now, &t);
  184. #else
  185. localtime_r(&t, &now);
  186. #endif
  187. char timestamp[32];
  188. strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
  189. static const char log_characters[] = "VDIWEFF";
  190. static_assert(arraysize(log_characters) - 1 == FATAL + 1,
  191. "Mismatch in size of log_characters and values in LogSeverity");
  192. char severity_char = log_characters[severity];
  193. fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n", tag ? tag : "nullptr", severity_char,
  194. timestamp, getpid(), GetThreadId(), file, line, message);
  195. }
  196. void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* /*file*/,
  197. unsigned int /*line*/, const char* message) {
  198. if (severity >= WARNING) {
  199. fflush(stdout);
  200. fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message);
  201. } else {
  202. fprintf(stdout, "%s\n", message);
  203. }
  204. }
  205. void DefaultAborter(const char* abort_message) {
  206. #ifdef __ANDROID__
  207. android_set_abort_message(abort_message);
  208. #else
  209. UNUSED(abort_message);
  210. #endif
  211. abort();
  212. }
  213. #ifdef __ANDROID__
  214. LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {
  215. }
  216. void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag,
  217. const char* file, unsigned int line,
  218. const char* message) {
  219. static constexpr android_LogPriority kLogSeverityToAndroidLogPriority[] = {
  220. ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
  221. ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL,
  222. ANDROID_LOG_FATAL,
  223. };
  224. static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
  225. "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
  226. int priority = kLogSeverityToAndroidLogPriority[severity];
  227. if (id == DEFAULT) {
  228. id = default_log_id_;
  229. }
  230. static constexpr log_id kLogIdToAndroidLogId[] = {
  231. LOG_ID_MAX, LOG_ID_MAIN, LOG_ID_SYSTEM,
  232. };
  233. static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1,
  234. "Mismatch in size of kLogIdToAndroidLogId and values in LogId");
  235. log_id lg_id = kLogIdToAndroidLogId[id];
  236. if (priority == ANDROID_LOG_FATAL) {
  237. __android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line,
  238. message);
  239. } else {
  240. __android_log_buf_print(lg_id, priority, tag, "%s", message);
  241. }
  242. }
  243. #endif
  244. void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
  245. SetLogger(std::forward<LogFunction>(logger));
  246. SetAborter(std::forward<AbortFunction>(aborter));
  247. if (gInitialized) {
  248. return;
  249. }
  250. gInitialized = true;
  251. // Stash the command line for later use. We can use /proc/self/cmdline on
  252. // Linux to recover this, but we don't have that luxury on the Mac/Windows,
  253. // and there are a couple of argv[0] variants that are commonly used.
  254. if (argv != nullptr) {
  255. SetDefaultTag(basename(argv[0]));
  256. }
  257. const char* tags = getenv("ANDROID_LOG_TAGS");
  258. if (tags == nullptr) {
  259. return;
  260. }
  261. std::vector<std::string> specs = Split(tags, " ");
  262. for (size_t i = 0; i < specs.size(); ++i) {
  263. // "tag-pattern:[vdiwefs]"
  264. std::string spec(specs[i]);
  265. if (spec.size() == 3 && StartsWith(spec, "*:")) {
  266. switch (spec[2]) {
  267. case 'v':
  268. gMinimumLogSeverity = VERBOSE;
  269. continue;
  270. case 'd':
  271. gMinimumLogSeverity = DEBUG;
  272. continue;
  273. case 'i':
  274. gMinimumLogSeverity = INFO;
  275. continue;
  276. case 'w':
  277. gMinimumLogSeverity = WARNING;
  278. continue;
  279. case 'e':
  280. gMinimumLogSeverity = ERROR;
  281. continue;
  282. case 'f':
  283. gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
  284. continue;
  285. // liblog will even suppress FATAL if you say 's' for silent, but that's
  286. // crazy!
  287. case 's':
  288. gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
  289. continue;
  290. }
  291. }
  292. LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags
  293. << ")";
  294. }
  295. }
  296. void SetLogger(LogFunction&& logger) {
  297. std::lock_guard<std::mutex> lock(LoggingLock());
  298. Logger() = std::move(logger);
  299. }
  300. void SetAborter(AbortFunction&& aborter) {
  301. std::lock_guard<std::mutex> lock(LoggingLock());
  302. Aborter() = std::move(aborter);
  303. }
  304. // This indirection greatly reduces the stack impact of having lots of
  305. // checks/logging in a function.
  306. class LogMessageData {
  307. public:
  308. LogMessageData(const char* file, unsigned int line, LogId id, LogSeverity severity,
  309. const char* tag, int error)
  310. : file_(GetFileBasename(file)),
  311. line_number_(line),
  312. id_(id),
  313. severity_(severity),
  314. tag_(tag),
  315. error_(error) {}
  316. const char* GetFile() const {
  317. return file_;
  318. }
  319. unsigned int GetLineNumber() const {
  320. return line_number_;
  321. }
  322. LogSeverity GetSeverity() const {
  323. return severity_;
  324. }
  325. const char* GetTag() const { return tag_; }
  326. LogId GetId() const {
  327. return id_;
  328. }
  329. int GetError() const {
  330. return error_;
  331. }
  332. std::ostream& GetBuffer() {
  333. return buffer_;
  334. }
  335. std::string ToString() const {
  336. return buffer_.str();
  337. }
  338. private:
  339. std::ostringstream buffer_;
  340. const char* const file_;
  341. const unsigned int line_number_;
  342. const LogId id_;
  343. const LogSeverity severity_;
  344. const char* const tag_;
  345. const int error_;
  346. DISALLOW_COPY_AND_ASSIGN(LogMessageData);
  347. };
  348. LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity,
  349. const char* tag, int error)
  350. : data_(new LogMessageData(file, line, id, severity, tag, error)) {}
  351. LogMessage::~LogMessage() {
  352. // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
  353. if (!WOULD_LOG(data_->GetSeverity())) {
  354. return;
  355. }
  356. // Finish constructing the message.
  357. if (data_->GetError() != -1) {
  358. data_->GetBuffer() << ": " << strerror(data_->GetError());
  359. }
  360. std::string msg(data_->ToString());
  361. if (data_->GetSeverity() == FATAL) {
  362. #ifdef __ANDROID__
  363. // Set the bionic abort message early to avoid liblog doing it
  364. // with the individual lines, so that we get the whole message.
  365. android_set_abort_message(msg.c_str());
  366. #endif
  367. }
  368. {
  369. // Do the actual logging with the lock held.
  370. std::lock_guard<std::mutex> lock(LoggingLock());
  371. if (msg.find('\n') == std::string::npos) {
  372. LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
  373. data_->GetTag(), msg.c_str());
  374. } else {
  375. msg += '\n';
  376. size_t i = 0;
  377. while (i < msg.size()) {
  378. size_t nl = msg.find('\n', i);
  379. msg[nl] = '\0';
  380. LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
  381. data_->GetTag(), &msg[i]);
  382. // Undo the zero-termination so we can give the complete message to the aborter.
  383. msg[nl] = '\n';
  384. i = nl + 1;
  385. }
  386. }
  387. }
  388. // Abort if necessary.
  389. if (data_->GetSeverity() == FATAL) {
  390. Aborter()(msg.c_str());
  391. }
  392. }
  393. std::ostream& LogMessage::stream() {
  394. return data_->GetBuffer();
  395. }
  396. void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
  397. const char* tag, const char* message) {
  398. if (tag == nullptr) {
  399. std::lock_guard<std::recursive_mutex> lock(TagLock());
  400. if (gDefaultTag == nullptr) {
  401. gDefaultTag = new std::string(getprogname());
  402. }
  403. Logger()(id, severity, gDefaultTag->c_str(), file, line, message);
  404. } else {
  405. Logger()(id, severity, tag, file, line, message);
  406. }
  407. }
  408. LogSeverity GetMinimumLogSeverity() {
  409. return gMinimumLogSeverity;
  410. }
  411. LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) {
  412. LogSeverity old_severity = gMinimumLogSeverity;
  413. gMinimumLogSeverity = new_severity;
  414. return old_severity;
  415. }
  416. ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) {
  417. old_ = SetMinimumLogSeverity(new_severity);
  418. }
  419. ScopedLogSeverity::~ScopedLogSeverity() {
  420. SetMinimumLogSeverity(old_);
  421. }
  422. } // namespace base
  423. } // namespace android