Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

debug_timer.h

00001 //
00002 //  The Worldforge Project
00003 //  Copyright (C) 2004 The Worldforge Project
00004 //
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU Lesser General Public License as 
00007 //  published by the Free Software Foundation; either version 2.1 of the
00008 //  License, or (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, SA.
00018 //
00019 //  For information about Worldforge and its authors, please contact
00020 //  the Worldforge Web Site at http://www.worldforge.org.
00021 //
00022 
00023 // Written by Ron Steinke, 01/2004
00024 
00025 #ifndef WFTK_DEBUG_TIMER_H
00026 #define WFTK_DEBUG_TIMER_H
00027 
00028 #include <wftk/debug.h>
00029 #ifdef DEBUG
00030 #include <wftk/timer.h>
00031 #endif
00032 
00033 namespace wftk {
00034 
00035 class DebugTimer
00036 {
00037  public:
00038   DebugTimer(const Debug& out, unsigned threshold = 2) :
00039     out_(out), threshold_(threshold) {set();}
00040 
00041   void set()
00042   {
00043 #ifdef DEBUG
00044     if(out_)
00045       time_ = Time::now();
00046 #endif
00047   }
00048 
00049   void mark(const std::string& msg, const std::string& post = "")
00050   {
00051     unsigned passed = check();
00052     if(passed >= threshold_)
00053       out_ << msg << " took " << passed << " ms " << post << Debug::endl;
00054   }
00055 
00056   // for custom versions of mark()
00057   unsigned check()
00058   {
00059 #ifdef DEBUG
00060     if(out_) {
00061       Time next = Time::now();
00062       unsigned passed = next - time_;
00063       time_ = next;
00064       return passed;
00065     }
00066     else
00067 #endif
00068       return 0;
00069   }
00070 
00071  private:
00072 #ifdef DEBUG
00073   Time time_;
00074 #endif
00075   Debug out_;
00076   unsigned threshold_;
00077 };
00078 
00079 } // namespace
00080 
00081 #endif // WFTK_DEBUG_TIMER_H

Generated Tue Apr 12 22:48:51 2005.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.