/** \file Utils.cc 

   Common utilities originally for Benthic Imaging AUV.

   \author k. headley
   copyright MBARI 2008 
*/
/****************************************************************************/
/* Copyright (c) 2008 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : Utils.cc                                                      */
/* Author   : K Headley                                                     */
/* Project  : Benthic Imaging AUV                                           */
/* Created  : 04/01/2008                                                    */
/****************************************************************************/
/* Modification History:                                                    */
/*  $Id: Utils.cc,v 1.2 2008/12/12 23:36:32 headley Exp $   */
/*  $Name: FastSim $ */
/****************************************************************************/

#include "Utils.h"

/**
   Print formatted debug message to stdout.
   Requires DEBUG compile time environment
   variable to be defined.

   @param fmt printf format string
   @param ... printf argument list
*/
void printdbg( char *fmt, ... ) {    
#ifdef DEBUG
  va_list args;     
  va_start( args, fmt );     
  vfprintf( stdout, fmt, args );     
  va_end( args );
#endif
}            

/**
   Delay for a specified period (in decimal sec)
   Uses nanosleep to implement the delay.

   @param delaySec Delay period length (decimal seconds)

*/
void delay( double delaySec ) {    
  struct timespec waitTime;
  unsigned long waitTimeNsec;
  time_t waitTimeSec;
  unsigned long inum=((unsigned long)delaySec);
  double frac=delaySec-inum;
  if(delaySec<=0.000000000){
    return;
  }
  waitTimeSec=(time_t)(inum);
  waitTimeNsec=(unsigned long)(frac*1000000000);
  waitTime.tv_sec=waitTimeSec;
  waitTime.tv_nsec=waitTimeNsec;
  nanosleep(&waitTime,NULL);
}            
