lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 7fa89ea6e764d58e86c9e1f3017fac0fde5bffba
parent eb6e405e86e96aff2fa01e22e79df06f68e403be
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 24 Apr 2017 19:07:25 +0200

Add(Log): Now printing log level in log:

Updated tests too

Diffstat:
Minc/m_print.h | 5+++++
Msrc/m_print.c | 4++++
Mtests/test_print.c | 24++++++++++++------------
3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/inc/m_print.h b/inc/m_print.h @@ -47,6 +47,11 @@ # define M_WARN_BEG M_COLOR_RED M_LOG_DEF_CHAR " " M_COLOR_RESET # define M_INFO_BEG M_COLOR_BLUE M_LOG_DEF_CHAR " " M_COLOR_RESET +# define M_LOG_PANIC_BEG "[PANIC] " +# define M_LOG_ERROR_BEG "[ERROR] " +# define M_LOG_WARN_BEG "[WARN] " +# define M_LOG_INFO_BEG "[INFO] " + /*! * \brief Print a string in an error fomat, then call exit with 1 * \note Support printf format diff --git a/src/m_print.c b/src/m_print.c @@ -65,6 +65,7 @@ void m_panic(const char *str, ...) { va_start(ap, str); if (g_log_flags & M_LOG_FORCE) { + m_log(M_LOG_PANIC_BEG); m_log_v(str, ap); } else @@ -85,6 +86,7 @@ void m_error(const char *str, ...) { va_start(ap, str); if (g_log_flags & M_LOG_FORCE) { + m_log(M_LOG_ERROR_BEG); m_log_v(str, ap); } else @@ -104,6 +106,7 @@ void m_warning(const char *str, ...) { va_start(ap, str); if (g_log_flags & M_LOG_FORCE) { + m_log(M_LOG_WARN_BEG); m_log_v(str, ap); } else @@ -120,6 +123,7 @@ void m_info(const char *str, ...) { va_start(ap, str); if (g_log_flags & M_LOG_FORCE) { + m_log(M_LOG_INFO_BEG); m_log_v(str, ap); } else diff --git a/tests/test_print.c b/tests/test_print.c @@ -116,12 +116,12 @@ TEST(print_log_minfo) { m_info("Test 123"); int fd = open(TEST_LOG_FN, O_RDONLY); - char buf[8]; + char buf[14]; TEST_ASSERT(fd != 0, "Can't open the log file"); - TEST_ASSERT(read(fd, buf, 8) == 8, "Read return is wrong"); + TEST_ASSERT(read(fd, buf, 14) == 14, "Read return is wrong"); close(fd); - TEST_ASSERT(memcmp(buf, "Test 123", 8) == 0, "Content of the file is wrong"); + TEST_ASSERT(memcmp(buf, "[INFO] Test 123", 14) == 0, "Content of the file is wrong"); TEST_ASSERT(m_clean_log() == true, "Return is wrong"); return TEST_SUCCESS; } @@ -131,11 +131,11 @@ TEST(print_log_mwarning) { m_warning("Test 123"); int fd = open(TEST_LOG_FN, O_RDONLY); - char buf[8]; + char buf[14]; TEST_ASSERT(fd != 0, "Can't open the log file"); - TEST_ASSERT(read(fd, buf, 8) == 8, "Read return is wrong"); - TEST_ASSERT(memcmp(buf, "Test 123", 8) == 0, "Content of the file is wrong"); + TEST_ASSERT(read(fd, buf, 14) == 14, "Read return is wrong"); + TEST_ASSERT(memcmp(buf, "[WARN] Test 123", 14) == 0, "Content of the file is wrong"); close(fd); TEST_ASSERT(m_clean_log() == true, "Return is wrong"); return TEST_SUCCESS; @@ -146,11 +146,11 @@ TEST(print_log_merror) { m_error("Test 123"); int fd = open(TEST_LOG_FN, O_RDONLY); - char buf[8]; + char buf[15]; TEST_ASSERT(fd != 0, "Can't open the log file"); - TEST_ASSERT(read(fd, buf, 8) == 8, "Read return is wrong"); - TEST_ASSERT(memcmp(buf, "Test 123", 8) == 0, "Content of the file is wrong"); + TEST_ASSERT(read(fd, buf, 15) == 15, "Read return is wrong"); + TEST_ASSERT(memcmp(buf, "[ERROR] Test 123", 15) == 0, "Content of the file is wrong"); close(fd); TEST_ASSERT(m_clean_log() == true, "Return is wrong"); return TEST_SUCCESS; @@ -172,12 +172,12 @@ TEST(print_log_mpanic) { } int fd = open(TEST_LOG_FN, O_RDONLY); - char buf[8]; + char buf[15]; TEST_ASSERT(fd != 0, "Can't open the log file"); - TEST_ASSERT(read(fd, buf, 8) == 8, "Read return is wrong"); + TEST_ASSERT(read(fd, buf, 15) == 15, "Read return is wrong"); close(fd); - TEST_ASSERT(memcmp(buf, "Test 123", 8) == 0, "Content of the file is wrong"); + TEST_ASSERT(memcmp(buf, "[PANIC] Test 123", 15) == 0, "Content of the file is wrong"); TEST_ASSERT(m_clean_log() == true, "Return is wrong"); return TEST_SUCCESS; }