Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d93c6d5e authored by Henri Chataing's avatar Henri Chataing
Browse files

Strip remaining system/gd/os/log.h definitions

The header cannot be removed straightaway as it includes log_adapter.h,
which most modules rely on for ADDRESS_To_LOGGABLE_STR

Bug: 305066880
Test: m com.android.btservices
Flag: EXEMPT, dead code removal
Change-Id: Iea14d270f122877684be3b918efced34fe062772
parent 81b7bcec
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ source_set("BluetoothOsSources_chromeos") {
    "chromeos/system_properties.cc",
    "chromeos/wakelock_native.cc",
    "system_properties_common.cc",
    "syslog.cc",
  ]

  configs += [
@@ -39,7 +38,6 @@ source_set("BluetoothOsSources_linux") {
    "linux/system_properties.cc",
    "linux/wakelock_native.cc",
    "system_properties_common.cc",
    "syslog.cc",
  ]

  configs += [
+1 −111
Original line number Diff line number Diff line
@@ -22,119 +22,9 @@

#include <cstdlib>

#ifndef LOG_TAG
#define LOG_TAG "bluetooth"
#endif

static_assert(LOG_TAG != nullptr, "LOG_TAG should never be NULL");

#include "os/log_tags.h"
#include "os/logging/log_adapter.h"

#if defined(FUZZ_TARGET)

// for fuzz targets, we just
// need to abort in this statement
// to catch the bug
#define LOG_ALWAYS_FATAL_INT(...) do {  \
    fprintf(stderr, __VA_ARGS__);       \
    abort();                            \
  } while (false)

#else /* end of defined(FUZZ_TARGET) */

#if defined(__ANDROID__)

#include <log/log.h>
#include <log/log_event_list.h>

#define LOG_ALWAYS_FATAL_INT(fmt, args...) do { \
  ALOGE(fmt, ##args);                           \
  abort();                                      \
} while (false)

#elif defined (ANDROID_EMULATOR)  /* end of defined(__ANDROID__) */
// Log using android emulator logging mechanism
#include "android/utils/debug.h"

#define LOGWRAPPER(fmt, args...) VERBOSE_INFO(bluetooth, "bluetooth: " fmt, \
                                               ##args)

#define LOG_ALWAYS_FATAL_INT(fmt, args...)                                          \
  do {                                                                              \
    fprintf(stderr, fmt "\n", ##args);                                              \
    abort();                                                                        \
  } while (false)

#elif defined(TARGET_FLOSS) /* end of defined (ANDROID_EMULATOR) */

#include "os/syslog.h"

// Prefix the log with tag, file, line and function
#define LOGWRAPPER(tag, fmt, args...) write_syslog(tag, LOG_TAG, "%s: " fmt, LOG_TAG, ##args)

#define LOG_ALWAYS_FATAL_INT(...)           \
  do {                                      \
    LOGWRAPPER(LOG_TAG_FATAL, __VA_ARGS__); \
    abort();                                \
  } while (false)

#else  /* end of defined (TARGET_FLOSS) */

/* syslog didn't work well here since we would be redefining LOG_DEBUG. */
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#include <chrono>
#include <cstdio>
#include <ctime>

#define LOGWRAPPER(fmt, args...)                                                                                    \
  do {                                                                                                              \
    auto _now = std::chrono::system_clock::now();                                                                   \
    auto _now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(_now);                                   \
    auto _now_t = std::chrono::system_clock::to_time_t(_now);                                                       \
    /* YYYY-MM-DD_HH:MM:SS.sss is 23 byte long, plus 1 for null terminator */                                       \
    char _buf[24];                                                                                                  \
    auto l = std::strftime(_buf, sizeof(_buf), "%Y-%m-%d %H:%M:%S", std::localtime(&_now_t));                       \
    snprintf(                                                                                                       \
        _buf + l, sizeof(_buf) - l, ".%03u", static_cast<unsigned int>(_now_ms.time_since_epoch().count() % 1000)); \
    /* pid max is 2^22 = 4194304 in 64-bit system, and 32768 by default, hence 7 digits are needed most */          \
    fprintf(                                                                                                        \
        stderr,                                                                                                     \
        "%s %7d %7ld %s:" fmt "\n",                                                                                 \
        _buf,                                                                                                       \
        static_cast<int>(getpid()),                                                                                 \
        syscall(SYS_gettid),                                                                                        \
        LOG_TAG,                                                                                                    \
        ##args);                                                                                                    \
  } while (false)

#ifndef LOG_ALWAYS_FATAL
#define LOG_ALWAYS_FATAL_INT(...) \
  do {                            \
    LOGWRAPPER(__VA_ARGS__);      \
    abort();                      \
  } while (false)
#endif

#endif /* defined(__ANDROID__) */

#endif /* defined(FUZZ_TARGET) */

#define _LOG_SRC_FMT_STR "%s:%d - %s: "
#define _PREPEND_SRC_LOC_IN_LOG(fmt, args...) \
  _LOG_SRC_FMT_STR fmt, __FILE__, __LINE__, __func__, ##args

// ---------------------------------------------------------
// All MACROs defined above are internal and should *not* be
// used directly (use LOG_XXX defined below instead).
// the output of LOG_XXX_INT does not contain the source
// location of the log emitting statement, so far they are only used by
// LogMsg, where the source locations is passed in.

#ifndef LOG_ALWAYS_FATAL
#define LOG_ALWAYS_FATAL(fmt, args...)                                        \
  LOG_ALWAYS_FATAL_INT(_PREPEND_SRC_LOC_IN_LOG(fmt, ##args))
#endif
#endif  // defined(__ANDROID__)

system/gd/os/log_tags.h

deleted100644 → 0
+0 −29
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

// TODO(b/305066880) - Deprecate once replaced with fmtlib implementation.
// These log levels may need to be mapped to system values. These values are
// used to control the log level and should match
// `bluetooth::log_internal::Level`.
enum LogLevels {
  LOG_TAG_VERBOSE = 2,
  LOG_TAG_DEBUG = 3,
  LOG_TAG_INFO = 4,
  LOG_TAG_WARN = 5,
  LOG_TAG_ERROR = 6,
  LOG_TAG_FATAL = 7,
};

system/gd/os/syslog.cc

deleted100644 → 0
+0 −90
Original line number Diff line number Diff line
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "os/syslog.h"

#include <syslog.h>

#include <cstdarg>
#include <memory>

#include "os/log_tags.h"

// TODO(b/305066880) - This implementation will replace this syslog
// implementation. Remove this file once everything is moved over to the new
// logging macros.
#include "bluetooth/log.h"

namespace bluetooth::log_internal {
extern Level GetLogLevelForTag(char const* tag);
}

namespace {
#define SYSLOG_IDENT "btadapterd"

const char kSyslogIdent[] = SYSLOG_IDENT;

// Map LOG_TAG_* to syslog mappings
const int kLevelMap[] = {
    /*LOG_TAG_VERBOSE=*/LOG_DEBUG,
    /*LOG_TAG_DEBUG=*/LOG_DEBUG,
    /*LOG_TAG_INFO=*/LOG_INFO,
    /*LOG_TAG_WARN=*/LOG_WARNING,
    /*LOG_TAG_ERROR=*/LOG_ERR,
    /*LOG_TAG_FATAL=*/LOG_CRIT,
};

static_assert(sizeof(kLevelMap) / sizeof(kLevelMap[0]) == (LOG_TAG_FATAL - LOG_TAG_VERBOSE) + 1);

class SyslogWrapper {
 public:
  SyslogWrapper() {
    openlog(kSyslogIdent, LOG_CONS | LOG_NDELAY | LOG_PID | LOG_PERROR, LOG_DAEMON);
  }

  ~SyslogWrapper() {
    closelog();
  }
};

std::unique_ptr<SyslogWrapper> gSyslog;
}  // namespace

void write_syslog(int level, const char* tag, const char* format, ...) {
  if (!gSyslog) {
    gSyslog = std::make_unique<SyslogWrapper>();
  }

  // Filter out logs that don't meet level requirement.
  bluetooth::log_internal::Level current_level = bluetooth::log_internal::GetLogLevelForTag(tag);
  if (static_cast<bluetooth::log_internal::Level>(level) < current_level) {
    return;
  }

  // I don't expect to see incorrect levels but making the check anyway so we
  // don't go out of bounds in the array above.
  if (level > LOG_TAG_FATAL) {
    level = LOG_TAG_ERROR;
  } else if (level < LOG_TAG_VERBOSE) {
    level = LOG_TAG_VERBOSE;
  }
  int syslog_level = kLevelMap[level - LOG_TAG_VERBOSE];

  va_list args;
  va_start(args, format);
  vsyslog(syslog_level, format, args);
  va_end(args);
}

system/gd/os/syslog.h

deleted100644 → 0
+0 −29
Original line number Diff line number Diff line
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <cstdarg>
#include <cstdint>

/**
 * This header is used for systems targeting syslog as their log target (i.e.
 * Floss builds).
 */

/**
 * Write log to syslog.
 */
void write_syslog(int level, const char* tag, const char* format, ...);