1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-11-14 20:36:03 +01:00
cdf2018-principal/chef/src/debug.c

146 lines
3.2 KiB
C
Raw Normal View History

2018-05-01 13:34:05 +02:00
#include <dirent.h>
2018-04-04 16:17:13 +02:00
#include <fcntl.h>
2018-04-29 09:38:49 +02:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
2018-04-04 16:17:13 +02:00
#include <string.h>
2018-04-29 09:38:49 +02:00
#include <time.h>
#include <unistd.h> // sleep
2018-05-01 13:34:05 +02:00
#include <wiringPi.h>
2018-04-04 16:17:13 +02:00
2018-04-29 09:38:49 +02:00
#include "debug.h"
// Variables globales
pthread_t tDebug;
struct debugArg* listeDebugArgs = NULL;
2018-04-04 16:17:13 +02:00
2018-04-29 09:38:49 +02:00
FILE* debugFd;
2018-05-01 13:34:05 +02:00
int nextId()
{
DIR* d;
struct dirent* dir;
d = opendir("log");
int maxId = 0, id, ret;
if (d) {
while ((dir = readdir(d)) != NULL) {
ret = sscanf(dir->d_name, "%d", &id);
if (ret == 1 && id > maxId) {
maxId = id;
}
}
closedir(d);
}
return maxId + 1;
}
struct timespec debugStart;
struct timespec debugNow;
struct timespec debugEcoule;
void* TaskDebug(void* pdata)
{
(void)pdata;
2018-04-29 09:38:49 +02:00
2018-05-01 13:34:05 +02:00
if (DEBUG_INTERVAL <= 0) {
return NULL;
}
clock_gettime(CLOCK_REALTIME, &debugStart);
2018-04-04 16:17:13 +02:00
2018-04-29 09:38:49 +02:00
fprintf(debugFd, "\n");
for (;;) {
2018-05-01 13:34:05 +02:00
clock_gettime(CLOCK_REALTIME, &debugNow);
if ((debugNow.tv_nsec - debugStart.tv_nsec) < 0) {
debugEcoule.tv_sec = debugNow.tv_sec - debugStart.tv_sec - 1;
debugEcoule.tv_nsec = debugNow.tv_nsec - debugStart.tv_nsec + 1000000000UL;
} else {
debugEcoule.tv_sec = debugNow.tv_sec - debugStart.tv_sec;
debugEcoule.tv_nsec = debugNow.tv_nsec - debugStart.tv_nsec;
}
fprintf(debugFd, "%ld.%09ld", debugEcoule.tv_sec, debugEcoule.tv_nsec);
2018-04-29 09:38:49 +02:00
struct debugArg* arg = listeDebugArgs;
while (arg != NULL) {
switch (arg->type) {
case d:
fprintf(debugFd, ",%d", *((int*)arg->var));
break;
case ld:
fprintf(debugFd, ",%ld", *((long int*)arg->var));
break;
case f:
fprintf(debugFd, ",%f", *((float*)arg->var));
break;
case lf:
fprintf(debugFd, ",%f", *((double*)arg->var));
break;
case s:
fprintf(debugFd, ",%s", *((char**)arg->var));
break;
default:
fprintf(debugFd, ",?");
break;
}
arg = arg->next;
}
fprintf(debugFd, "\n");
2018-05-01 13:34:05 +02:00
fflush(debugFd);
2018-04-04 16:17:13 +02:00
2018-05-01 13:34:05 +02:00
delay(DEBUG_INTERVAL);
}
return NULL;
}
2018-04-04 16:17:13 +02:00
void configureDebug()
{
2018-04-04 16:17:13 +02:00
// Génération du nom de fichier
char path[256];
2018-05-01 13:34:05 +02:00
sprintf(path, "log/%d.csv", nextId());
2018-04-04 16:17:13 +02:00
// Open file
2018-04-29 09:38:49 +02:00
debugFd = fopen(path, "w");
if (debugFd == NULL) {
perror("fopen debug file");
2018-04-04 16:17:13 +02:00
return;
}
2018-04-29 09:38:49 +02:00
fprintf(debugFd, "time");
}
void registerDebugVar(char* name, enum debugArgTypes type, void* var)
{
fprintf(debugFd, ",%s", name);
2018-04-29 09:38:49 +02:00
struct debugArg* arg = NULL;
struct debugArg** addrArg = &listeDebugArgs;
while (*addrArg != NULL) {
addrArg = &((*addrArg)->next);
}
arg = malloc(sizeof(struct debugArg));
arg->type = type;
arg->var = var;
arg->next = NULL;
*addrArg = arg;
}
void startDebug()
{
pthread_create(&tDebug, NULL, TaskDebug, NULL);
}
void deconfigureDebug()
{
pthread_cancel(tDebug);
2018-04-29 09:38:49 +02:00
fclose(debugFd);
// TODO Vider la liste des arguments
}