180 lines
6.7 KiB
C
180 lines
6.7 KiB
C
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
* Gem-graph client *
|
|
* Finite State Machine - Journal *
|
|
* *
|
|
* Copyright © 2024 Libre en Communs <contact@a-lec.org> *
|
|
* Copyright © 2024 Jean Sirmai <jean@a-lec.org> *
|
|
* *
|
|
* This file is part of Gem-graph. *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify it *
|
|
* under the terms of the GNU Affero General Public License *
|
|
* as published by the Free Software Foundation, *
|
|
* either version 3 of the License, *
|
|
* or (at your option) any later version. *
|
|
* *
|
|
* This program is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; *
|
|
* without even the implied warranty of MERCHANTABILITY *
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
* See the GNU Affero General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
//#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <string.h>
|
|
#include <sys/time.h>
|
|
#include "../include/fsm.h"
|
|
|
|
/******************************************************************************/
|
|
/* A journal stores chronologically the events */
|
|
/* during a session run (rules exec, mainly) */
|
|
/******************************************************************************/
|
|
|
|
/* 2024-09-22
|
|
* What types of events should be reported ? (fsm, widgets, ... )
|
|
* For what purpose ?
|
|
* What information must be collected and transmitted ?
|
|
* How do you name and classify this information ?
|
|
* How do you present it ?
|
|
*
|
|
* date clé (rank) fichier source fonction source +/- valeur */
|
|
|
|
|
|
/*
|
|
time_t current_time = time(NULL);
|
|
// Check if the time retrieval was successful
|
|
if (current_time == ((time_t)-1)) printf("Error getting current time.\n");
|
|
// Convert to local time format and print
|
|
printf(" Current timestamp: %ld %s\n", current_time, message);
|
|
printf(" Current time: %s", ctime(¤t_time));
|
|
*/
|
|
|
|
void fsm_journal_init (fsm_struct_journal *jj)
|
|
{
|
|
jj->first = NULL;
|
|
jj->last = NULL;
|
|
}
|
|
|
|
void fsm_journal_clear (fsm_struct_journal *jj,
|
|
const char *file_source,
|
|
const char *function_source,
|
|
const char *string_value)
|
|
{
|
|
fsm_struct_journal_unit *tmp;
|
|
fsm_struct_journal_unit *a_unit = jj->first;
|
|
while(a_unit)
|
|
{
|
|
tmp = a_unit;
|
|
a_unit = a_unit->next;
|
|
free (tmp);
|
|
}
|
|
jj->first = NULL;
|
|
jj->last = NULL;
|
|
}
|
|
|
|
void fsm_journal_push_front (fsm_struct_journal *jj,
|
|
const char *file_source,
|
|
const char *function_source,
|
|
const char *string_value)
|
|
{
|
|
struct timeval tv;
|
|
gettimeofday (&tv, NULL);
|
|
fsm_struct_journal_unit *new_unit = malloc (sizeof(fsm_struct_journal_unit));
|
|
if (! new_unit) exit (EXIT_FAILURE);
|
|
|
|
new_unit->yy_dd_mm = tv.tv_sec;
|
|
new_unit->usec = tv.tv_usec;
|
|
new_unit->file_source = file_source;
|
|
new_unit->function_source = function_source;
|
|
new_unit->string_value = string_value;
|
|
|
|
new_unit->next = jj->first;
|
|
new_unit->prev = NULL;
|
|
if (jj->first) jj->first->prev = new_unit;
|
|
else jj->last = new_unit;
|
|
jj->first = new_unit;
|
|
}
|
|
|
|
long fsm_journal_pop_back (fsm_struct_journal *jj,
|
|
const char *file_source,
|
|
const char *function_source,
|
|
const char *string_value)
|
|
{
|
|
long usec;
|
|
fsm_struct_journal_unit *tmp = jj->last;
|
|
if (! tmp) return -1;
|
|
usec = tmp->usec;
|
|
jj->last = tmp->prev;
|
|
if (jj->last) jj->last->next = NULL;
|
|
else jj->first = NULL;
|
|
free (tmp);
|
|
return usec; // retourne l'évènement retiré du journal.
|
|
}
|
|
|
|
int fsm_journal_length (fsm_struct_journal jj)
|
|
{
|
|
fsm_struct_journal_unit *a_unit = jj.first;
|
|
int nb = 0;
|
|
while (a_unit)
|
|
{
|
|
nb ++;
|
|
a_unit = a_unit->next;
|
|
}
|
|
// printf ("fsm journal length = %d\n", nb);
|
|
return nb;
|
|
}
|
|
|
|
void fsm_journal_seek (fsm_struct_journal jj, long usec,
|
|
const char *file_source,
|
|
const char *function_source,
|
|
const char *string_value)
|
|
{
|
|
fsm_struct_journal_unit *a_unit = jj.first;
|
|
int nb = 0;
|
|
while (a_unit)
|
|
{
|
|
if (usec == a_unit->usec) nb++;
|
|
a_unit = a_unit->next;
|
|
}
|
|
if (nb > 0) printf ("> date (usec) %ld found %d times in journal\n", usec, nb);
|
|
else printf ("> date (usec) %ld not found in journal\n", usec);
|
|
}
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
// ref: sudo cat /var/log/messages
|
|
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz" "%Y-%m-%d %H:%M:%S"
|
|
// https://www.man7.org/linux/man-pages/man3/strftime.3.html
|
|
// https://en.cppreference.com/w/c/io/fprintf
|
|
// https://nicolasj.developpez.com/articles/libc/string/
|
|
// https://thelinuxcode.com/pass-string-function-c-language/
|
|
// https://mefics.org/fr/allocation-dynamique-de-m%C3%A9moire-en-c-fonctions-malloc-calloc/
|
|
//------------------------------------------------------------------------------
|
|
|
|
void fsm_journal_publish (fsm_struct_journal jj)
|
|
{
|
|
fsm_struct_journal_unit *a_unit = jj.last;
|
|
char buf [JOURNAL_LOG_MAX_LENGTH];
|
|
int nb = 0;
|
|
while (a_unit)
|
|
{
|
|
strftime(buf, sizeof(buf), "%D %T", localtime(&a_unit->yy_dd_mm));
|
|
g_message ("%s + %-6ld %6d %-32s %-38s %-50s",
|
|
buf,
|
|
a_unit->usec,
|
|
nb,
|
|
a_unit->file_source,
|
|
a_unit->function_source,
|
|
a_unit->string_value);
|
|
a_unit = a_unit->prev;
|
|
nb ++;
|
|
}
|
|
}
|
|
|