Initial load of driver
r1083 | dcl | 2006-08-25 14:46:51 +1000 (Fri, 25 Aug 2006) | 2 lines
This commit is contained in:
129
site_ansto/hardsup/Monitor/utility.c
Normal file
129
site_ansto/hardsup/Monitor/utility.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Utility functions and structures
|
||||
*/
|
||||
#include "utility.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define NUM_TIMESTAMPS 20
|
||||
#define SZ_TIMESTAMP 40
|
||||
|
||||
static char ts_array[NUM_TIMESTAMPS][SZ_TIMESTAMP];
|
||||
static int ts_index = -1;
|
||||
|
||||
char* make_timestamp(const struct timeval* tv)
|
||||
{
|
||||
char *str;
|
||||
if (ts_index < 0 || ts_index >= NUM_TIMESTAMPS)
|
||||
ts_index = 0;
|
||||
str = ts_array[ts_index++];
|
||||
struct timeval now;
|
||||
if (tv == NULL)
|
||||
{
|
||||
tv = &now;
|
||||
gettimeofday(&now, NULL);
|
||||
}
|
||||
snprintf(str, SZ_TIMESTAMP, "%02d:%02d:%02d.%06d",
|
||||
(int) (tv->tv_sec / 3600 % 24),
|
||||
(int) (tv->tv_sec / 60 % 60),
|
||||
(int) (tv->tv_sec % 60),
|
||||
(int) (tv->tv_usec));
|
||||
return str;
|
||||
}
|
||||
|
||||
double time_diff(struct timeval* later, struct timeval* earlier)
|
||||
{
|
||||
double delta =
|
||||
((double) later->tv_sec - (double) earlier->tv_sec)
|
||||
+ 0.000001 * ((double) later->tv_usec - (double) earlier->tv_usec);
|
||||
return delta;
|
||||
}
|
||||
|
||||
/*
|
||||
* subtract the earlier time value from the later time value
|
||||
*/
|
||||
int time_sub(struct timeval* later, struct timeval* earlier)
|
||||
{
|
||||
if (later->tv_sec < earlier->tv_sec ||
|
||||
(later->tv_sec == earlier->tv_sec &&
|
||||
later->tv_usec < earlier->tv_usec))
|
||||
return -1;
|
||||
later->tv_sec -= earlier->tv_sec;
|
||||
if (later->tv_usec < earlier->tv_usec)
|
||||
{
|
||||
later->tv_sec--;
|
||||
later->tv_usec = 1000000 + later->tv_usec - earlier->tv_usec;
|
||||
}
|
||||
else
|
||||
{
|
||||
later->tv_usec -= earlier->tv_usec;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int time_cmp(struct timeval* later, struct timeval* earlier)
|
||||
{
|
||||
double delta = (1.0 * later->tv_sec + 0.000001 * later->tv_usec) -
|
||||
(1.0 * earlier->tv_sec + 0.000001 * earlier->tv_usec);
|
||||
if (delta == 0.0)
|
||||
return 0;
|
||||
else if (delta > 0.0)
|
||||
return 1;
|
||||
else return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Advance the timer by <msec> milliseconds until it is in the future
|
||||
*/
|
||||
void time_adv(struct timeval* tmr, int msec)
|
||||
{
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
while (tmr->tv_sec < now.tv_sec ||
|
||||
(tmr->tv_sec == now.tv_sec &&
|
||||
tmr->tv_usec <= now.tv_usec))
|
||||
{
|
||||
tmr->tv_sec += msec / 1000;
|
||||
tmr->tv_usec += (msec % 1000) * 1000;
|
||||
if (tmr->tv_usec >= 1000000)
|
||||
{
|
||||
tmr->tv_sec += tmr->tv_usec / 1000000;
|
||||
tmr->tv_usec = tmr->tv_usec % 1000000;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define DEBUG_FILE "debug_file.txt"
|
||||
#include <stdarg.h>
|
||||
#ifndef dprintf
|
||||
static int debug_level = -1;
|
||||
int set_debug_level(int new_level)
|
||||
{
|
||||
int temp = debug_level;
|
||||
debug_level = new_level;
|
||||
return temp;
|
||||
}
|
||||
|
||||
int dprintf(int level, const char* format, ...)
|
||||
{
|
||||
int result = 0;
|
||||
int iRet = 0;
|
||||
va_list ap;
|
||||
|
||||
if (level <= debug_level)
|
||||
{
|
||||
FILE* debug_file = fopen(DEBUG_FILE, "a");
|
||||
if (debug_file)
|
||||
{
|
||||
va_start(ap, format);
|
||||
iRet = fprintf(debug_file, "%s ", make_timestamp(NULL));
|
||||
result = iRet;
|
||||
iRet = vfprintf(debug_file, format, ap);
|
||||
result += iRet;
|
||||
va_end(ap);
|
||||
fclose(debug_file);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user