Skip to content
Browse files

adding ts_logger module

  • Loading branch information...
1 parent a96fb7f commit 0b54e9aeeef87cdf67ec9aa25cc7683bfa342eda Ivan Sidarau committed May 15, 2012
View
2 CMakeLists.txt
@@ -4,4 +4,4 @@ set( CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "Configurations"
project( ${SOLUTION_NAME} )
include( environment.cmake required )
-modules( time_tracker ts_queue property_reader task_processor logger queue_logger file_logger system_processor )
+modules( time_tracker ts_queue property_reader task_processor logger ts_logger queue_logger file_logger system_processor )
View
4 README
@@ -28,3 +28,7 @@ Description: file_logger module create template class that can log information u
* system_processor module, created by Ivan Sidarau
Description: system_processor module is a singleton based class that gave simple possibility to create special waiter.
+
+ * ts_logger module, created by Ivan Sidarau.
+Description: logger<> like module with mutex synchonization on write method, could be used in several threads (if performance is a must, consider using queue_logger).
+
View
2 _msvc_gen_2005_64.bat
@@ -13,7 +13,7 @@ if not exist %BUILD_FOLDER% (
)
cd %BUILD_FOLDER%
-cmake -DVERBOSE=OFF -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DSOLUTION_NAME=system_utilities -DVERBOSE=ON -G "Visual Studio 8 2005 Win64" ../
+cmake -DVERBOSE=OFF -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DSOLUTION_NAME=system_utilities -G "Visual Studio 8 2005 Win64" ../
cd ../
echo "%BUILD_FOLDER%/system_utilities.sln" > _start_msvc.bat
View
10 sources/ts_logger/CMakeLists.txt
@@ -0,0 +1,10 @@
+find_package( Boost 1.41 ${SEARCH_PARAMETERS} COMPONENTS
+ filesystem
+ thread
+ regex
+ date_time
+ system
+)
+
+compile_project( ${module_name} "*.cpp" "*.h" STATIC libraries logger Boost )
+
View
1 sources/ts_logger/ts_logger.cpp
@@ -0,0 +1 @@
+#include "ts_logger.h"
View
49 sources/ts_logger/ts_logger.h
@@ -0,0 +1,49 @@
+#ifndef _SYSTEM_UTILITIES_COMMON_TS_LOGGER_H_
+#define _SYSTEM_UTILITIES_COMMON_TS_LOGGER_H_
+
+#include <logger.h>
+
+#include <boost/thread.hpp>
+
+namespace system_utilities
+{
+ namespace tests_
+ {
+ namespace common
+ {
+ void ts_logger_write_tests();
+ };
+ }
+ namespace common
+ {
+
+ template< bool turn_on = true, bool flush_stream = true, bool print_prefix = true >
+ class ts_logger : public logger< turn_on, flush_stream, print_prefix >
+ {
+ friend void tests_::common::ts_logger_write_tests();
+
+
+ mutable boost::mutex protect_write_;
+
+ explicit ts_logger( const ts_logger& copy_from );
+ public:
+ explicit ts_logger( std::ostream& out )
+ : logger< turn_on, flush_stream, print_prefix >( out )
+ {
+ }
+ virtual ~ts_logger()
+ {
+ }
+ protected:
+ void write( const details::message_level::value value, const std::string& message )
+ {
+ boost::mutex::scoped_lock lock( protect_write_ );
+ logger< turn_on, flush_stream, print_prefix >::write( value, message );
+ }
+ };
+
+ };
+};
+
+#endif // _SYSTEM_UTILITIES_COMMON_TS_LOGGER_H_
+
View
12 tests/logger_tests/logger_tests.cpp
@@ -44,11 +44,11 @@ namespace system_utilities
void logger_constructor_tests()
{
std::stringstream str;
- logger< false > l_false( str );
- logger< true > l_true( str );
- logger< true, false > l_true_false( str );
- logger< true, false, false > l_true_false_false( str );
- logger< true, true, false > l_true_true_false( str );
+ BOOST_CHECK_NO_THROW( (logger< false >( str )) );
+ BOOST_CHECK_NO_THROW( (logger< true >( str )) );
+ BOOST_CHECK_NO_THROW( (logger< true, false >( str )) );
+ BOOST_CHECK_NO_THROW( (logger< true, false, false >( str )) );
+ BOOST_CHECK_NO_THROW( (logger< true, true, false >( str )) );
}
void logger_write_tests()
{
@@ -88,7 +88,7 @@ namespace system_utilities
{
std::stringstream it_is_just_a_test_on_existing_flush_method;
logger<> l( it_is_just_a_test_on_existing_flush_method );
- l.flush();
+ BOOST_CHECK_NO_THROW( l.flush() );
}
void logger_note_tests()
{
View
11 tests/ts_logger_tests/CMakeLists.txt
@@ -0,0 +1,11 @@
+
+find_package( Boost 1.41 ${SEARCH_PARAMETERS} COMPONENTS
+ filesystem
+ thread
+ regex
+ date_time
+ system
+ unit_test_framework
+)
+compile_project( ${tests_name} "*.cpp" "*.h" BINARY tests ${module_name} logger time_tracker Boost )
+register_test( ${tests_name} 0.5 8.1 )
View
28 tests/ts_logger_tests/test_registrator.cpp
@@ -0,0 +1,28 @@
+#include "test_registrator.h"
+
+#include <boost/test/unit_test.hpp>
+using boost::unit_test_framework::test_suite;
+using boost::unit_test_framework::test_case;
+
+test_suite* init_unit_test_suite( int argc, char* argv[] )
+{
+ test_suite* ts1 = BOOST_TEST_SUITE( "ts_logger tests" );
+ boost::unit_test::unit_test_log.set_threshold_level( boost::unit_test::log_messages );
+
+ using namespace system_utilities::tests_::common;
+ ts1->add( BOOST_TEST_CASE( &ts_logger_constructor_tests ) );
+ ts1->add( BOOST_TEST_CASE( &ts_logger_write_tests ) );
+ ts1->add( BOOST_TEST_CASE( &ts_logger_flush_tests ) );
+ //
+ ts1->add( BOOST_TEST_CASE( &ts_logger_note_tests ) );
+ ts1->add( BOOST_TEST_CASE( &ts_logger_warn_tests ) );
+ ts1->add( BOOST_TEST_CASE( &ts_logger_error_tests ) );
+ ts1->add( BOOST_TEST_CASE( &ts_logger_debug_tests ) );
+ ts1->add( BOOST_TEST_CASE( &ts_logger_fatal_tests ) );
+ //
+#ifdef RUN_PERFORMANCE_TESTS
+ ts1->add( BOOST_TEST_CASE( &ts_logger_write_performance_tests ) );
+#endif
+
+ return ts1;
+}
View
22 tests/ts_logger_tests/test_registrator.h
@@ -0,0 +1,22 @@
+#include <boost/test/test_tools.hpp>
+
+namespace system_utilities
+{
+ namespace tests_
+ {
+ namespace common
+ {
+ void ts_logger_constructor_tests();
+ void ts_logger_write_tests();
+ void ts_logger_flush_tests();
+ void ts_logger_note_tests();
+ void ts_logger_warn_tests();
+ void ts_logger_error_tests();
+ void ts_logger_debug_tests();
+ void ts_logger_fatal_tests();
+ //
+ void ts_logger_write_performance_tests();
+ }
+ }
+}
+
View
199 tests/ts_logger_tests/ts_logger_tests.cpp
@@ -0,0 +1,199 @@
+#include "test_registrator.h"
+
+#include <ts_logger.h>
+#include <time_tracker.h>
+
+#include <boost/regex.hpp>
+
+using namespace system_utilities::common;
+
+namespace system_utilities
+{
+ namespace tests_
+ {
+ namespace common
+ {
+ namespace details
+ {
+ template< bool turn_on, bool flush_stream, bool print_prefix >
+ void ts_logger_write_performance_test_helper( const size_t millisecs_for_stream, const size_t millisecs_for_method )
+ {
+ typedef ts_logger< turn_on, flush_stream, print_prefix > ts_logger;
+ {
+ std::stringstream stream;
+ ts_logger l( stream );
+ time_tracker tt;
+ static const size_t test_size = 200000;
+ for ( size_t i = 0 ; i < test_size ; ++i )
+ l.note() << "0123456789qwertyuioplkjhgfdsazxcvbnm";
+ std::cout << tt.milliseconds() << std::endl;
+ BOOST_CHECK_EQUAL( tt.milliseconds() < millisecs_for_stream, true );
+ }
+ {
+ std::stringstream stream;
+ ts_logger l( stream );
+ time_tracker tt;
+ static const size_t test_size = 200000;
+ for ( size_t i = 0 ; i < test_size ; ++i )
+ l.note( "0123456789qwertyuioplkjhgfdsazxcvbnm" );
+ std::cout << tt.milliseconds() << std::endl;
+ BOOST_CHECK_EQUAL( tt.milliseconds() < millisecs_for_method, true );
+ }
+ }
+ }
+ void ts_logger_constructor_tests()
+ {
+ std::stringstream str;
+ BOOST_CHECK_NO_THROW( (ts_logger< false >( str )) );
+ BOOST_CHECK_NO_THROW( (ts_logger< true >( str )) );
+ BOOST_CHECK_NO_THROW( (ts_logger< true, false >( str )) );
+ BOOST_CHECK_NO_THROW( (ts_logger< true, false, false >( str )) );
+ BOOST_CHECK_NO_THROW( (ts_logger< true, true, false >( str )) );
+ }
+ void ts_logger_write_tests()
+ {
+ {
+ std::stringstream stream;
+ ts_logger< false > no_logger( stream );
+ no_logger.write( system_utilities::common::details::message_level::note, "message" );
+ const std::string stream_content = stream.str();
+ BOOST_CHECK_EQUAL( "", stream_content );
+ }
+ {
+ std::stringstream stream;
+ ts_logger<> log( stream );
+ log.write( system_utilities::common::details::message_level::warn, "message" );
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:[A-Z ]{7}\\]\\: message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ {
+ std::stringstream stream;
+ ts_logger< true, false > log( stream );
+ log.write( system_utilities::common::details::message_level::note, "message" );
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:[A-Z ]{7}\\]\\: message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ {
+ std::stringstream stream;
+ ts_logger< true, false, false > log( stream );
+ log.write( system_utilities::common::details::message_level::note, "message" );
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ }
+ void ts_logger_flush_tests()
+ {
+ std::stringstream it_is_just_a_test_on_existing_flush_method;
+ ts_logger<> l( it_is_just_a_test_on_existing_flush_method );
+ BOOST_CHECK_NO_THROW( l.flush() );
+ }
+ void ts_logger_note_tests()
+ {
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.note( "note message" );
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:NOTE \\]\\: note message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.note() << "note message";
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:NOTE \\]\\: note message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ }
+ void ts_logger_warn_tests()
+ {
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.warn( "not important message" );
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:WARNING\\]\\: not important message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.warn() << "not important message";
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:WARNING\\]\\: not important message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ }
+ void ts_logger_error_tests()
+ {
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.error( "error message" );
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:ERROR \\]\\: error message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.error() << "error message";
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:ERROR \\]\\: error message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ }
+ void ts_logger_debug_tests()
+ {
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.debug( "debug message" );
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:DEBUG \\]\\: debug message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.debug() << "debug message";
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:DEBUG \\]\\: debug message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ }
+ void ts_logger_fatal_tests()
+ {
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.fatal( "fatal message" );
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:FATAL \\]\\: fatal message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ {
+ std::stringstream stream;
+ ts_logger<> l( stream );
+ l.fatal() << "fatal message";
+ const std::string stream_content = stream.str();
+ boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:FATAL \\]\\: fatal message\n" );
+ BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true );
+ }
+ }
+ //
+ void ts_logger_write_performance_tests()
+ {
+ details::ts_logger_write_performance_test_helper< true, true, true >( 1750, 1450 );
+ details::ts_logger_write_performance_test_helper< false, true, true >( 350, 50 );
+ details::ts_logger_write_performance_test_helper< true, true, false >( 400, 100 );
+ details::ts_logger_write_performance_test_helper< true, false, false >( 400, 100 );
+ details::ts_logger_write_performance_test_helper< true, false, true >( 1750, 1450 );
+ }
+ }
+ }
+}

0 comments on commit 0b54e9a

Please sign in to comment.
Something went wrong with that request. Please try again.