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