You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.2 KiB

  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. #ifndef FST_LOG_H_
  20. #define FST_LOG_H_
  21. #include <cassert>
  22. #include <cstdlib>
  23. #include <iostream>
  24. #include <ostream>
  25. #include <string_view>
  26. #include <fst/flags.h>
  27. class LogMessage;
  28. class LogMessage;
  29. DECLARE_int32(v);
  30. class LogMessage {
  31. public:
  32. explicit LogMessage(std::string_view type) : fatal_(type == "FATAL") {
  33. std::cerr << type << ": ";
  34. }
  35. ~LogMessage() {
  36. std::cerr << std::endl;
  37. if (fatal_) exit(1);
  38. }
  39. std::ostream &stream() { return std::cerr; }
  40. private:
  41. bool fatal_;
  42. };
  43. #define LOG(type) LogMessage(#type).stream()
  44. #define VLOG(level) if ((level) <= FST_FLAGS_v) LOG(INFO)
  45. // Checks.
  46. inline void FstCheck(bool x, std::string_view expr, std::string_view file,
  47. int line) {
  48. if (!x) {
  49. LOG(FATAL) << "Check failed: \"" << expr << "\" file: " << file
  50. << " line: " << line;
  51. }
  52. }
  53. #define CHECK(x) FstCheck(static_cast<bool>(x), #x, __FILE__, __LINE__)
  54. #define CHECK_EQ(x, y) CHECK((x) == (y))
  55. #define CHECK_LT(x, y) CHECK((x) < (y))
  56. #define CHECK_GT(x, y) CHECK((x) > (y))
  57. #define CHECK_LE(x, y) CHECK((x) <= (y))
  58. #define CHECK_GE(x, y) CHECK((x) >= (y))
  59. #define CHECK_NE(x, y) CHECK((x) != (y))
  60. // Debug checks.
  61. #define DCHECK(x) assert(x)
  62. #define DCHECK_EQ(x, y) DCHECK((x) == (y))
  63. #define DCHECK_LT(x, y) DCHECK((x) < (y))
  64. #define DCHECK_GT(x, y) DCHECK((x) > (y))
  65. #define DCHECK_LE(x, y) DCHECK((x) <= (y))
  66. #define DCHECK_GE(x, y) DCHECK((x) >= (y))
  67. #define DCHECK_NE(x, y) DCHECK((x) != (y))
  68. #endif // FST_LOG_H_