kaldi-error-test.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // base/kaldi-error-test.cc
  2. // Copyright 2009-2011 Microsoft Corporation
  3. // See ../../COPYING for clarification regarding multiple authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  13. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  14. // MERCHANTABLITY OR NON-INFRINGEMENT.
  15. // See the Apache 2 License for the specific language governing permissions and
  16. // limitations under the License.
  17. #include "base/kaldi-common.h"
  18. // testing that we get the stack trace.
  19. namespace kaldi {
  20. void MyFunction2() { KALDI_ERR << "Ignore this error"; }
  21. void MyFunction1() { MyFunction2(); }
  22. void UnitTestError() {
  23. {
  24. std::cerr << "Ignore next error:\n";
  25. MyFunction1();
  26. }
  27. }
  28. void VerifySymbolRange(const std::string &trace, const bool want_found,
  29. const std::string &want_symbol) {
  30. size_t begin, end;
  31. const bool found = internal::LocateSymbolRange(trace, &begin, &end);
  32. if (found != want_found) {
  33. KALDI_ERR << "Found mismatch, got " << found << " want " << want_found;
  34. }
  35. if (!found) {
  36. return;
  37. }
  38. const std::string symbol = trace.substr(begin, end - begin);
  39. if (symbol != want_symbol) {
  40. KALDI_ERR << "Symbol mismatch, got " << symbol << " want " << want_symbol;
  41. }
  42. }
  43. void TestLocateSymbolRange() {
  44. VerifySymbolRange("", false, "");
  45. VerifySymbolRange(
  46. R"TRACE(./kaldi-error-test(_ZN5kaldi13UnitTestErrorEv+0xb) [0x804965d])TRACE",
  47. true, "_ZN5kaldi13UnitTestErrorEv");
  48. // It is ok thread_start is not found because it is a C symbol.
  49. VerifySymbolRange(
  50. R"TRACE(31 libsystem_pthread.dylib 0x00007fff6fe4e40d thread_start + 13)TRACE",
  51. false, "");
  52. VerifySymbolRange(
  53. R"TRACE(0 server 0x000000010f67614d _ZNK5kaldi13MessageLogger10LogMessageEv + 813)TRACE",
  54. true, "_ZNK5kaldi13MessageLogger10LogMessageEv");
  55. VerifySymbolRange(
  56. R"TRACE(29 libsystem_pthread.dylib 0x00007fff6fe4f2eb _pthread_body + 126)TRACE",
  57. true, "_pthread_body");
  58. }
  59. } // namespace kaldi
  60. int main() {
  61. kaldi::TestLocateSymbolRange();
  62. kaldi::SetProgramName("/foo/bar/kaldi-error-test");
  63. try {
  64. kaldi::UnitTestError();
  65. KALDI_ASSERT(0); // should not happen.
  66. exit(1);
  67. } catch (kaldi::KaldiFatalError &e) {
  68. std::cout << "The error we generated was: '" << e.KaldiMessage() << "'\n";
  69. }
  70. }