Bring aboard log level parsing function.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sun, 19 Feb 2012 21:05:23 +0000 (19:05 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sun, 19 Feb 2012 21:05:23 +0000 (19:05 -0200)
src/CMakeLists.txt
src/host/loglevel.cpp [new file with mode: 0644]
src/host/loglevel.h [new file with mode: 0644]
test/CMakeLists.test_loglevel.txt [new file with mode: 0644]
test/CMakeLists.txt
test/test_loglevel.cpp [new file with mode: 0644]

index 793414f..4f678e8 100644 (file)
@@ -49,6 +49,7 @@ set(SOURCES
     config/option/hostsdownlimitoption.cpp
     config/option/linkdownintervaloption.cpp
     config/option/linkupintervaloption.cpp
+    config/option/logleveloption.cpp
     config/option/nameserveroption.cpp
     config/option/pingfaillimitoption.cpp
     config/option/sourcenetworkinterfaceoption.cpp
@@ -58,6 +59,7 @@ set(SOURCES
     dns/hostaddress.cpp
     dns/timetolive.cpp
     host/hoststatus.cpp
+    host/loglevel.cpp
     host/messagepayload.cpp
     host/networkinterfacelist.cpp
     host/pinger.cpp
diff --git a/src/host/loglevel.cpp b/src/host/loglevel.cpp
new file mode 100644 (file)
index 0000000..a396011
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
+
+#include "host/loglevel.h"
+
+#include <algorithm>
+#include <map>
+
+#include <boost/assert.hpp>
+
+using namespace std;
+
+static map<string, LogLevel> log_level_string_map;
+
+LogLevel get_log_level_from_string( const string & log_level_string )
+{
+    BOOST_ASSERT( !log_level_string.empty() );
+
+    // convert to uppercase to allow the log_level to be case insensitive
+    string log_level_uppercase_string( log_level_string );
+    transform( log_level_string.begin(), log_level_string.end(),
+               log_level_uppercase_string.begin(),
+               ::toupper );
+
+    // TODO move to an init method
+    log_level_string_map[ "DEBUG" ] = LogLevel_DEBUG;
+    log_level_string_map[ "INFO" ] = LogLevel_INFO;
+    log_level_string_map[ "NOTICE" ] = LogLevel_NOTICE;
+    log_level_string_map[ "WARNING" ] = LogLevel_WARNING;
+    log_level_string_map[ "ERROR" ] = LogLevel_ERROR;
+    log_level_string_map[ "CRITICAL" ] = LogLevel_CRITICAL;
+    log_level_string_map[ "ALERT" ] = LogLevel_ALERT;
+    log_level_string_map[ "EMERGENCY" ] = LogLevel_EMERGENCY;
+
+    LogLevel log_level = log_level_string_map[ log_level_uppercase_string ];
+
+    return log_level;
+}
diff --git a/src/host/loglevel.h b/src/host/loglevel.h
new file mode 100644 (file)
index 0000000..280e780
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef LOG_LEVEL_H
+#define LOG_LEVEL_H
+
+#include <string>
+
+enum LogLevel
+{
+    LogLevel_First = 0,
+    LogLevel_DEBUG = LogLevel_First,
+    LogLevel_INFO,
+    LogLevel_NOTICE,
+    LogLevel_WARNING,
+    LogLevel_ERROR,
+    LogLevel_CRITICAL,
+    LogLevel_ALERT,
+    LogLevel_EMERGENCY,
+    LogLevel_Last = LogLevel_EMERGENCY
+};
+
+LogLevel get_log_level_from_string( const std::string & log_level_string );
+
+#endif // LOG_LEVEL_H
diff --git a/test/CMakeLists.test_loglevel.txt b/test/CMakeLists.test_loglevel.txt
new file mode 100644 (file)
index 0000000..86da955
--- /dev/null
@@ -0,0 +1,15 @@
+# compiler: creates the binaries
+add_executable(test_loglevel
+    test_loglevel.cpp
+    ${CMAKE_SOURCE_DIR}/src/host/loglevel.cpp
+)
+
+# linker: link the program against the libraries
+target_link_libraries(
+    test_loglevel
+    ${I2NCOMMON_LIBRARIES}
+    ${Boost_LIBRARIES}
+)
+
+# cmake: invocation via "make test"
+add_test(test_loglevel test_loglevel)
index 885455e..1fdeb02 100644 (file)
@@ -31,6 +31,7 @@ include(CMakeLists.test_messagepayload.txt)
 include(CMakeLists.test_pingprotocol.txt)
 include(CMakeLists.test_hoststatus.txt)
 include(CMakeLists.test_linkstatus.txt)
+include(CMakeLists.test_loglevel.txt)
 include(CMakeLists.test_ipv4header.txt)
 include(CMakeLists.test_ipv6header.txt)
 include(CMakeLists.test_icmpv4header.txt)
@@ -46,8 +47,10 @@ add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS
     test_pingprotocol
     test_hoststatus
     test_linkstatus
+    test_loglevel
     test_ipv4header
     test_ipv6header
     test_icmpv4header
     test_icmpv6header
+    test_tcpheader
 )
diff --git a/test/test_loglevel.cpp b/test/test_loglevel.cpp
new file mode 100644 (file)
index 0000000..7499073
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
+
+#define BOOST_TEST_MAIN
+#define BOOST_TEST_DYN_LINK
+
+#include <boost/test/unit_test.hpp>
+
+#include "host/loglevel.h"
+
+BOOST_AUTO_TEST_SUITE( TestLogLevel )
+
+BOOST_AUTO_TEST_CASE( lowercase )
+{
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "debug" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "info" ), LogLevel_INFO );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "notice" ), LogLevel_NOTICE );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "warning" ), LogLevel_WARNING );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "error" ), LogLevel_ERROR );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "critical" ), LogLevel_CRITICAL );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "alert" ), LogLevel_ALERT );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "emergency" ), LogLevel_EMERGENCY );
+}
+
+BOOST_AUTO_TEST_CASE( uppercase )
+{
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "DEBUG" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "INFO" ), LogLevel_INFO );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "NOTICE" ), LogLevel_NOTICE );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "WARNING" ), LogLevel_WARNING );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "ERROR" ), LogLevel_ERROR );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "CRITICAL" ), LogLevel_CRITICAL );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "ALERT" ), LogLevel_ALERT );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "EMERGENCY" ), LogLevel_EMERGENCY );
+}
+
+BOOST_AUTO_TEST_CASE( mixed_case )
+{
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "DeBuG" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "InfO" ), LogLevel_INFO );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "NoTicE" ), LogLevel_NOTICE );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "warNing" ), LogLevel_WARNING );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "errOR" ), LogLevel_ERROR );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "critiCaL" ), LogLevel_CRITICAL );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "aleRt" ), LogLevel_ALERT );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "EmerGENCy" ), LogLevel_EMERGENCY );
+}
+
+BOOST_AUTO_TEST_CASE( misspelled )
+{
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "when" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "the" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "word" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "is" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "wrong" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "stick" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "to" ), LogLevel_DEBUG );
+    BOOST_CHECK_EQUAL( get_log_level_from_string( "DEBUG" ), LogLevel_DEBUG );
+}
+
+BOOST_AUTO_TEST_SUITE_END()