Added tests for logging
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
#include <check.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include "ucore_logging.h"
|
||||
|
||||
const char *file_name = "logfile_test.log";
|
||||
|
||||
//for cleaning the created log files..
|
||||
static void logging_rm_files(void)
|
||||
{
|
||||
remove(file_name);
|
||||
}
|
||||
|
||||
START_TEST (test_logfile_location)
|
||||
|
||||
struct uc_log_module m = {
|
||||
.id = 0,
|
||||
.short_name = "MOD1",
|
||||
.long_name = "Module 1",
|
||||
.log_level = UC_LL_DEBUG,
|
||||
};
|
||||
struct uc_log_modules mods = {
|
||||
.mods = &m,
|
||||
.cnt = 1,
|
||||
};
|
||||
struct stat st;
|
||||
struct uc_log_destination *dest;
|
||||
int rc;
|
||||
|
||||
uc_log_init(&mods);
|
||||
|
||||
dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1);
|
||||
fail_if(dest == NULL);
|
||||
uc_log_add_destination(dest);
|
||||
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
|
||||
rc = stat(file_name, &st);
|
||||
fail_if(rc != 0, "stat failed: %s\n", strerror(errno));
|
||||
fail_if(st.st_size != 79*2, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_logfile_no_location)
|
||||
|
||||
struct uc_log_module m = {
|
||||
.id = 0,
|
||||
.short_name = "MOD1",
|
||||
.long_name = "Module 1",
|
||||
.log_level = UC_LL_DEBUG,
|
||||
};
|
||||
struct uc_log_modules mods = {
|
||||
.mods = &m,
|
||||
.cnt = 1,
|
||||
};
|
||||
struct stat st;
|
||||
struct uc_log_destination *dest;
|
||||
int rc;
|
||||
|
||||
uc_log_init(&mods);
|
||||
|
||||
dest = uc_log_new_file(file_name, UC_LL_DEBUG, 0);
|
||||
fail_if(dest == NULL);
|
||||
uc_log_add_destination(dest);
|
||||
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
|
||||
rc = stat(file_name, &st);
|
||||
fail_if(rc != 0, "stat failed: %s\n", strerror(errno));
|
||||
fail_if(st.st_size != 55*2, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_logfile_loglevel_surpressed_dest)
|
||||
|
||||
struct uc_log_module m = {
|
||||
.id = 0,
|
||||
.short_name = "MOD1",
|
||||
.long_name = "Module 1",
|
||||
.log_level = UC_LL_DEBUG,
|
||||
};
|
||||
struct uc_log_modules mods = {
|
||||
.mods = &m,
|
||||
.cnt = 1,
|
||||
};
|
||||
struct stat st;
|
||||
struct uc_log_destination *dest;
|
||||
int rc;
|
||||
|
||||
uc_log_init(&mods);
|
||||
|
||||
|
||||
//file destination have UC_LL_WARNING, so nothing should be logged
|
||||
dest = uc_log_new_file(file_name, UC_LL_WARNING, 10);
|
||||
fail_if(dest == NULL);
|
||||
uc_log_add_destination(dest);
|
||||
|
||||
UC_LOGF(UC_LL_INFO, 1, "Lorum ipson %d", 1);
|
||||
|
||||
rc = stat(file_name, &st);
|
||||
fail_if(rc != 0, "stat failed: %s\n", strerror(errno));
|
||||
|
||||
fail_if(st.st_size != 0, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_logfile_loglevel_surpressed_module)
|
||||
|
||||
struct uc_log_module m = {
|
||||
.id = 0,
|
||||
.short_name = "MOD1",
|
||||
.long_name = "Module 1",
|
||||
//file destination have UC_LL_WARNING, so nothing should be logged
|
||||
.log_level = UC_LL_WARNING,
|
||||
};
|
||||
struct uc_log_modules mods = {
|
||||
.mods = &m,
|
||||
.cnt = 1,
|
||||
};
|
||||
struct stat st;
|
||||
struct uc_log_destination *dest;
|
||||
int rc;
|
||||
|
||||
uc_log_init(&mods);
|
||||
|
||||
|
||||
dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1);
|
||||
fail_if(dest == NULL);
|
||||
uc_log_add_destination(dest);
|
||||
|
||||
UC_LOGF(UC_LL_INFO, 0, "Lorum ipson %d", 100);
|
||||
|
||||
rc = stat(file_name, &st);
|
||||
fail_if(rc != 0, "stat failed: %s\n", strerror(errno));
|
||||
|
||||
fail_if(st.st_size != 0, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_logfile_reopen_files)
|
||||
|
||||
struct uc_log_module m = {
|
||||
.id = 0,
|
||||
.short_name = "MOD1",
|
||||
.long_name = "Module 1",
|
||||
.log_level = UC_LL_DEBUG,
|
||||
};
|
||||
struct uc_log_modules mods = {
|
||||
.mods = &m,
|
||||
.cnt = 1,
|
||||
};
|
||||
struct stat st;
|
||||
struct uc_log_destination *dest;
|
||||
int rc;
|
||||
|
||||
uc_log_init(&mods);
|
||||
|
||||
dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1);
|
||||
fail_if(dest == NULL);
|
||||
uc_log_add_destination(dest);
|
||||
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
|
||||
rc = stat(file_name, &st);
|
||||
fail_if(rc != 0, "stat failed: %s\n", strerror(errno));
|
||||
fail_if(st.st_size != 80*2, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement
|
||||
|
||||
rc = remove(file_name);
|
||||
fail_if(rc != 0, "Remove failed %s\n", strerror(errno));
|
||||
|
||||
rc = uc_log_reopen_files();
|
||||
fail_if(rc != 0, "uc_log_reopen_files failed: %d\n", rc);
|
||||
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
|
||||
rc = stat(file_name, &st);
|
||||
fail_if(rc != 0, "2. stat failed: %s\n", strerror(errno));
|
||||
fail_if(st.st_size != 80, "was %ld\n", (long)st.st_size);//should only one line there now
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_logfile_remove_dest)
|
||||
|
||||
struct uc_log_module m = {
|
||||
.id = 0,
|
||||
.short_name = "MOD1",
|
||||
.long_name = "Module 1",
|
||||
.log_level = UC_LL_DEBUG,
|
||||
};
|
||||
struct uc_log_modules mods = {
|
||||
.mods = &m,
|
||||
.cnt = 1,
|
||||
};
|
||||
struct stat st;
|
||||
struct uc_log_destination *dest;
|
||||
int rc;
|
||||
|
||||
uc_log_init(&mods);
|
||||
|
||||
dest = uc_log_new_file(file_name, UC_LL_DEBUG, 1);
|
||||
fail_if(dest == NULL);
|
||||
uc_log_add_destination(dest);
|
||||
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
|
||||
|
||||
rc = stat(file_name, &st);
|
||||
fail_if(rc != 0, "stat failed: %s\n", strerror(errno));
|
||||
fail_if(st.st_size != 80*2, "was %ld\n", (long)st.st_size);//should be 82 chars per log statement
|
||||
|
||||
uc_log_remove_destination(dest);
|
||||
|
||||
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1); //should not be logged now
|
||||
|
||||
rc = stat(file_name, &st);
|
||||
fail_if(rc != 0, "2. stat failed: %s\n", strerror(errno));
|
||||
fail_if(st.st_size != 80*2, "was %ld\n", (long)st.st_size);//should be same size
|
||||
|
||||
END_TEST
|
||||
|
||||
Suite *logging_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("logging");
|
||||
TCase *tc = tcase_create("ucore_logging");
|
||||
|
||||
tcase_add_test(tc, test_logfile_location);
|
||||
tcase_add_test(tc, test_logfile_no_location);
|
||||
tcase_add_test(tc, test_logfile_loglevel_surpressed_dest);
|
||||
tcase_add_test(tc, test_logfile_loglevel_surpressed_module);
|
||||
tcase_add_test(tc, test_logfile_reopen_files);
|
||||
tcase_add_test(tc, test_logfile_remove_dest);
|
||||
|
||||
tcase_add_checked_fixture(tc, logging_rm_files, logging_rm_files);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user