Colors at logging. test_Common.hpp renamed to Fixtures.hpp. TimerThread remove user is suspicious

master
Denes Matetelki 14 years ago
parent 696c28a5c8
commit 083825bc2e

@ -0,0 +1,144 @@
#ifndef COLORS_HPP
#define COLRS_HPP
// http://www.linux.gr/cgi-bin/man2html?console_codes+4
/// Formatters:
/// 1 set bold
#define F_BOLD "1"
/// 2 set half-bright (simulated with color on a color display)
#define F_HALF_BRIGHT "2"
/// 4 set underscore (simulated with color on a color display)
/// (the colors used to simulate dim or underline are set using ESC ] ...)
#define F_UNDERSCORE "4"
/// 5 set blink
#define F_BLINK "5"
/// 7 set reverse video
#define F_REVERSE_COLORS "7"
/// 10 reset selected mapping, display control flag, and toggle meta flag
/// (ECMA-48 says "primary font").
#define F_PRIMARY_FONT "10"
/// 11 select null mapping, set display control flag, reset toggle meta flag
/// (ECMA-48 says "first alternate font").
#define F_FIRST_ALTERNATE_FONT "11"
/// 12 select null mapping, set display control flag, set toggle meta flag
/// (ECMA-48 says "second alternate font").
/// The toggle meta flag causes the high bit of a byte to be toggled
/// before the mapping table translation is done.
#define F_SECOND_ALTERNATE_FONT "12"
/// 21 set normal intensity (ECMA-48 says "doubly underlined")
#define F_DOUBLY_UNDERLINED "21"
/// 22 set normal intensity
#define F_NORMAL_INTENSITY "22"
/// 24 underline off
#define F_UNDERLINE_OFF "24"
/// 25 blink off
#define F_BLINK_OFF "25"
/// 27 reverse video off
#define F_REVERSE_VIDEO_OFF "27"
/// Foregrounds:
/// 30 set black foreground
#define FG_BLACK "30"
/// 31 set red foreground
#define FG_RED "31"
/// 32 set green foreground
#define FG_GREEN "32"
/// 33 set brown foreground
#define FG_BROWN "33"
/// 34 set blue foreground
#define FG_BLUE "34"
/// 35 set magenta foreground
#define FG_MAGENTA "35"
/// 36 set cyan foreground
#define FG_CYAN "36"
/// 37 set white foreground
#define FG_WHITE "37"
/// 38 set underscore on, set default foreground color
#define FG_UNDERSCORE_ON "38"
/// 39 set underscore off, set default foreground color
#define FG_UNDERSCORE_OFF "39"
/// Backgronds:
/// 40 set black background
#define BG_BLACK "40"
/// 41 set red background
#define BG_RED "41"
/// 42 set green background
#define BG_GREEN "42"
/// 43 set brown background
#define BG_BROWN "43"
/// 44 set blue background
#define BG_BLUE "44"
/// 45 set magenta background
#define BG_MAGENTA "45"
/// 46 set cyan background
#define BG_CYNA "46"
/// 47 set white background
#define BG_WHITE "47"
/// 49 set default background color
#define BG_DEFAULT "49"
#ifndef NOCOLOR
#define COLOR(x) COLOR_FG(x)
#define COLOR_FG(x) "\33[0;" x "m"
#define COLOR_BG(x) "\33[0;" FG_UNDERSCORE_OFF ";" x "m"
#define COLOR_FG_BG(x,y) "\33[0;" x ";" y "m"
#define COLOR_F(x) "\33[" x ";" FG_UNDERSCORE_OFF "m"
#define COLOR_F_FG(x,y) "\33[" x ";" y ";" BG_DEFAULT "m"
#define COLOR_F_FG_BG(x,y,z) "\33[" x ";" y ";" z "m"
#define COLOR_RESET "\33[0m"
#else
#define COLOR(x) ""
#define COLOR_FG(x) ""
#define COLOR_BG(x) ""
#define COLOR_FG_BG(x,y) ""
#define COLOR_F(x) ""
#define COLOR_F_FG(x,y) ""
#define COLOR_F_FG_BG(x,y,z) ""
#define COLOR_RESET ""
#endif
// #define REMOVE_PREV_LINE "\b"
#endif // COLORS_HPP

@ -39,22 +39,11 @@ inline const char* extractFilename( const char *path )
}
class BadConversion : public std::runtime_error
{
public:
BadConversion(std::string const& s) : std::runtime_error(s) { }
};
template<typename T>
inline std::string stringify(T const& x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion(std::string("stringify(") + typeid(x).name() + ")");
o << x;
return o.str();
}

@ -70,6 +70,13 @@ Logger::getInstance()->log_pointer( \
this, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
else (void)0
#define TRACE_STATIC \
if(MAX_LOFLEVEL >= Logger::FINEST && \
Logger::getInstance()->getLoglevel() >= Logger::FINEST ) \
Logger::getInstance()->log_pointer( \
0, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
else (void)0
#define LOG(level, msg) \
if (MAX_LOFLEVEL >= level && \

@ -1,6 +1,7 @@
#include "Logger.hpp"
#include <time.h> //time
#include "Colors.hpp"
void Logger::init(std::ostream& log_stream )
@ -22,11 +23,12 @@ void Logger::log_pointer( const void* msg,
const char* function)
{
*m_ostream << getTime() << " "
<< extractFilename(file) << ":"
<< line << " "
<< function << " "
<< "\"" << msg << "\""
<< std::endl;
<< COLOR( FG_GREEN ) << extractFilename(file)
<< COLOR_RESET << ":"
<< COLOR( FG_BROWN ) << line << COLOR_RESET << " "
<< COLOR( FG_CYAN ) << function << COLOR_RESET << " "
<< COLOR( FG_BLUE ) << "\"" << msg << "\""
<< COLOR_RESET << std::endl;
}
@ -36,16 +38,17 @@ void Logger::log_string( const char* msg,
const char* function)
{
*m_ostream << getTime() << " "
<< extractFilename(file) << ":"
<< line << " "
<< function << " "
<< "\"" << msg << "\""
<< std::endl;
<< COLOR( FG_GREEN ) << extractFilename(file)
<< COLOR_RESET << ":"
<< COLOR( FG_BROWN ) << line << COLOR_RESET << " "
<< COLOR( FG_CYAN ) << function << COLOR_RESET << " "
<< COLOR_F_FG( F_BOLD, FG_BROWN ) << "\"" << msg << "\""
<< COLOR_RESET << std::endl;
}
void Logger::msg(const char* text)
{
*m_ostream << text << std::endl;
*m_ostream << COLOR_F( F_BOLD) << text << COLOR_RESET << std::endl;
}

@ -33,13 +33,10 @@ void Thread::start()
sched_param param;
pthread_attr_init(&attr);
if ( pthread_attr_setschedpolicy( &attr, SCHED_RR ) != 0 ) {
throw std::runtime_error( "Coudn't set thread scheduler." );
}
pthread_attr_setschedpolicy( &attr, SCHED_RR );
param.sched_priority = 50;
if ( pthread_attr_setschedparam( &attr, &param ) != 0 ) {
throw std::runtime_error( "Coudn't set thread priority.");
}
pthread_attr_setschedparam( &attr, &param );
pthread_create( &m_threadHandler, &attr, threadStarter, ( void* )this );
} else {
@ -73,6 +70,6 @@ void Thread::sendSignal( const int nSignal ) const
void* Thread::threadStarter( void* pData )
{
LOG(Logger::FINEST, "static");
TRACE_STATIC;
return static_cast<Thread *>(pData)->run();
}

@ -67,7 +67,7 @@ bool TimerThread::removeTimerUser ( void* timerUser )
if ( (void*)(it->second.user) == (void*)timerUser ) {
m_users.erase(tmp);
m_condVar.signal();
found = true; // one usercan be registered multiple times
found = true; // one user can be registered multiple times
}
}
return found;

@ -10,8 +10,7 @@ if(CXXTEST_FOUND)
enable_testing()
CXXTEST_ADD_TEST(test
generated_main.cpp
test_Common.hpp
Fixture.hpp
test_Singelton.hpp
test_Mutex.hpp
@ -21,6 +20,7 @@ if(CXXTEST_FOUND)
test_ThreadPool.hpp
test_Semaphore.hpp
test_TimerThread.hpp
test_Common.hpp
)
target_link_libraries(test CppUtils gcov)
endif()

@ -0,0 +1,35 @@
#ifndef FIXTURE_HPP
#define FIXTURE_HPP
#include <cxxtest/TestSuite.h>
#include <cxxtest/GlobalFixture.h>
#include "Common.hpp"
#include <iostream>
class TestFixture : public CxxTest::GlobalFixture
{
bool setUpWorld()
{
Logger::createInstance();
Logger::init(std::cout);
Logger::setLogLevel(Logger::FINEST);
return true;
}
bool tearDownWorld()
{
Logger::destroy();
return true;
}
};
static TestFixture testCommon;
/// @todo remove the dot at prev line from cxxtest
#define TEST_HEADER \
MSG( std::string("\n+++ ").append(__PRETTY_FUNCTION__).append(" +++\n").c_str());
#endif // FIXTURE_HPP

@ -1,33 +1,23 @@
#ifndef TEST_COMMON_HPP
#define TEST_COMMON_HPP
#include <cxxtest/TestSuite.h>
#include <cxxtest/GlobalFixture.h>
#include "Fixture.hpp"
#include "Common.hpp"
#include <iostream>
class TestCommon : public CxxTest::GlobalFixture
{
bool setUpWorld()
{
Logger::createInstance();
Logger::init(std::cout);
return true;
}
#include <time.h> // timespec
bool tearDownWorld()
{
Logger::destroy();
return true;
}
class TestCommon : public CxxTest::TestSuite
{
};
static TestCommon testCommon;
public:
#define TEST_HEADER \
MSG( std::string("\n+++ ").append(__PRETTY_FUNCTION__).append(" +++\n").c_str());
void testCoverageIncrease( void )
{
// coverage increase of addTotimespec
long int sec = 0;
long int nsec = 1000000000 + 1;
timespec ts = addTotimespec(sec, nsec);
}
#endif // TEST_COMMON_HPP
};

@ -1,7 +1,7 @@
#include <cxxtest/TestSuite.h>
#include "Common.hpp"
#include "test_Common.hpp"
#include "Fixture.hpp"
#include "Mutex.hpp"
#include <errno.h> // EDEADLK, EPERM, ETIMEDOUT

@ -3,7 +3,7 @@
#define private public // need to reach private variables
#include "Common.hpp"
#include "test_Common.hpp"
#include "Fixture.hpp"
#include "Mutex.hpp"
class TestPThreadWrappers : public CxxTest::TestSuite

@ -1,7 +1,7 @@
#include <cxxtest/TestSuite.h>
#include "ScopedLock.hpp"
#include "test_Common.hpp"
#include "Fixture.hpp"
class TestScopedLock : public CxxTest::TestSuite

@ -3,7 +3,7 @@
#include "Semaphore.hpp"
#include "Thread.hpp"
#include "Common.hpp"
#include "test_Common.hpp"
#include "Fixture.hpp"
class TestSemaphore : public CxxTest::TestSuite
{

@ -3,7 +3,7 @@
#define private public // need to reach Singleton's private m_instance
#include "Common.hpp"
#include "test_Common.hpp"
#include "Fixture.hpp"
#include "Singleton.hpp"

@ -1,6 +1,6 @@
#include <cxxtest/TestSuite.h>
#include "test_Common.hpp"
#include "Fixture.hpp"
#include "Thread.hpp"
#include "Common.hpp"

@ -6,7 +6,7 @@
#include "Thread.hpp"
#include "ThreadPool.hpp"
#include "Common.hpp"
#include "test_Common.hpp"
#include "Fixture.hpp"

@ -1,9 +1,15 @@
#include <cxxtest/TestSuite.h>
#include "test_Common.hpp"
#include "Fixture.hpp"
#define private public // reach TimerThread's private multimap
#include "TimerThread.hpp"
#include <time.h>
class TestTimerThread : public CxxTest::TestSuite
{
@ -48,6 +54,27 @@ public:
delete user;
}
void testBasicTimeSpec( void )
{
TEST_HEADER;
TimerThread* tt = new TimerThread();
tt->start();
sleep(1);
DummyTimerUser *user = new DummyTimerUser();
timespec ts = { 1, 123 };
tt->addTimerUser( user, ts );
sleep(4);
tt->stop();
sleep(1);
TS_ASSERT_EQUALS( user->m_counter, 1 );
delete tt;
delete user;
}
void testPeriodic( void )
{
TEST_HEADER;
@ -68,6 +95,34 @@ public:
delete user;
}
void testPeriodicTimeSpec( void )
{
TEST_HEADER;
TimerThread* tt = new TimerThread();
tt->start();
sleep(1);
DummyTimerUser *user = new DummyTimerUser();
timespec ts = { 2, 3 };
timespec tsperiod = { 1, 2 };
tt->addTimerUser( user, ts, tsperiod );
/// @bug What is wrong here?
// DummyTimerUser *user2 = new DummyTimerUser();
// timespec ts2 = { 0, 100000000 };
// tt->addTimerUser( user2, ts, ts2 );
sleep(6);
tt->stop();
sleep(1);
TS_ASSERT_EQUALS( user->m_counter, 104 );
// TS_ASSERT_EQUALS( user2->m_counter, 110 );
delete tt;
delete user;
}
void testDestroyed( void )
{
TEST_HEADER;
@ -111,4 +166,40 @@ public:
delete user;
}
/// @bug this case does not work, investigate!
void bugtestRemovedMultiple( void )
{
TEST_HEADER;
TimerThread* tt = new TimerThread();
tt->start();
sleep(1);
DummyTimerUser *user = new DummyTimerUser();
tt->addTimerUser( user, 10 );
DummyTimerUser *user2 = new DummyTimerUser();
tt->addTimerUser( user2, 12 );
DummyTimerUser *user3 = new DummyTimerUser();
tt->addTimerUser( user3, 13 );
DummyTimerUser *user4 = new DummyTimerUser();
tt->addTimerUser( user4, 14 );
TS_ASSERT_EQUALS( tt->m_users.size(), 4);
sleep(2);
tt->removeTimerUser( user );
TS_ASSERT_EQUALS( tt->m_users.size(), 0);
sleep(1);
tt->stop();
sleep(1);
delete tt;
delete user;
delete user2;
delete user3;
delete user4;
}
};

Loading…
Cancel
Save