141 lines
3.1 KiB
C
141 lines
3.1 KiB
C
/*
|
|
* 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 dbg_printf
|
|
static int debug_level = -1;
|
|
static bool new_line = true;
|
|
|
|
int set_debug_level(int new_level)
|
|
{
|
|
int temp = debug_level;
|
|
debug_level = new_level;
|
|
return temp;
|
|
}
|
|
|
|
int dbg_printf(int level, const char* format, ...)
|
|
{
|
|
int result = 0;
|
|
int iRet = 0;
|
|
va_list ap;
|
|
char buffer[2048];
|
|
|
|
if (level <= debug_level)
|
|
{
|
|
FILE* debug_file = fopen(DEBUG_FILE, "a");
|
|
if (debug_file)
|
|
{
|
|
va_start(ap, format);
|
|
iRet = vsnprintf(buffer, sizeof(buffer), format, ap);
|
|
va_end(ap);
|
|
if (iRet > 0)
|
|
{
|
|
if (new_line)
|
|
result = fprintf(debug_file, "%s ", make_timestamp(NULL));
|
|
result += iRet;
|
|
fputs(buffer, debug_file);
|
|
if (buffer[iRet - 1] == '\n')
|
|
new_line = true;
|
|
else
|
|
new_line = false;
|
|
}
|
|
fclose(debug_file);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
#endif
|