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

Commit 96e7ef5e authored by Tom Cherry's avatar Tom Cherry
Browse files

liblog: check loggability before formatting

Only print logs with priority >= INFO on host, as fake_log_device
does.

This fixes a regression in host builds where loggability wasn't
checked at all.

Bug: 69935292
Test: ART host tests don't print extraneous logs
Change-Id: I885b794da6f24bd905192252925e4a9f88b06674
parent e99ec483
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -516,13 +516,12 @@ void FakeClose() {
  memset(&log_state, 0, sizeof(log_state));
}

int __android_log_is_loggable(int prio, const char*, int def) {
int __android_log_is_loggable(int prio, const char*, int) {
  int minimum_priority = __android_log_get_minimum_priority();
  if (minimum_priority == ANDROID_LOG_DEFAULT) {
    return prio >= def;
  } else {
    return prio >= minimum_priority;
    minimum_priority = ANDROID_LOG_INFO;
  }
  return prio >= minimum_priority;
}

int __android_log_is_loggable_len(int prio, const char*, size_t, int def) {
+4 −0
Original line number Diff line number Diff line
@@ -194,6 +194,10 @@ struct __android_logger_data {
 * Writes the log message specified with logger_data and msg to the log.  logger_data includes
 * additional file name and line number information that a logger may use.  logger_data is versioned
 * for backwards compatibility.
 * This assumes that loggability has already been checked through __android_log_is_loggable().
 * Higher level logging libraries, such as libbase, first check loggability, then format their
 * buffers, then pass the message to liblog via this function, and therefore we do not want to
 * duplicate the loggability check here.
 */
void __android_log_write_logger_data(struct __android_logger_data* logger_data, const char* msg);

+27 −12
Original line number Diff line number Diff line
@@ -185,15 +185,6 @@ static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
      errno = save_errno;
      return -EINVAL;
    }
  } else {
    int prio = *static_cast<int*>(vec[0].iov_base);
    const char* tag = static_cast<const char*>(vec[1].iov_base);
    size_t len = vec[1].iov_len;

    if (!__android_log_is_loggable_len(prio, tag, len - 1, ANDROID_LOG_VERBOSE)) {
      errno = save_errno;
      return -EPERM;
    }
  }

  ret = LogdWrite(log_id, &ts, vec, nr);
@@ -292,20 +283,35 @@ void __android_log_write_logger_data(__android_logger_data* logger_data, const c
}

int __android_log_buf_write(int bufID, int prio, const char* tag, const char* msg) {
  if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
    return 0;
  }

  __android_logger_data logger_data = {sizeof(__android_logger_data), bufID, prio, tag, nullptr, 0};
  __android_log_write_logger_data(&logger_data, msg);
  return 1;
}

int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap) {
  if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
    return 0;
  }

  char buf[LOG_BUF_SIZE];

  vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);

  return __android_log_write(prio, tag, buf);
  __android_logger_data logger_data = {
      sizeof(__android_logger_data), LOG_ID_MAIN, prio, tag, nullptr, 0};
  __android_log_write_logger_data(&logger_data, buf);
  return 1;
}

int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
  if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
    return 0;
  }

  va_list ap;
  char buf[LOG_BUF_SIZE];

@@ -313,10 +319,17 @@ int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
  vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
  va_end(ap);

  return __android_log_write(prio, tag, buf);
  __android_logger_data logger_data = {
      sizeof(__android_logger_data), LOG_ID_MAIN, prio, tag, nullptr, 0};
  __android_log_write_logger_data(&logger_data, buf);
  return 1;
}

int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) {
  if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
    return 0;
  }

  va_list ap;
  char buf[LOG_BUF_SIZE];

@@ -324,7 +337,9 @@ int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fm
  vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
  va_end(ap);

  return __android_log_buf_write(bufID, prio, tag, buf);
  __android_logger_data logger_data = {sizeof(__android_logger_data), bufID, prio, tag, nullptr, 0};
  __android_log_write_logger_data(&logger_data, buf);
  return 1;
}

void __android_log_assert(const char* cond, const char* tag, const char* fmt, ...) {