test/logging_tests.c

changeset 170
5fbb9efebe4a
parent 134
4d320dc3a7af
child 177
11ad03783baf
equal deleted inserted replaced
169:279dd3ca7a77 170:5fbb9efebe4a
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "logging_tests.h" 29 #include "logging_tests.h"
30 #include <time.h>
31
32 UCX_TEST(test_ucx_logger_new) {
33
34 FILE *stream = tmpfile();
35 UcxLogger *logger = ucx_logger_new(stream,
36 UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
37
38 UCX_TEST_BEGIN
39 UCX_TEST_ASSERT(logger->stream == stream, "stream not set");
40 UCX_TEST_ASSERT(logger->mask == (UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL),
41 "mask not set");
42 UCX_TEST_ASSERT(logger->level == UCX_LOGGER_INFO,
43 "level not set");
44 UCX_TEST_ASSERT(logger->writer == (write_func)fwrite,
45 "writer not set to fwrite");
46 UCX_TEST_ASSERT(strcmp(logger->dateformat, "%F %T %z ") == 0,
47 "date format not set to \"%F %T %z\"");
48
49 UCX_TEST_ASSERT(logger->levels->count == 4,
50 "incorrect number of registered log levels");
51
52 int level = UCX_LOGGER_ERROR;
53 UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
54 "[ERROR]") == 0, "invalid error level");
55 level = UCX_LOGGER_WARN;
56 UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
57 "[WARNING]") == 0, "invalid warning level");
58 level = UCX_LOGGER_INFO;
59 UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
60 "[INFO]") == 0, "invalid info level");
61 level = UCX_LOGGER_TRACE;
62 UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
63 "[TRACE]") == 0, "invalid trace level");
64
65 UCX_TEST_END
66
67 fclose(stream);
68 ucx_logger_free(logger);
69 }
30 70
31 UCX_TEST(test_ucx_logger_log) { 71 UCX_TEST(test_ucx_logger_log) {
32 char buffer[100]; 72 char buffer[100];
73
33 FILE *stream = tmpfile(); 74 FILE *stream = tmpfile();
34 75
35 UcxLogger *logger = ucx_logger_new(stream, 76 UcxLogger *logger = ucx_logger_new(stream,
36 UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL); 77 UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
78 logger->dateformat = "%F:";
37 79
38 UCX_TEST_BEGIN 80 UCX_TEST_BEGIN
39 ucx_logger_info(logger, "allright"); 81 const uint line1 = __LINE__; ucx_logger_info(logger, "allright");
82
40 ucx_logger_trace(logger, "dont log this!"); 83 ucx_logger_trace(logger, "dont log this!");
41 ucx_logger_error(logger, "error %d!", 42); 84
85 logger->mask |= UCX_LOGGER_TIMESTAMP;
86 time_t now = time(NULL);
87 char timestr[13];
88 strftime(timestr, 12, "%F:", localtime(&now));
89 const uint line2 = __LINE__; ucx_logger_error(logger, "error %d!", 42);
90
42 fseek(stream, 0, SEEK_SET); 91 fseek(stream, 0, SEEK_SET);
43 size_t r = fread(buffer, 1, 100, stream); 92 size_t r = fread(buffer, 1, 100, stream);
44 93
45 // TODO: completely rewrite this test 94 const size_t expected_length = 87;
46 95 char expected[expected_length+1];
47 size_t expected_length = 76; 96 snprintf(expected, expected_length+1,
48 UCX_TEST_ASSERT(r == expected_length && strncmp(buffer, 97 "[INFO] logging_tests.c:%u - allright\n"
49 "[INFO] logging_tests.c:39 - allright\n" 98 "[ERROR] %slogging_tests.c:%u - error 42!\n", line1, timestr, line2);
50 "[ERROR] logging_tests.c:41 - error 42!\n", expected_length) == 0, "incorrect logs"); 99
100 UCX_TEST_ASSERT(r == expected_length, "incorrect log length");
101 UCX_TEST_ASSERT(strncmp(buffer, expected, expected_length) == 0,
102 "incorrect logs");
51 103
52 UCX_TEST_END 104 UCX_TEST_END
53 105
54 ucx_logger_free(logger); 106 ucx_logger_free(logger);
55 fclose(stream); 107 fclose(stream);

mercurial