| // Copyright 2016 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include <lib/syslog/cpp/log_settings.h> |
| #include <lib/syslog/cpp/macros.h> |
| |
| #include <algorithm> |
| #include <iostream> |
| |
| #if defined(__Fuchsia__) |
| #include <lib/syslog/global.h> |
| #include <zircon/status.h> |
| #elif defined(OS_ANDROID) |
| #include <android/log.h> |
| #elif defined(OS_IOS) |
| #include <lib/syslog.h> |
| #endif |
| |
| namespace syslog { |
| namespace { |
| |
| #ifndef __Fuchsia__ |
| |
| const std::string GetNameForLogSeverity(LogSeverity severity) { |
| switch (severity) { |
| case LOG_TRACE: |
| return "TRACE"; |
| case LOG_DEBUG: |
| return "DEBUG"; |
| case LOG_INFO: |
| return "INFO"; |
| case LOG_WARNING: |
| return "WARNING"; |
| case LOG_ERROR: |
| return "ERROR"; |
| case LOG_FATAL: |
| return "FATAL"; |
| } |
| |
| if (severity > LOG_DEBUG && severity < LOG_INFO) { |
| std::ostringstream stream; |
| stream << "VLOG(" << (LOG_INFO - severity) << ")"; |
| return stream.str(); |
| } |
| |
| return "UNKNOWN"; |
| } |
| #endif |
| |
| const char* StripDots(const char* path) { |
| while (strncmp(path, "../", 3) == 0) |
| path += 3; |
| return path; |
| } |
| |
| const char* StripPath(const char* path) { |
| auto p = strrchr(path, '/'); |
| if (p) |
| return p + 1; |
| else |
| return path; |
| } |
| |
| } // namespace |
| |
| LogMessage::LogMessage(LogSeverity severity, const char* file, int line, const char* condition, |
| const char* tag |
| #if defined(__Fuchsia__) |
| , |
| zx_status_t status |
| #endif |
| ) |
| : severity_(severity), |
| file_(file), |
| line_(line), |
| tag_(tag) |
| #if defined(__Fuchsia__) |
| , |
| status_(status) |
| #endif |
| { |
| #if !defined(__Fuchsia__) |
| if (tag) |
| stream_ << "[" << tag_ << "] "; |
| #endif |
| stream_ << "["; |
| // With syslog the severity is included in the metadata so no need to add it |
| // to the log message itself. |
| #ifndef __Fuchsia__ |
| stream_ << GetNameForLogSeverity(severity) << ":"; |
| #endif |
| stream_ << (severity > LOG_INFO ? StripDots(file_) : StripPath(file_)) << "(" << line_ << ")] "; |
| |
| if (condition) |
| stream_ << "Check failed: " << condition << ". "; |
| } |
| |
| LogMessage::~LogMessage() { |
| #if defined(__Fuchsia__) |
| if (status_ != std::numeric_limits<zx_status_t>::max()) { |
| stream_ << ": " << status_ << " (" << zx_status_get_string(status_) << ")"; |
| } |
| #else |
| stream_ << std::endl; |
| #endif |
| |
| #if defined(__Fuchsia__) |
| // Write fatal logs to stderr as well because death tests sometimes verify a certain |
| // log message was printed prior to the crash. |
| // TODO(samans): Convert tests to not depend on stderr. https://fxbug.dev/49593 |
| if (severity_ == LOG_FATAL) |
| std::cerr << stream_.str() << std::endl; |
| fx_logger_t* logger = fx_log_get_logger(); |
| fx_logger_log(logger, severity_, tag_, stream_.str().c_str()); |
| #elif defined(OS_ANDROID) |
| android_LogPriority priority = (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN; |
| switch (severity_) { |
| case LOG_INFO: |
| priority = ANDROID_LOG_INFO; |
| break; |
| case LOG_WARNING: |
| priority = ANDROID_LOG_WARN; |
| break; |
| case LOG_ERROR: |
| priority = ANDROID_LOG_ERROR; |
| break; |
| case LOG_FATAL: |
| priority = ANDROID_LOG_FATAL; |
| break; |
| } |
| __android_log_write(priority, ANDROID_LOG_TAG, stream_.str().c_str()); |
| #elif defined(OS_IOS) |
| syslog(LOG_ALERT, "%s", stream_.str().c_str()); |
| #else |
| std::cerr << stream_.str(); |
| std::cerr.flush(); |
| #endif |
| |
| if (severity_ >= LOG_FATAL) |
| __builtin_debugtrap(); |
| } |
| |
| bool LogFirstNState::ShouldLog(uint32_t n) { |
| const uint32_t counter_value = counter_.fetch_add(1, std::memory_order_relaxed); |
| return counter_value < n; |
| } |
| |
| int GetVlogVerbosity() { |
| int min_level = GetMinLogLevel(); |
| if (min_level < LOG_INFO && min_level > LOG_DEBUG) { |
| return LOG_INFO - min_level; |
| } |
| return 0; |
| } |
| |
| bool ShouldCreateLogMessage(LogSeverity severity) { return severity >= GetMinLogLevel(); } |
| |
| } // namespace syslog |