| 1 | /* Copyright (c) 2016, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #ifndef TEST_LOGGER_INCLUDED |
| 25 | #define TEST_LOGGER_INCLUDED |
| 26 | #include "gcs_xcom_interface.h" |
| 27 | #include "mysql/gcs/gcs_logging_system.h" |
| 28 | |
| 29 | /* |
| 30 | It is a logger utility helps unit test to verify functions which will log |
| 31 | errors correctly. |
| 32 | |
| 33 | Usage: |
| 34 | #include "test_logger.h" |
| 35 | |
| 36 | test_logger.clear_event(); // clear all logged events before a test. |
| 37 | ... |
| 38 | test_logger.assert_error("Expected error message"); |
| 39 | */ |
| 40 | class Test_logger : public Logger_interface { |
| 41 | private: |
| 42 | std::stringstream m_log_stream; |
| 43 | |
| 44 | std::string get_event() { return m_log_stream.str(); } |
| 45 | |
| 46 | void assert_event(gcs_log_level_t level, const std::string &expected) { |
| 47 | std::string complete_log(gcs_log_levels[level]); |
| 48 | complete_log += GCS_PREFIX; |
| 49 | complete_log += expected; |
| 50 | |
| 51 | ASSERT_EQ(complete_log, get_event()); |
| 52 | } |
| 53 | |
| 54 | public: |
| 55 | Test_logger() { Gcs_log_manager::initialize(this); } |
| 56 | |
| 57 | ~Test_logger() {} |
| 58 | |
| 59 | enum_gcs_error initialize() { return GCS_OK; } |
| 60 | |
| 61 | enum_gcs_error finalize() { return GCS_OK; } |
| 62 | |
| 63 | void log_event(const gcs_log_level_t level, const std::string &message) { |
| 64 | m_log_stream << gcs_log_levels[level] << message; |
| 65 | } |
| 66 | |
| 67 | void clear_event() { m_log_stream.str(""); } |
| 68 | |
| 69 | void assert_error(const std::string &expected) { |
| 70 | assert_event(GCS_ERROR, expected); |
| 71 | } |
| 72 | |
| 73 | void assert_error(const std::stringstream &expected) { |
| 74 | assert_error(expected.str()); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | Test_logger test_logger; |
| 79 | |
| 80 | #endif // TEST_LOGGER_INCLUDED |