Changed geometry to RLog-based logging and counters ar
authorDmitriy Morozov <morozov@cs.duke.edu>
Tue, 19 Feb 2008 09:10:51 -0500
branchar
changeset 62 0e7554fd0d51
parent 61 f905b57dd7ab
child 63 ef9470ed5441
Changed geometry to RLog-based logging and counters
include/geometry/kinetic-sort.hpp
include/geometry/simulator.h
include/geometry/simulator.hpp
include/utilities/eventqueue.h
include/utilities/log.h
tests/geometry/CMakeLists.txt
tests/geometry/polynomial.cpp
tests/geometry/test-kinetic-sort.cpp
--- a/include/geometry/kinetic-sort.hpp	Tue Feb 19 07:26:01 2008 -0500
+++ b/include/geometry/kinetic-sort.hpp	Tue Feb 19 09:10:51 2008 -0500
@@ -1,3 +1,19 @@
+#include "utilities/log.h"
+#include "utilities/counter.h"
+
+#ifdef LOGGING
+static rlog::RLogChannel* rlKineticSort =           DEF_CHANNEL("geometry/kinetic-sort", rlog::Log_Debug);
+static rlog::RLogChannel* rlKineticSortAudit =      DEF_CHANNEL("geometry/kinetic-sort/audit", rlog::Log_Debug);
+static rlog::RLogChannel* rlKineticSortSchedule =   DEF_CHANNEL("geometry/kinetic-sort/schedule", rlog::Log_Debug);
+static rlog::RLogChannel* rlKineticSortProcess =    DEF_CHANNEL("geometry/kinetic-sort/process", rlog::Log_Debug);
+#endif // LOGGING
+
+#ifdef COUNTERS
+static Counter*  cKineticSort =                     GetCounter("kinetic-sort");
+static Counter*  cKineticSortSwap =                 GetCounter("kinetic-sort/swap");
+#endif // COUNTERS
+
+
 template<class ElementIterator_, class TrajectoryExtractor_, class Simulator_, class Swap_>
 KineticSort<ElementIterator_, TrajectoryExtractor_, Simulator_, Swap_>::
 KineticSort()
@@ -81,6 +97,7 @@
 {
 	// TODO: AssertMsg(boost::next(pos) != list_.end(), "Cannot swap the last element");
 
+    Count(cKineticSortSwap);
 	swap_(pos->element, simulator);
 
 	// Remove events
@@ -110,7 +127,7 @@
 	typedef 		typename Simulator::Time					Time;
 	
 	Time t = simulator->audit_time();
-	std::cout << "Auditing at " << t << std::endl;
+	rLog(rlKineticSortAudit, "Auditing at %s", tostring(t).c_str());
 
 	TrajectoryExtractor	te;
 	
@@ -119,22 +136,25 @@
 	RationalFunction cur_trajectory = te(cur->element);
 	while (next != list_.end())
 	{
-		(*(cur->swap_event_key))->print(std::cout << "  ") << std::endl;
+		rLog(rlKineticSortAudit, "  %s", intostring(**(cur->swap_event_key)).c_str());
 
 		RationalFunction next_trajectory = te(next->element);
-		std::cout << "  Auditing:   " << cur_trajectory << ", " << next_trajectory << std::endl;
-		std::cout << "  Difference: " << next_trajectory - cur_trajectory << std::endl;
-		std::cout << "  Sign at:    " << t << ", " << PolynomialKernel::sign_at(next_trajectory - cur_trajectory, t) << std::endl;
+		rLog(rlKineticSortAudit, "  Auditing:   %s, %s", tostring(cur_trajectory).c_str(),
+                                                         tostring(next_trajectory).c_str());
+		rLog(rlKineticSortAudit, "  Difference: %s", tostring(next_trajectory - cur_trajectory).c_str());
+		rLog(rlKineticSortAudit, "  Sign at:    %s, %s", tostring(t).c_str(),
+                                                         tostring(PolynomialKernel::sign_at(next_trajectory - cur_trajectory, t)).c_str());
 		if (PolynomialKernel::sign_at(next_trajectory - cur_trajectory, t) == -1)
 		{
-			std::cout << "Audit failed at " << *cur->element << ", " << *next->element << std::endl;
+			rError("Audit failed at %s, %s", tostring(*cur->element).c_str(), 
+                                             tostring(*next->element).c_str());
 			return false;
 		}
 
 		cur_trajectory = next_trajectory;
 		cur = next++;
 	}
-	if (cur != list_.end()) (*(cur->swap_event_key))->print(std::cout << "  ") << std::endl;
+	if (cur != list_.end()) rLog(rlKineticSortAudit, "  %s", intostring(**(cur->swap_event_key)).c_str());
 	return true;
 }
 
@@ -154,7 +174,7 @@
 	while (next != e)
 	{
 		RationalFunction next_trajectory = te(next->element);
-		std::cout << "Next trajectory: " << next_trajectory << std::endl;
+		rLog(rlKineticSortSchedule, "Next trajectory: %s", tostring(next_trajectory).c_str());
 		// TODO: add assertion that (next_trajectory - cur_trajectory)(s->curren_time()) > 0
 		cur->swap_event_key = simulator->add(next_trajectory - cur_trajectory, SwapEvent(this, cur));
 		cur = next++;
@@ -205,7 +225,7 @@
 		virtual bool				process(Simulator* s) const;
 		void						set_position(iterator i)					{ pos_ = i; }
 		iterator					position() const							{ return pos_; }
-		std::ostream&				print(std::ostream& out) const;
+		std::ostream&				operator<<(std::ostream& out) const;
 
 	private:
 		KineticSort*				sort_;
@@ -217,7 +237,7 @@
 KineticSort<ElementIterator_, TrajectoryExtractor_, Simulator_, Swap_>::SwapEvent::
 process(Simulator* s) const
 { 
-	std::cout << "Swapping. Current time: " << s->current_time() << std::endl;
+	rLog(rlKineticSortProcess, "Swapping. Current time: %s", tostring(s->current_time()).c_str());
 	sort_->swap(pos_, s); 
 	return true; 
 }
@@ -225,9 +245,8 @@
 template<class ElementIterator_, class TrajectoryExtractor_, class Simulator_, class Swap_>
 std::ostream&				
 KineticSort<ElementIterator_, TrajectoryExtractor_, Simulator_, Swap_>::SwapEvent::
-print(std::ostream& out) const
+operator<<(std::ostream& out) const
 {
-	Parent::print(out) << ", SwapEvent at " << TrajectoryExtractor_()(position()->element);
+	Parent::operator<<(out) << ", SwapEvent at " << TrajectoryExtractor_()(position()->element);
 	return out;
 }
-
--- a/include/geometry/simulator.h	Tue Feb 19 07:26:01 2008 -0500
+++ b/include/geometry/simulator.h	Tue Feb 19 09:10:51 2008 -0500
@@ -53,7 +53,7 @@
 		Time						audit_time() const;
 		bool						reached_infinity() const					{ return reached_infinity_; }
 
-		std::ostream&				print(std::ostream& out) const;
+		std::ostream&				operator<<(std::ostream& out) const;
 
 	private:
 		void						update(Key i);
@@ -94,7 +94,7 @@
 				return root_stack().top() < e.root_stack().top();
 		}
 
-		virtual std::ostream&		print(std::ostream& out) const				{ return out << "Event with " << root_stack_.size() << " roots"; }
+		virtual std::ostream&		operator<<(std::ostream& out) const				{ return out << "Event with " << root_stack_.size() << " roots"; }
 
 	private:
 		RootStack					root_stack_;
--- a/include/geometry/simulator.hpp	Tue Feb 19 07:26:01 2008 -0500
+++ b/include/geometry/simulator.hpp	Tue Feb 19 09:10:51 2008 -0500
@@ -1,3 +1,16 @@
+#include "utilities/log.h"
+#include "utilities/counter.h"
+
+#ifdef LOGGING
+static rlog::RLogChannel* rlSimulator =             DEF_CHANNEL("geometry/simulator", rlog::Log_Debug);
+
+#endif // LOGGING
+
+#ifdef COUNTERS
+static Counter*  cSimulatorProcess =                GetCounter("simulator/process");
+#endif // COUNTERS
+
+
 template<class PolyKernel_, template<class Event> class EventComparison_>
 template<class Event_>
 typename Simulator<PolyKernel_, EventComparison_>::Key
@@ -15,7 +28,7 @@
 add(const RationalFunction& f, const Event_& e)
 {
 	Event* ee = new Event_(e);
-	//std::cout << "Solving: " << f << std::endl;
+	rLog(rlSimulator, "Solving: %s", tostring(f).c_str());
 	PolynomialKernel::solve(f, ee->root_stack());
 	bool sign = PolynomialKernel::sign_at_negative_infinity(f);
 	while (!ee->root_stack().empty() && ee->root_stack().top() < current_time())
@@ -24,7 +37,7 @@
 		sign = !sign;
 	}
 	if (sign) ee->root_stack().pop();			// TODO: double-check the logic
-	//std::cout << "Pushing: " << ee->root_stack().top() << std::endl;
+	rLog(rlSimulator, "Pushing: %s", tostring(ee->root_stack().top()).c_str());
 	return queue_.push(ee);
 }
 		
@@ -46,7 +59,8 @@
 Simulator<PolyKernel_, EventComparison_>::
 process()
 {
-	std::cout << "Queue size: " << queue_.size() << std::endl;
+    Count(cSimulatorProcess);
+	rLog(rlSimulator, "Queue size: %i", queue_.size());
 	Key top = queue_.top();
 	Event* e = *top;
 	
@@ -80,8 +94,22 @@
 template<class PolyKernel_, template<class Event> class EventComparison_>
 std::ostream&
 Simulator<PolyKernel_, EventComparison_>::
-print(std::ostream& out) const
+operator<<(std::ostream& out) const
 {
 	out << "Simulator: " << std::endl;
 	return queue_.print(out, "  ");
 }
+
+template<class PolyKernel_, template<class Event> class EventComparison_>
+std::ostream&
+operator<<(std::ostream& out, const Simulator<PolyKernel_, EventComparison_>& s)
+{
+    return s.operator<<(out);
+}
+
+template<class PolyKernel_, template<class Event> class EventComparison_>
+std::ostream&
+operator<<(std::ostream& out, const typename Simulator<PolyKernel_, EventComparison_>::Event& e)
+{
+    return e.operator<<(out);
+}
--- a/include/utilities/eventqueue.h	Tue Feb 19 07:26:01 2008 -0500
+++ b/include/utilities/eventqueue.h	Tue Feb 19 09:10:51 2008 -0500
@@ -64,7 +64,7 @@
 print(std::ostream& out, const std::string& prefix) const
 {
 	for (typename QueueRepresentation::const_iterator cur = queue_.begin(); cur != queue_.end(); ++cur)
-		(*cur)->print(out << prefix) << std::endl;
+		(*cur)->operator<<(out << prefix) << std::endl;
 	return out;
 }
 
--- a/include/utilities/log.h	Tue Feb 19 07:26:01 2008 -0500
+++ b/include/utilities/log.h	Tue Feb 19 09:10:51 2008 -0500
@@ -12,6 +12,8 @@
 
 template<class T>
 std::string tostring(const T& t) { std::ostringstream out; out << t; return out.str(); }
+template<class T>
+std::string intostring(const T& t) { std::ostringstream out; t.operator<<(out); return out.str(); }
 
 #define AssertMsg(cond, message, ...)		do { if (!(cond)) { rError(message, ##__VA_ARGS__); rAssertSilent(cond); } } while (0)
 	
--- a/tests/geometry/CMakeLists.txt	Tue Feb 19 07:26:01 2008 -0500
+++ b/tests/geometry/CMakeLists.txt	Tue Feb 19 09:10:51 2008 -0500
@@ -7,6 +7,6 @@
 
 foreach 					(t ${targets})
 	add_executable			(${t} ${t}.cpp)
-	target_link_libraries	(${t} ${synaps_libraries})
+	target_link_libraries	(${t} ${synaps_libraries} ${libraries})
 endforeach 					(t ${targets})
 
--- a/tests/geometry/polynomial.cpp	Tue Feb 19 07:26:01 2008 -0500
+++ b/tests/geometry/polynomial.cpp	Tue Feb 19 09:10:51 2008 -0500
@@ -75,8 +75,6 @@
 		while (!roots.empty()) { std::cout << roots.top() << std::endl; roots.pop(); }
 	}
 
-	return 0;
-
 	// Edges
 	{
 		PointContainer vertices(2);
--- a/tests/geometry/test-kinetic-sort.cpp	Tue Feb 19 07:26:01 2008 -0500
+++ b/tests/geometry/test-kinetic-sort.cpp	Tue Feb 19 09:10:51 2008 -0500
@@ -6,6 +6,8 @@
 #include <boost/utility.hpp>
 #include <boost/bind.hpp>
 
+#include <utilities/log.h>
+
 typedef		double							FieldType;
 //typedef		ZZ								FieldType;
 //typedef		QQ								FieldType;
@@ -37,8 +39,15 @@
 	s->splice(i, *s, boost::next(i));
 }
 
-int main()
+int main(int argc, char** argv)
 {
+#ifdef LOGGING
+    rlog::RLogInit(argc, argv);
+
+    //stdoutLog.subscribeTo( RLOG_CHANNEL("geometry/simulator") );
+    stdoutLog.subscribeTo( RLOG_CHANNEL("geometry/kinetic-sort") );
+#endif
+
 	SimulatorFT		simulator;
 	SortDS 			list;
 
@@ -62,8 +71,8 @@
 	while(!simulator.reached_infinity() && simulator.current_time() < 4)
 	{
 		std::cout << "Current time before: " << simulator.current_time() << std::endl;
-		//if (!ks.audit(&simulator)) return 1;
-		//simulator.print(std::cout << "Auditing ");
+		if (!ks.audit(&simulator)) return 1;
+        std::cout << "Examining " << simulator;
 		simulator.process();
 		std::cout << "Current time after: " << simulator.current_time() << std::endl;
 	}