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 (at) 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: 36 LogMessage(const string &type) : fatal_(type == "FATAL") { 37 std::cerr << type << ": "; 38 } 39 ~LogMessage() { 40 std::cerr << std::endl; 41 if(fatal_) 42 exit(1); 43 } 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 54 inline void CHECK(bool x) { assert(x); } 55 56 #define CHECK_EQ(x, y) CHECK((x) == (y)) 57 #define CHECK_LT(x, y) CHECK((x) < (y)) 58 #define CHECK_GT(x, y) CHECK((x) > (y)) 59 #define CHECK_LE(x, y) CHECK((x) <= (y)) 60 #define CHECK_GE(x, y) CHECK((x) >= (y)) 61 #define CHECK_NE(x, y) CHECK((x) != (y)) 62 63 // Ports 64 #define ATTRIBUTE_DEPRECATED __attribute__((deprecated)) 65 66 #endif // FST_LIB_LOG_H__ 67