Merge "logcat: use stdio for free buffering." am: d07c55b6f7 am: 62a4946b53 am: d005ee945a

Original change: https://android-review.googlesource.com/c/platform/system/logging/+/1894740

Change-Id: I0f887ceb9bde86c7387c14eed790f4612e43d689
GitOrigin-RevId: b3260357fee68ef3684f35eb8e4abf7cc86e9b24
diff --git a/liblog/include/log/logprint.h b/liblog/include/log/logprint.h
index 7dfd914..0cff640 100644
--- a/liblog/include/log/logprint.h
+++ b/liblog/include/log/logprint.h
@@ -17,6 +17,7 @@
 #pragma once
 
 #include <stdint.h>
+#include <stdio.h>
 #include <sys/types.h>
 
 #include <android/log.h>
@@ -147,13 +148,9 @@
                                 size_t* p_outLength);
 
 /**
- * Either print or do not print log line, based on filter
- *
- * Assumes single threaded execution
- *
+ * Formats a log message into a FILE*.
  */
-int android_log_printLogLine(AndroidLogFormat* p_format, int fd,
-                             const AndroidLogEntry* entry);
+size_t android_log_printLogLine(AndroidLogFormat* p_format, FILE* fp, const AndroidLogEntry* entry);
 
 #ifdef __cplusplus
 }
diff --git a/liblog/logprint.cpp b/liblog/logprint.cpp
index 639b8d9..3f2a820 100644
--- a/liblog/logprint.cpp
+++ b/liblog/logprint.cpp
@@ -1,19 +1,18 @@
 /*
-**
-** Copyright 2006-2014, 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.
-*/
+ * Copyright 2006, 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 <log/logprint.h>
 
@@ -1691,42 +1690,22 @@
   return ret;
 }
 
-/**
- * Either print or do not print log line, based on filter
- *
- * Returns count bytes written
- */
-
-int android_log_printLogLine(AndroidLogFormat* p_format, int fd, const AndroidLogEntry* entry) {
-  int ret;
-  char defaultBuffer[512];
-  char* outBuffer = NULL;
-  size_t totalLen;
-
-  outBuffer =
-      android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
-
-  if (!outBuffer) return -1;
-
-  do {
-    ret = write(fd, outBuffer, totalLen);
-  } while (ret < 0 && errno == EINTR);
-
-  if (ret < 0) {
-    fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
-    ret = 0;
-    goto done;
+size_t android_log_printLogLine(AndroidLogFormat* p_format, FILE* fp,
+                                const AndroidLogEntry* entry) {
+  char buf[4096] __attribute__((__uninitialized__));
+  size_t line_length;
+  char* line = android_log_formatLogLine(p_format, buf, sizeof(buf), entry, &line_length);
+  if (!line) {
+    fprintf(stderr, "android_log_formatLogLine failed\n");
+    exit(1);
   }
 
-  if (((size_t)ret) < totalLen) {
-    fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
-    goto done;
+  size_t bytesWritten = fwrite(line, 1, line_length, fp);
+  if (bytesWritten != line_length) {
+    perror("fwrite failed");
+    exit(1);
   }
 
-done:
-  if (outBuffer != defaultBuffer) {
-    free(outBuffer);
-  }
-
-  return ret;
+  if (line != buf) free(line);
+  return bytesWritten;
 }