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

Commit 42e35892 authored by Henri Chataing's avatar Henri Chataing Committed by Gerrit Code Review
Browse files

Merge changes I906ec7bd,Ie370ffbf

* changes:
  RootCanal: Convert remaining logs to fmtlib
  RootCanal: Implement fmt::formatter for Address, AddressWithType
parents b2a230f4 45e8f67d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ bool crash_callback(const void* crash_context, size_t crash_context_size,
  }
  ERROR("Backtrace:");
  for (const auto& frame : data.frames) {
    ERROR("{}", unwinder.FormatFrame(frame).c_str());
    ERROR("{}", unwinder.FormatFrame(frame));
  }
  return true;
}
+0 −14
Original line number Diff line number Diff line
@@ -69,20 +69,6 @@ static void Log(Verbosity verb, char const* file, int line, char const* format,
  rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, \
                      __VA_ARGS__)

// TODO: still required by the generated HCI parser and serializer backend.
#define LOG_INFO(...)                                                       \
  rootcanal::log::Log(rootcanal::log::Verbosity::kInfo, __FILE__, __LINE__, \
                      "{}", fmt::sprintf(__VA_ARGS__))
#define LOG_WARN(...)                                                          \
  rootcanal::log::Log(rootcanal::log::Verbosity::kWarning, __FILE__, __LINE__, \
                      "{}", fmt::sprintf(__VA_ARGS__))
#define LOG_ERROR(...)                                                       \
  rootcanal::log::Log(rootcanal::log::Verbosity::kError, __FILE__, __LINE__, \
                      "{}", fmt::sprintf(__VA_ARGS__))
#define LOG_ALWAYS_FATAL(...)                                                \
  rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, \
                      "{}", fmt::sprintf(__VA_ARGS__))

#define ASSERT(x)                                                       \
  __builtin_expect((x) != 0, true) ||                                   \
      (rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, \
+13 −0
Original line number Diff line number Diff line
@@ -18,3 +18,16 @@
// FIXME: Change hci_packets.h to not depend on os/log.h
//        and remove this.
#include "include/log.h"

#define LOG_INFO(...)                                                       \
  rootcanal::log::Log(rootcanal::log::Verbosity::kInfo, __FILE__, __LINE__, \
                      "{}", fmt::sprintf(__VA_ARGS__))
#define LOG_WARN(...)                                                          \
  rootcanal::log::Log(rootcanal::log::Verbosity::kWarning, __FILE__, __LINE__, \
                      "{}", fmt::sprintf(__VA_ARGS__))
#define LOG_ERROR(...)                                                       \
  rootcanal::log::Log(rootcanal::log::Verbosity::kError, __FILE__, __LINE__, \
                      "{}", fmt::sprintf(__VA_ARGS__))
#define LOG_ALWAYS_FATAL(...)                                                \
  rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, \
                      "{}", fmt::sprintf(__VA_ARGS__))
+79 −0
Original line number Diff line number Diff line
@@ -18,12 +18,16 @@

#include "hci/address.h"

#include <fmt/core.h>

#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <sstream>

#include "hci/address_with_type.h"

namespace bluetooth {
namespace hci {

@@ -115,3 +119,78 @@ bool Address::IsValidAddress(const std::string& address) {

}  // namespace hci
}  // namespace bluetooth

template <>
struct fmt::formatter<bluetooth::hci::Address> {
  // Presentation format: 'x' - lowercase, 'X' - uppercase.
  char presentation = 'x';

  // Parses format specifications of the form ['x' | 'X'].
  constexpr auto parse(format_parse_context& ctx)
      -> format_parse_context::iterator {
    // Parse the presentation format and store it in the formatter:
    auto it = ctx.begin();
    auto end = ctx.end();
    if (it != end && (*it == 'x' || *it == 'X')) {
      presentation = *it++;
    }

    // Check if reached the end of the range:
    if (it != end && *it != '}') {
      ctx.on_error("invalid format");
    }

    // Return an iterator past the end of the parsed range:
    return it;
  }

  // Formats the address a using the parsed format specification (presentation)
  // stored in this formatter.
  auto format(const bluetooth::hci::Address& a, format_context& ctx) const
      -> format_context::iterator {
    return presentation == 'x'
               ? fmt::format_to(ctx.out(),
                                "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
                                a.address[5], a.address[4], a.address[3],
                                a.address[2], a.address[1], a.address[0])
               : fmt::format_to(ctx.out(),
                                "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
                                a.address[5], a.address[4], a.address[3],
                                a.address[2], a.address[1], a.address[0]);
  }
};

template <>
struct fmt::formatter<bluetooth::hci::AddressWithType> {
  // Presentation format: 'x' - lowercase, 'X' - uppercase.
  char presentation = 'x';

  // Parses format specifications of the form ['x' | 'X'].
  constexpr auto parse(format_parse_context& ctx)
      -> format_parse_context::iterator {
    // Parse the presentation format and store it in the formatter:
    auto it = ctx.begin();
    auto end = ctx.end();
    if (it != end && (*it == 'x' || *it == 'X')) {
      presentation = *it++;
    }

    // Check if reached the end of the range:
    if (it != end && *it != '}') {
      ctx.on_error("invalid format");
    }

    // Return an iterator past the end of the parsed range:
    return it;
  }

  // Formats the address a using the parsed format specification (presentation)
  // stored in this formatter.
  auto format(const bluetooth::hci::AddressWithType& a,
              format_context& ctx) const -> format_context::iterator {
    auto out = presentation == 'x'
                   ? fmt::format_to(ctx.out(), "{:x}", a.GetAddress())
                   : fmt::format_to(ctx.out(), "{:X}", a.GetAddress());
    return fmt::format_to(out, "[{}]", AddressTypeText(a.GetAddressType()));
  }
};
+4 −5
Original line number Diff line number Diff line
@@ -96,18 +96,17 @@ bool AclConnectionHandler::CreatePendingLeConnection(
    auto connection = std::get<AclConnection>(pair);
    if (connection.GetAddress() == peer ||
        connection.GetResolvedAddress() == resolved_peer) {
      LOG_INFO("%s: %s is already connected", __func__,
               peer.ToString().c_str());
      INFO("{}: {} is already connected", __func__, peer);
      if (connection.GetResolvedAddress() == resolved_peer) {
        LOG_INFO("%s: allowing a second connection with %s", __func__,
                 resolved_peer.ToString().c_str());
        INFO("{}: allowing a second connection with {}", __func__,
             resolved_peer);
      } else {
        return false;
      }
    }
  }
  if (le_connection_pending_) {
    LOG_INFO("%s: connection already pending", __func__);
    INFO("{}: connection already pending", __func__);
    return false;
  }
  le_connection_pending_ = true;
Loading