* Test Logging. * * Note: In order to let these tests pass the formatting of the expected logMessage has to stay directly after the LogMessage (__LINE__-1), * otherwise the tests will fail. */
| 123 | * otherwise the tests will fail. |
| 124 | */ |
| 125 | static void testLogging() |
| 126 | { |
| 127 | TestLogger myTestLogger; |
| 128 | LogSinkPrinter myLogSink("test", &myTestLogger); |
| 129 | const char* printBuffer = nullptr; |
| 130 | const char* LOG_MODULE = strrchr(__FILE__, '\\'); /* Windows backslash */ |
| 131 | const char* TEST_STRING_1 = "TestMessage"; |
| 132 | const String TEST_STRING_2 = "TestMessageAsString"; |
| 133 | char expectedLogMessage[128U]; |
| 134 | int lineNo = 0; |
| 135 | const size_t TIMESTAMP_IDX = 0U; |
| 136 | const size_t LOG_LEVEL_IDX = LogSinkPrinter::TIMESTAMP_LEN + 1U; |
| 137 | const size_t FILENAME_IDX = LOG_LEVEL_IDX + LogSinkPrinter::LOG_LEVEL_LEN + 1U; |
| 138 | const size_t MESSAGE_IDX = LogSinkPrinter::MSG_INDEX; |
| 139 | String expected; |
| 140 | |
| 141 | /* If no windows backslash is found, maybe its a linux slash? */ |
| 142 | if (nullptr == LOG_MODULE) |
| 143 | { |
| 144 | LOG_MODULE = strrchr(__FILE__, '/'); |
| 145 | } |
| 146 | |
| 147 | /* No directory found in filename. */ |
| 148 | if (nullptr == LOG_MODULE) |
| 149 | { |
| 150 | LOG_MODULE = __FILE__; |
| 151 | } |
| 152 | /* Overstep backslash/slash. */ |
| 153 | else |
| 154 | { |
| 155 | ++LOG_MODULE; |
| 156 | } |
| 157 | |
| 158 | /* Check initial LogLevel. */ |
| 159 | TEST_ASSERT_TRUE(Logging::getInstance().registerSink(&myLogSink)); |
| 160 | TEST_ASSERT_TRUE(Logging::getInstance().selectSink("test")); |
| 161 | TEST_ASSERT_EQUAL(Logging::getInstance().getLogLevel() , Logging::LOG_LEVEL_INFO); |
| 162 | |
| 163 | /* Set LogLevel to LOGLEVEL_INFO. */ |
| 164 | Logging::getInstance().setLogLevel(Logging::LOG_LEVEL_INFO); |
| 165 | TEST_ASSERT_EQUAL(Logging::getInstance().getLogLevel() , Logging::LOG_LEVEL_INFO); |
| 166 | |
| 167 | /* Set LogLevel to LOG_LEVEL_ERROR and trigger a LOG_INFO message. */ |
| 168 | Logging::getInstance().setLogLevel(Logging::LOG_LEVEL_ERROR); |
| 169 | LOG_INFO(TEST_STRING_1); |
| 170 | (void)snprintf(expectedLogMessage, sizeof(expectedLogMessage), "%s", ""); |
| 171 | printBuffer = myTestLogger.getBuffer(); |
| 172 | TEST_ASSERT_EQUAL_STRING(expectedLogMessage, printBuffer); |
| 173 | |
| 174 | /* Check expected error log output, with type const char* string. */ |
| 175 | LOG_ERROR(TEST_STRING_1); lineNo = __LINE__; |
| 176 | |
| 177 | /* Skip timestamp */ |
| 178 | |
| 179 | /* Check log level */ |
| 180 | expected = "ERROR"; |
| 181 | TEST_ASSERT_EQUAL_STRING_LEN(expected.c_str(), &printBuffer[LOG_LEVEL_IDX], expected.length()); |
| 182 |
nothing calls this directly
no test coverage detected