1f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// log.h
2f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
3f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Licensed under the Apache License, Version 2.0 (the "License");
4f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// you may not use this file except in compliance with the License.
5f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// You may obtain a copy of the License at
6f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
7f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//      http://www.apache.org/licenses/LICENSE-2.0
8f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
9f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Unless required by applicable law or agreed to in writing, software
10f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// distributed under the License is distributed on an "AS IS" BASIS,
11f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// See the License for the specific language governing permissions and
13f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// limitations under the License.
14f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
15f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Author: riley@google.com (Michael Riley)
16f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
17f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// \file
18f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Google-style logging declarations and inline definitions.
19f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
20f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#ifndef FST_LIB_LOG_H__
21f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define FST_LIB_LOG_H__
22f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
23f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <cassert>
24f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <iostream>
25f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <string>
26f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
27f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/types.h>
28f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/flags.h>
29f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
30f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonusing std::string;
31f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
32f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDECLARE_int32(v);
33f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
34f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass LogMessage {
35f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
36f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  LogMessage(const string &type) : fatal_(type == "FATAL") {
37f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    std::cerr << type << ": ";
38f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
39f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  ~LogMessage() {
40f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    std::cerr << std::endl;
41f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    if(fatal_)
42f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      exit(1);
43f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
44f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  std::ostream &stream() { return std::cerr; }
45f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
46f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson private:
47f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool fatal_;
48f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
49f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
50f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define LOG(type) LogMessage(#type).stream()
51f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define VLOG(level) if ((level) <= FLAGS_v) LOG(INFO)
52f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
53f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Checks
54f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsoninline void CHECK(bool x) { assert(x); }
55f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
56f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define CHECK_EQ(x, y) CHECK((x) == (y))
57f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define CHECK_LT(x, y) CHECK((x) < (y))
58f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define CHECK_GT(x, y) CHECK((x) > (y))
59f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define CHECK_LE(x, y) CHECK((x) <= (y))
60f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define CHECK_GE(x, y) CHECK((x) >= (y))
61f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define CHECK_NE(x, y) CHECK((x) != (y))
62f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
63f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Ports
64f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
65f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
66f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#endif  // FST_LIB_LOG_H__
67