test_logging.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*************************************************************************
  2. * Copyright (C) [2021] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <string>
  22. #include <thread>
  23. #include "cnstream_logging.hpp"
  24. using cnstream::InitCNStreamLogging;
  25. using cnstream::ShutdownCNStreamLogging;
  26. TEST(CoreLog, CreateLogFile) {
  27. FLAGS_log_to_file = true;
  28. InitCNStreamLogging("/tmp");
  29. LOGI(CoreLog) << "Create log file at current directory";
  30. ShutdownCNStreamLogging();
  31. InitCNStreamLogging("unexist_directory");
  32. LOGE(CoreLog) << "Create log file at non-exist directory";
  33. ShutdownCNStreamLogging();
  34. std::string longlog(1028, '=');
  35. LOGW(CoreLog) << "Test long log " << longlog;
  36. }
  37. // This test case sometimes results in hang.
  38. //
  39. // TEST(CoreLog, LogFatal) {
  40. // InitCNStreamLogging("./");
  41. // EXPECT_DEATH_IF_SUPPORTED(LOGF(CoreLog) << "Should abort", "");
  42. // ShutdownCNStreamLogging();
  43. // }
  44. TEST(CoreLog, LogSink) {
  45. class MyLogSink : public cnstream::LogSink {
  46. void Send(cnstream::LogSeverity severity, const char* category, const char* filename, int line,
  47. const struct ::tm* tm_time, int32_t usecs, const char* message, size_t message_len) override {
  48. EXPECT_EQ(3, static_cast<int>(severity));
  49. EXPECT_STREQ(category, "CoreLog");
  50. EXPECT_STREQ("test_logging.cpp", filename);
  51. EXPECT_STREQ(message, "This log should be transmitted by LogSink::Send\n");
  52. std::cout << "MyLogSink: " << ToString(severity, category, filename, line, tm_time, usecs, message, message_len)
  53. << std::endl;
  54. }
  55. void WaitTillSent() override { std::cout << "MyLogSink Done" << std::endl; }
  56. };
  57. MyLogSink mysink;
  58. MyLogSink mysink1;
  59. cnstream::AddLogSink(&mysink);
  60. cnstream::AddLogSink(&mysink1);
  61. LOGI(CoreLog) << "This log should be transmitted by LogSink::Send";
  62. cnstream::RemoveLogSink(&mysink);
  63. cnstream::RemoveLogSink(&mysink1);
  64. }