TensorFlow Serving C++ API Documentation
net_logging.h
1 /* Copyright 2019 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_SERVING_UTIL_NET_HTTP_INTERNAL_NET_LOGGING_H_
17 #define TENSORFLOW_SERVING_UTIL_NET_HTTP_INTERNAL_NET_LOGGING_H_
18 
19 #include <string>
20 
21 #include "absl/base/attributes.h"
22 #include "absl/base/log_severity.h"
23 #include "absl/base/macros.h"
24 #include "absl/base/port.h"
25 
26 // This initial version is a minimum fork from absl/base/internal/raw_logging.h
27 // Hooks are not supported.
28 //
29 // TODO(wenboz): finalize the log support for net_http
30 // * make logging pluggable (by TF serving or by adapting external libraries
31 
32 #define NET_LOG(severity, ...) \
33  do { \
34  constexpr const char* net_logging_internal_basename = \
35  ::tensorflow::serving::net_http::Basename(__FILE__, \
36  sizeof(__FILE__) - 1); \
37  ::tensorflow::serving::net_http::NetLog(NET_LOGGING_INTERNAL_##severity, \
38  net_logging_internal_basename, \
39  __LINE__, __VA_ARGS__); \
40  } while (0)
41 
42 #define NET_CHECK(condition, message) \
43  do { \
44  if (ABSL_PREDICT_FALSE(!(condition))) { \
45  NET_LOG(FATAL, "Check %s failed: %s", #condition, message); \
46  } \
47  } while (0)
48 
49 #define NET_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
50 #define NET_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
51 #define NET_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
52 #define NET_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
53 #define NET_LOGGING_INTERNAL_LEVEL(severity) \
54  ::absl::NormalizeLogSeverity(severity)
55 
56 namespace tensorflow {
57 namespace serving {
58 namespace net_http {
59 
60 void NetLog(absl::LogSeverity severity, const char* file, int line,
61  const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
62 
63 void SafeWriteToStderr(const char* s, size_t len);
64 
65 constexpr const char* Basename(const char* fname, int offset) {
66  return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
67  ? fname + offset
68  : Basename(fname, offset - 1);
69 }
70 
71 } // namespace net_http
72 } // namespace serving
73 } // namespace tensorflow
74 
75 #endif // TENSORFLOW_SERVING_UTIL_NET_HTTP_INTERNAL_NET_LOGGING_H_