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

70 lines
1.4 KiB
C
Raw Normal View History

#include "debug.h"
#include <stdio.h>
#include <unistd.h> // sleep
2018-04-04 16:17:13 +02:00
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "position.h"
void* TaskDebug(void* pdata)
{
(void)pdata;
2018-04-04 16:17:13 +02:00
struct timespec tim; // 100 ms
tim.tv_sec = 0;
tim.tv_nsec = 100000000L;
/* tim.tv_sec = 1; */
/* tim.tv_nsec = 0; */
char line[1024];
clock_t t;
for (;;) {
2018-04-04 16:17:13 +02:00
// Calculating time index
t = clock() - debugStart;
// Generating line
sprintf(line, "%ld,%d,%ld,%ld\n", t, nbCalcPos, lCodTot, rCodTot);
// Writing
write(debugFd, line, strlen(line));
// Sleeping
nanosleep(&tim, NULL);
}
return NULL;
}
2018-04-04 16:17:13 +02:00
void configureDebug()
{
2018-04-04 16:17:13 +02:00
debugStart = clock();
// Génération du nom de fichier
char path[256];
time_t startTime;
time(&startTime);
sprintf(path, "log/%ld.csv", startTime);
// Open file
debugFd = open(path, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (debugFd < 0) {
fprintf(stderr, "Impossible d'ouvrir le fichier '%s', debug désactivé.\n", path);
return;
}
2018-04-04 16:17:13 +02:00
char header[] = "time,nbCalcPos,lCodTot,rCodTot\n";
write(debugFd, header, strlen(header));
pthread_create(&tDebug, NULL, TaskDebug, NULL);
}
void deconfigureDebug()
{
pthread_cancel(tDebug);
2018-04-04 16:17:13 +02:00
close(debugFd);
}