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

Debug.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta 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 GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Sat May 28 18:07:23 EDT 2005 by ken@xorian.net
00020 //      modified on Wed May  6 17:44:43 PDT 1998 by heydon
00021 
00022 #include <stdio.h>
00023 #include <Basics.H>
00024 #include "Debug.H"
00025 #include <sys/time.h>
00026 #if defined (__linux__)
00027 #include <unistd.h>
00028 #endif 
00029 
00030 // fatal programmer error
00031 class FatalError {
00032   public: FatalError() { /*EMPTY*/ }
00033 };
00034 
00035 // global variables
00036 Basics::mutex Debug_mu;
00037 const int Debug_TimeStampLen = 28; // "hh:mm:ss.mmm MM/DD/YYYY -- "
00038 char Debug_TimeStampBuff[Debug_TimeStampLen];
00039 
00040 void Debug::Lock() throw () { Debug_mu.lock(); }
00041 void Debug::Unlock() throw () { Debug_mu.unlock(); }
00042 
00043 char *Debug::Timestamp() throw ()
00044 /* Requires "Debug_mu" to be held and for the results to be unused after
00045    Debug_mu is unlocked. */
00046 {
00047     struct timeval tv;
00048     struct timezone tz;
00049     struct tm *tm;
00050     if (gettimeofday(&tv, &tz) < 0) {
00051         throw FatalError();
00052     }
00053     time_t secs = tv.tv_sec;
00054     if((tm = localtime(&secs)) == NULL) {
00055         throw FatalError();
00056     }
00057     sprintf(Debug_TimeStampBuff, "%02d:%02d:%02d.%03d %02d/%02d/%04d -- ",
00058       tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec/1000,
00059       tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
00060     return Debug_TimeStampBuff;
00061 }
00062 
00063 int Debug::MyRand(int lower, int upper) throw ()
00064 {
00065     int res;
00066     res = lower + (rand() % (upper-lower+1));
00067     return res;
00068 }
00069 
00070 void Debug::BlockForever() throw ()
00071 {
00072     Basics::mutex mu0;
00073     Basics::cond cond0;
00074     mu0.lock();
00075     cond0.wait(mu0);
00076 }

Generated on Mon May 8 00:48:31 2006 for Vesta by  doxygen 1.4.2