16 #include "tensorflow_serving/util/net_http/internal/net_logging.h"
25 #include "absl/base/attributes.h"
26 #include "absl/base/config.h"
27 #include "absl/base/log_severity.h"
29 #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
30 defined(__Fuchsia__) || defined(__native_client__) || \
31 defined(__EMSCRIPTEN__)
34 #define NET_HAVE_POSIX_WRITE 1
35 #define NET_LOW_LEVEL_WRITE_SUPPORTED 1
37 #undef NET_HAVE_POSIX_WRITE
40 #if (defined(__linux__) || defined(__FreeBSD__)) && !defined(__ANDROID__)
41 #include <sys/syscall.h>
42 #define NET_HAVE_SYSCALL_WRITE 1
43 #define NET_LOW_LEVEL_WRITE_SUPPORTED 1
45 #undef NET_HAVE_SYSCALL_WRITE
51 #define NET_HAVE_RAW_IO 1
52 #define NET_LOW_LEVEL_WRITE_SUPPORTED 1
54 #undef NET_HAVE_RAW_IO
57 #ifdef NET_LOW_LEVEL_WRITE_SUPPORTED
58 static const char kTruncated[] =
" ... (message truncated)\n";
60 inline static bool VADoNetLog(
char** buf,
int* size,
const char* format,
61 va_list ap) ABSL_PRINTF_ATTRIBUTE(3, 0);
62 inline static bool VADoNetLog(
char** buf,
int* size,
const char* format,
64 int n = vsnprintf(*buf, *size, format, ap);
66 if (n < 0 || n > *size) {
68 if (
static_cast<size_t>(*size) >
sizeof(kTruncated)) {
69 n = *size -
sizeof(kTruncated);
80 static constexpr
int kLogBufSize = 10000;
84 bool DoNetLog(
char** buf,
int* size,
const char* format, ...)
85 ABSL_PRINTF_ATTRIBUTE(3, 4);
86 bool DoNetLog(
char** buf,
int* size, const
char* format, ...) {
89 int n = vsnprintf(*buf, *size, format, ap);
91 if (n < 0 || n > *size)
return false;
97 void NetLogVA(absl::LogSeverity severity,
const char* file,
int line,
98 const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0);
99 void NetLogVA(absl::LogSeverity severity,
const char* file,
int line,
100 const char* format, va_list ap) {
101 char buffer[kLogBufSize];
103 int size =
sizeof(buffer);
104 #ifdef NET_LOW_LEVEL_WRITE_SUPPORTED
107 bool enabled =
false;
110 #ifdef ABSL_MIN_LOG_LEVEL
111 if (severity <
static_cast<absl::LogSeverity
>(ABSL_MIN_LOG_LEVEL) &&
112 severity < absl::LogSeverity::kFatal) {
118 DoNetLog(&buf, &size,
"[%s : %d] NET_LOG: ", file, line);
121 #ifdef NET_LOW_LEVEL_WRITE_SUPPORTED
123 bool no_chop = VADoNetLog(&buf, &size, format, ap);
125 DoNetLog(&buf, &size,
"\n");
127 DoNetLog(&buf, &size,
"%s", kTruncated);
129 tensorflow::serving::net_http::SafeWriteToStderr(buffer, strlen(buffer));
132 static_cast<void>(format);
133 static_cast<void>(ap);
136 if (severity == absl::LogSeverity::kFatal) {
143 namespace tensorflow {
147 void SafeWriteToStderr(
const char* s,
size_t len) {
148 #if defined(NET_HAVE_SYSCALL_WRITE)
149 syscall(SYS_write, STDERR_FILENO, s, len);
150 #elif defined(NET_HAVE_POSIX_WRITE)
151 write(STDERR_FILENO, s, len);
152 #elif defined(NET_HAVE_RAW_IO)
160 void NetLog(absl::LogSeverity severity,
const char* file,
int line,
161 const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
162 void NetLog(absl::LogSeverity severity, const
char* file,
int line,
163 const
char* format, ...) {
165 va_start(ap, format);
166 NetLogVA(severity, file, line, format, ap);