1 // log.h
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 // Author: riley@google.com (Michael Riley)
16 //
17 // \file
18 // Google-style logging declarations and inline definitions.
19 
20 #ifndef FST_LIB_LOG_H__
21 #define FST_LIB_LOG_H__
22 
23 #include <cassert>
24 #include <iostream>
25 #include <string>
26 
27 #include <fst/types.h>
28 #include <fst/flags.h>
29 
30 using std::string;
31 
32 DECLARE_int32(v);
33 
34 class LogMessage {
35  public:
LogMessage(const string & type)36   LogMessage(const string &type) : fatal_(type == "FATAL") {
37     std::cerr << type << ": ";
38   }
~LogMessage()39   ~LogMessage() {
40     std::cerr << std::endl;
41     if(fatal_)
42       exit(1);
43   }
stream()44   std::ostream &stream() { return std::cerr; }
45 
46  private:
47   bool fatal_;
48 };
49 
50 #define LOG(type) LogMessage(#type).stream()
51 #define VLOG(level) if ((level) <= FLAGS_v) LOG(INFO)
52 
53 // Checks
FstCheck(bool x,const char * expr,const char * file,int line)54 inline void FstCheck(bool x, const char* expr,
55                 const char *file, int line) {
56   if (!x) {
57     LOG(FATAL) << "Check failed: \"" << expr
58                << "\" file: " << file
59                << " line: " << line;
60   }
61 }
62 
63 #define CHECK(x) FstCheck((x), #x, __FILE__, __LINE__)
64 #define CHECK_EQ(x, y) CHECK((x) == (y))
65 #define CHECK_LT(x, y) CHECK((x) < (y))
66 #define CHECK_GT(x, y) CHECK((x) > (y))
67 #define CHECK_LE(x, y) CHECK((x) <= (y))
68 #define CHECK_GE(x, y) CHECK((x) >= (y))
69 #define CHECK_NE(x, y) CHECK((x) != (y))
70 
71 // Debug checks
72 #define DCHECK(x) assert(x)
73 #define DCHECK_EQ(x, y) DCHECK((x) == (y))
74 #define DCHECK_LT(x, y) DCHECK((x) < (y))
75 #define DCHECK_GT(x, y) DCHECK((x) > (y))
76 #define DCHECK_LE(x, y) DCHECK((x) <= (y))
77 #define DCHECK_GE(x, y) DCHECK((x) >= (y))
78 #define DCHECK_NE(x, y) DCHECK((x) != (y))
79 
80 
81 // Ports
82 #define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
83 
84 #endif  // FST_LIB_LOG_H__
85