2024-09-10 12:58:18 +02:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* 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/>. *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2024-08-29 08:21:22 +02:00
|
|
|
|
2024-09-22 22:47:52 +02:00
|
|
|
//#include <stdbool.h>
|
2024-08-29 08:21:22 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-09-11 09:05:10 +02:00
|
|
|
#include <time.h>
|
2024-09-14 23:03:19 +02:00
|
|
|
#include <string.h>
|
2024-09-11 09:05:10 +02:00
|
|
|
#include <sys/time.h>
|
2024-08-29 08:21:22 +02:00
|
|
|
#include "../../include/fsm.h"
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2024-09-22 22:47:52 +02:00
|
|
|
/* A journal stores chronologically the events */
|
2024-09-10 22:37:31 +02:00
|
|
|
/* during a session run (rules exec, mainly) */
|
2024-08-29 08:21:22 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2024-09-22 22:47:52 +02:00
|
|
|
/* 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 */
|
|
|
|
|
2024-08-29 08:21:22 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
/*
|
2024-09-11 09:05:10 +02:00
|
|
|
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
|
2024-09-11 10:23:07 +02:00
|
|
|
printf(" Current timestamp: %ld %s\n", current_time, message);
|
2024-09-11 09:05:10 +02:00
|
|
|
printf(" Current time: %s", ctime(¤t_time));
|
2024-09-11 10:23:07 +02:00
|
|
|
*/
|
2024-09-11 23:57:13 +02:00
|
|
|
|
2024-09-16 16:23:45 +02:00
|
|
|
void fsm_journal_init (journal *jj)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-11 09:05:10 +02:00
|
|
|
jj->first = NULL;
|
|
|
|
jj->last = NULL;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-22 22:47:52 +02:00
|
|
|
void fsm_journal_clear (journal *jj,
|
|
|
|
const char *file_source,
|
|
|
|
const char *function_source,
|
|
|
|
const char *string_value)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *tmp;
|
|
|
|
unit *a_unit = jj->first;
|
|
|
|
while(a_unit)
|
|
|
|
{
|
|
|
|
tmp = a_unit;
|
|
|
|
a_unit = a_unit->next;
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
jj->first = NULL;
|
|
|
|
jj->last = NULL;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-22 22:47:52 +02:00
|
|
|
void fsm_journal_push_front (journal *jj,
|
|
|
|
const char *file_source,
|
|
|
|
const char *function_source,
|
|
|
|
const char *string_value)
|
2024-09-10 22:37:31 +02:00
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *new_unit = malloc(sizeof(unit));
|
|
|
|
if (! new_unit) exit (EXIT_FAILURE);
|
2024-09-11 23:57:13 +02:00
|
|
|
new_unit->yy_dd_mm = tv.tv_sec;
|
|
|
|
new_unit->usec = tv.tv_usec;
|
2024-09-22 22:47:52 +02:00
|
|
|
new_unit->file_source = file_source;
|
|
|
|
new_unit->function_source = function_source;
|
|
|
|
new_unit->string_value = string_value;
|
2024-09-10 22:37:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-09-22 22:47:52 +02:00
|
|
|
long fsm_journal_pop_back (journal *jj,
|
|
|
|
const char *file_source,
|
|
|
|
const char *function_source,
|
|
|
|
const char *string_value)
|
2024-09-10 22:37:31 +02:00
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
long usec;
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *tmp = jj->last;
|
|
|
|
if (! tmp) return -1;
|
2024-09-11 23:57:13 +02:00
|
|
|
usec = tmp->usec;
|
2024-09-10 22:37:31 +02:00
|
|
|
jj->last = tmp->prev;
|
|
|
|
if (jj->last) jj->last->next = NULL;
|
|
|
|
else jj->first = NULL;
|
|
|
|
free (tmp);
|
2024-09-11 23:57:13 +02:00
|
|
|
return usec; // retourne l'évènement retiré du journal.
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-11 23:57:13 +02:00
|
|
|
int fsm_journal_length (journal jj)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *a_unit = jj.first;
|
|
|
|
int nb = 0;
|
|
|
|
while (a_unit)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
nb ++;
|
|
|
|
a_unit = a_unit->next;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
2024-09-11 23:57:13 +02:00
|
|
|
// printf ("fsm journal length = %d\n", nb);
|
2024-09-10 22:37:31 +02:00
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
|
2024-09-22 22:47:52 +02:00
|
|
|
void fsm_journal_seek (journal jj, long usec,
|
|
|
|
const char *file_source,
|
|
|
|
const char *function_source,
|
|
|
|
const char *string_value)
|
2024-09-10 22:37:31 +02:00
|
|
|
{
|
2024-09-11 08:13:00 +02:00
|
|
|
unit *a_unit = jj.first;
|
2024-09-11 09:05:10 +02:00
|
|
|
int nb = 0;
|
2024-09-11 08:13:00 +02:00
|
|
|
while (a_unit)
|
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
if (usec == a_unit->usec) nb++;
|
2024-09-11 08:13:00 +02:00
|
|
|
a_unit = a_unit->next;
|
|
|
|
}
|
2024-09-11 23:57:13 +02:00
|
|
|
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);
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-14 23:03:19 +02:00
|
|
|
|
2024-09-16 08:06:08 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2024-09-22 22:47:52 +02:00
|
|
|
// ref: sudo cat /var/log/messages
|
2024-09-16 08:06:08 +02:00
|
|
|
// 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/
|
2024-09-21 08:47:17 +02:00
|
|
|
// https://mefics.org/fr/allocation-dynamique-de-m%C3%A9moire-en-c-fonctions-malloc-calloc/
|
2024-09-16 08:06:08 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2024-09-14 23:03:19 +02:00
|
|
|
|
2024-09-16 08:06:08 +02:00
|
|
|
void fsm_journal_publish (journal jj)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
unit *a_unit = jj.last;
|
2024-09-20 07:28:02 +02:00
|
|
|
char buf [JOURNAL_LOG_MAX_LENGTH];
|
2024-09-11 23:57:13 +02:00
|
|
|
int nb = 0;
|
2024-09-10 22:37:31 +02:00
|
|
|
while (a_unit)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
strftime(buf, sizeof(buf), "%D %T", localtime(&a_unit->yy_dd_mm));
|
2024-09-22 22:47:52 +02:00
|
|
|
g_message ("%s + %-6ld %6d %-30s %-60s %-60s",
|
|
|
|
buf,
|
|
|
|
a_unit->usec,
|
|
|
|
nb,
|
|
|
|
a_unit->file_source,
|
|
|
|
a_unit->function_source,
|
|
|
|
a_unit->string_value);
|
2024-09-11 23:57:13 +02:00
|
|
|
a_unit = a_unit->prev;
|
|
|
|
nb ++;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-10 22:37:31 +02:00
|
|
|
|