xref: /aosp_15_r20/external/pytorch/c10/util/logging_is_google_glog.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #ifndef C10_UTIL_LOGGING_IS_GOOGLE_GLOG_H_
2 #define C10_UTIL_LOGGING_IS_GOOGLE_GLOG_H_
3 
4 #include <map>
5 #include <set>
6 #include <vector>
7 
8 #include <iomanip> // because some of the caffe2 code uses e.g. std::setw
9 // Using google glog. For glog 0.3.2 versions, stl_logging.h needs to be before
10 // logging.h to actually use stl_logging. Because template magic.
11 // In addition, we do not do stl logging in .cu files because nvcc does not like
12 // it. Some mobile platforms do not like stl_logging, so we add an
13 // overload in that case as well.
14 
15 #ifdef __CUDACC__
16 #include <cuda.h>
17 #endif
18 
19 #if !defined(__CUDACC__) && !defined(C10_USE_MINIMAL_GLOG)
20 #include <glog/stl_logging.h>
21 
22 // Old versions of glog don't declare this using declaration, so help
23 // them out.  Fortunately, C++ won't complain if you declare the same
24 // using declaration multiple times.
25 namespace std {
26 using ::operator<<;
27 }
28 
29 #else // !defined(__CUDACC__) && !defined(C10_USE_MINIMAL_GLOG)
30 
31 // In the cudacc compiler scenario, we will simply ignore the container
32 // printout feature. Basically we need to register a fake overload for
33 // vector/string - here, we just ignore the entries in the logs.
34 
35 namespace std {
36 #define INSTANTIATE_FOR_CONTAINER(container)                      \
37   template <class... Types>                                       \
38   ostream& operator<<(ostream& out, const container<Types...>&) { \
39     return out;                                                   \
40   }
41 
42 INSTANTIATE_FOR_CONTAINER(vector)
43 INSTANTIATE_FOR_CONTAINER(map)
44 INSTANTIATE_FOR_CONTAINER(set)
45 #undef INSTANTIATE_FOR_CONTAINER
46 } // namespace std
47 
48 #endif
49 
50 #include <glog/logging.h>
51 
52 // Additional macros on top of glog
53 #define TORCH_CHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
54 #define TORCH_CHECK_NE(val1, val2) CHECK_NE(val1, val2)
55 #define TORCH_CHECK_LE(val1, val2) CHECK_LE(val1, val2)
56 #define TORCH_CHECK_LT(val1, val2) CHECK_LT(val1, val2)
57 #define TORCH_CHECK_GE(val1, val2) CHECK_GE(val1, val2)
58 #define TORCH_CHECK_GT(val1, val2) CHECK_GT(val1, val2)
59 
60 #ifndef NDEBUG
61 #define TORCH_DCHECK_EQ(val1, val2) DCHECK_EQ(val1, val2)
62 #define TORCH_DCHECK_NE(val1, val2) DCHECK_NE(val1, val2)
63 #define TORCH_DCHECK_LE(val1, val2) DCHECK_LE(val1, val2)
64 #define TORCH_DCHECK_LT(val1, val2) DCHECK_LT(val1, val2)
65 #define TORCH_DCHECK_GE(val1, val2) DCHECK_GE(val1, val2)
66 #define TORCH_DCHECK_GT(val1, val2) DCHECK_GT(val1, val2)
67 #else // !NDEBUG
68 // These versions generate no code in optimized mode.
69 #define TORCH_DCHECK_EQ(val1, val2) \
70   while (false)                     \
71   DCHECK_EQ(val1, val2)
72 #define TORCH_DCHECK_NE(val1, val2) \
73   while (false)                     \
74   DCHECK_NE(val1, val2)
75 #define TORCH_DCHECK_LE(val1, val2) \
76   while (false)                     \
77   DCHECK_LE(val1, val2)
78 #define TORCH_DCHECK_LT(val1, val2) \
79   while (false)                     \
80   DCHECK_LT(val1, val2)
81 #define TORCH_DCHECK_GE(val1, val2) \
82   while (false)                     \
83   DCHECK_GE(val1, val2)
84 #define TORCH_DCHECK_GT(val1, val2) \
85   while (false)                     \
86   DCHECK_GT(val1, val2)
87 #endif // NDEBUG
88 
89 // Check that a pointer is not null.
90 #define TORCH_CHECK_NOTNULL(val) CHECK_NOTNULL(val)
91 
92 #ifndef NDEBUG
93 // Debug only version of TORCH_CHECK_NOTNULL
94 #define TORCH_DCHECK_NOTNULL(val) DCHECK_NOTNULL(val)
95 #else // !NDEBUG
96 // Optimized version - generates no code.
97 #define TORCH_DCHECK_NOTNULL(val) \
98   while (false)                   \
99   DCHECK_NOTNULL(val)
100 #endif // NDEBUG
101 
102 // Log with source location information override (to be used in generic
103 // warning/error handlers implemented as functions, not macros)
104 //
105 // Note, we don't respect GOOGLE_STRIP_LOG here for simplicity
106 #define LOG_AT_FILE_LINE(n, file, line) \
107   ::google::LogMessage(file, line, ::google::GLOG_##n).stream()
108 
109 #endif // C10_UTIL_LOGGING_IS_GOOGLE_GLOG_H_
110