1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-11-15 04:46:06 +01:00
cdf2018-principal/chef/src/debug.c

116 lines
2.3 KiB
C
Raw Normal View History

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-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;
void* TaskDebug(void* pdata)
{
(void)pdata;
2018-04-29 09:38:49 +02:00
clock_t debugStart;
2018-04-30 22:40:20 +02:00
debugStart = clock(); // TODO struct timespec
2018-04-29 09:38:49 +02:00
2018-04-04 16:17:13 +02:00
struct timespec tim; // 100 ms
tim.tv_sec = 0;
tim.tv_nsec = 100000000L;
2018-04-29 09:38:49 +02:00
fprintf(debugFd, "\n");
for (;;) {
2018-04-29 09:38:49 +02:00
clock_t t = clock() - debugStart;
fprintf(debugFd, "%ld", t);
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-04-04 16:17:13 +02:00
nanosleep(&tim, NULL);
}
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];
time_t startTime;
time(&startTime);
sprintf(path, "log/%ld.csv", startTime);
// 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
}