home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1995 March / SOFM_Mar1995.bin / pc / utility / os2 / clock / debug.cpp < prev    next >
Text File  |  1995-01-27  |  8KB  |  223 lines

  1. /****************************************************************** DEBUG.CPP
  2.  *                                                                          *
  3.  *  Debugging Aids                                                          *
  4.  *                                                                          *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_BASE
  8. #define INCL_WIN
  9. #include <os2.h>
  10.  
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. #include "debug.h"
  18. #include "mutex.h"
  19.  
  20. extern HFILE Timer = 0 ;
  21. extern BOOL Trace = FALSE ;
  22.  
  23.  
  24. /****************************************************************************
  25.  *                                                                          *
  26.  *                       Display Debug Message                              *
  27.  *                                                                          *
  28.  ****************************************************************************/
  29.  
  30. extern VOID Debug ( HWND hwnd, char *Message, ... )
  31. {
  32.  /***************************************************************************
  33.   * Local Declarations                                                      *
  34.   ***************************************************************************/
  35.  
  36.   va_list Marker ;
  37.   char Text [500] ;
  38.  
  39.  /***************************************************************************
  40.   * Format the debug message.                                               *
  41.   ***************************************************************************/
  42.  
  43.   va_start ( Marker, Message ) ;
  44.   vsprintf ( Text, Message, Marker ) ;
  45.   va_end ( Marker ) ;
  46.  
  47.  /***************************************************************************
  48.   * Display the log message and wait for the user to press ENTER.           *
  49.   ***************************************************************************/
  50.  
  51.   WinMessageBox ( HWND_DESKTOP, hwnd, PSZ(Text), PSZ("Debug"), 0, MB_ENTER ) ;
  52. }
  53.  
  54. /****************************************************************************
  55.  *                                                                          *
  56.  *                         Log Debug Message                                *
  57.  *                                                                          *
  58.  ****************************************************************************/
  59.  
  60. Mutex LogSemaphore ( PSZ(NULL) ) ;
  61.  
  62. extern VOID Log ( char *Message, ... )
  63. {
  64.  /***************************************************************************
  65.   * Serialize this function by requesting its semaphore.                    *
  66.   ***************************************************************************/
  67.  
  68.   LogSemaphore.Request ( SEM_INDEFINITE_WAIT ) ;
  69.  
  70.  /***************************************************************************
  71.   * Format the message.                                                     *
  72.   ***************************************************************************/
  73.  
  74.   va_list Marker ;
  75.   va_start ( Marker, Message ) ;
  76.  
  77.   static char Buffer [1024] ;
  78.   vsprintf ( Buffer, Message, Marker ) ;
  79.  
  80.   va_end ( Marker ) ;
  81.  
  82.  /***************************************************************************
  83.   * Open the log file.                                                      *
  84.   ***************************************************************************/
  85.  
  86.   FILE *File = fopen ( "CLOCK.LOG", "a" ) ;
  87.  
  88.  /***************************************************************************
  89.   * If the file got opened, write the message to the log file and close it. *
  90.   ***************************************************************************/
  91.  
  92.   if ( File ) {
  93.     char Time [9], Date [9] ;
  94.     fprintf ( File, "%s %s %s\n", _strtime(Time), _strdate(Date), Buffer ) ;
  95.     fclose ( File ) ;
  96.   } /* endif */
  97.  
  98.  /***************************************************************************
  99.   * Release the function semaphore and return.                              *
  100.   ***************************************************************************/
  101.  
  102.   LogSemaphore.Release ( ) ;
  103. }
  104.  
  105. /****************************************************************************
  106.  *                                                                          *
  107.  *                          Open Timer for Use                              *
  108.  *                                                                          *
  109.  ****************************************************************************/
  110.  
  111. extern BOOL OpenTimer ( VOID )
  112. {
  113.   if ( Timer )
  114.     DosClose ( Timer ) ;
  115.  
  116.   ULONG Action ;
  117.   if ( DosOpen ( (PSZ)"TIMER$", &Timer, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) )
  118.   {
  119.     return ( FALSE ) ;
  120.   }
  121.  
  122.   return ( TRUE ) ;
  123. }
  124.  
  125. /****************************************************************************
  126.  *                                                                          *
  127.  *                              Close Timer                                 *
  128.  *                                                                          *
  129.  ****************************************************************************/
  130.  
  131. extern VOID CloseTimer ( VOID )
  132. {
  133.   DosClose ( Timer ) ;
  134. }
  135.  
  136. /****************************************************************************
  137.  *                                                                          *
  138.  *                       Read Time from HRTIMER.SYS                         *
  139.  *                                                                          *
  140.  ****************************************************************************/
  141.  
  142. extern BOOL GetTime ( PTIMESTAMP pts )
  143. {
  144.   ULONG ByteCount ;
  145.  
  146.   if ( DosRead ( Timer, pts, sizeof(*pts), &ByteCount ) )
  147.     return ( FALSE ) ;
  148.  
  149.   return ( TRUE ) ;
  150. }
  151.  
  152. /****************************************************************************
  153.  *                                                                          *
  154.  *                         Calculate Elapsed Time                           *
  155.  *                                                                          *
  156.  ****************************************************************************/
  157.  
  158. extern ULONG ComputeElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs )
  159. {
  160.   ULONG ulMsecs, ulNsecs;
  161.   TIMESTAMP tsStart, tsStop ;
  162.  
  163.   tsStart = *ptsStart ;                       // De-reference timestamp
  164.                                               //     structures for speed
  165.   tsStop  = *ptsStop ;
  166.  
  167.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  168.  
  169.   if( tsStart.ulNs > tsStop.ulNs )            // If nanosecond overflow ...
  170.   {
  171.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  172.     ulMsecs--;                                        // Adjust milliseconds
  173.   }
  174.   else
  175.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  176.  
  177.   *pulNs = ulNsecs ;
  178.  
  179.   return ( ulMsecs ) ;
  180. }
  181.  
  182. /****************************************************************************
  183.  *                                                                          *
  184.  *  Allocate Memory                                                         *
  185.  *                                                                          *
  186.  ****************************************************************************/
  187.  
  188. //#define ALLOCATE_THROUGH_DOS
  189.  
  190. extern PVOID AllocateMemory ( ULONG ByteCount )
  191. {
  192.   #ifdef ALLOCATE_THROUGH_DOS
  193.   {
  194.     PVOID Memory ;
  195.     DosAllocMem ( &Memory, ByteCount, PAG_READ | PAG_WRITE | PAG_COMMIT ) ;
  196.     return ( Memory ) ;
  197.   }
  198.   #else
  199.   {
  200.     return ( malloc ( ByteCount ) ) ;
  201.   }
  202.   #endif
  203. }
  204.  
  205. /****************************************************************************
  206.  *                                                                          *
  207.  *  Free Memory                                                             *
  208.  *                                                                          *
  209.  ****************************************************************************/
  210.  
  211. extern VOID FreeMemory ( PVOID Memory )
  212. {
  213.   #ifdef ALLOCATE_THROUGH_DOS
  214.   {
  215.     DosFreeMem ( Memory ) ;
  216.   }
  217.   #else
  218.   {
  219.     free ( Memory ) ;
  220.   }
  221.   #endif
  222. }
  223.