FST  openfst-1.8.2
OpenFst Library
log.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Google-style logging declarations and inline definitions.
19 
20 #ifndef FST_LOG_H_
21 #define FST_LOG_H_
22 
23 #include <cassert>
24 #include <iostream>
25 #include <ostream>
26 #include <string_view>
27 
28 #include <fst/flags.h>
29 
30 DECLARE_int32(v);
31 
32 class LogMessage {
33  public:
34  explicit LogMessage(std::string_view type) : fatal_(type == "FATAL") {
35  std::cerr << type << ": ";
36  }
37 
39  std::cerr << std::endl;
40  if (fatal_) exit(1);
41  }
42 
43  std::ostream &stream() { return std::cerr; }
44 
45  private:
46  bool fatal_;
47 };
48 
49 #define LOG(type) LogMessage(#type).stream()
50 #define VLOG(level) if ((level) <= FST_FLAGS_v) LOG(INFO)
51 
52 // Checks.
53 inline void FstCheck(bool x, std::string_view expr, std::string_view file,
54  int line) {
55  if (!x) {
56  LOG(FATAL) << "Check failed: \"" << expr << "\" file: " << file
57  << " line: " << line;
58  }
59 }
60 
61 #define CHECK(x) FstCheck(static_cast<bool>(x), #x, __FILE__, __LINE__)
62 #define CHECK_EQ(x, y) CHECK((x) == (y))
63 #define CHECK_LT(x, y) CHECK((x) < (y))
64 #define CHECK_GT(x, y) CHECK((x) > (y))
65 #define CHECK_LE(x, y) CHECK((x) <= (y))
66 #define CHECK_GE(x, y) CHECK((x) >= (y))
67 #define CHECK_NE(x, y) CHECK((x) != (y))
68 
69 // Debug checks.
70 #define DCHECK(x) assert(x)
71 #define DCHECK_EQ(x, y) DCHECK((x) == (y))
72 #define DCHECK_LT(x, y) DCHECK((x) < (y))
73 #define DCHECK_GT(x, y) DCHECK((x) > (y))
74 #define DCHECK_LE(x, y) DCHECK((x) <= (y))
75 #define DCHECK_GE(x, y) DCHECK((x) >= (y))
76 #define DCHECK_NE(x, y) DCHECK((x) != (y))
77 
78 #endif // FST_LOG_H_
~LogMessage()
Definition: log.h:38
#define LOG(type)
Definition: log.h:49
Definition: log.h:32
std::ostream & stream()
Definition: log.h:43
LogMessage(std::string_view type)
Definition: log.h:34
void FstCheck(bool x, std::string_view expr, std::string_view file, int line)
Definition: log.h:53
DECLARE_int32(v)