Explorar o código

:sparkles: Ajout fonction d'affichage message erreur

Loquicom %!s(int64=6) %!d(string=hai) anos
pai
achega
5053d96a0c
Modificáronse 3 ficheiros con 35 adicións e 4 borrados
  1. 27 3
      error.c
  2. 3 1
      error.h
  3. 5 0
      mysh.c

+ 27 - 3
error.c

@@ -4,9 +4,11 @@
  *
  * Created on 8 novembre 2018
  */
+#define _POSIX_C_SOURCE 200809L
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/types.h>
@@ -29,7 +31,7 @@ char* serrlib[] = {
 };
 Error error;
 
-/* --- Fonctions privée --- */
+/* --- Fonctions de la structure --- */
 void end(){
     time_t temps;
     int fd;
@@ -56,6 +58,8 @@ void end(){
     error.exit_status = NULL;
     error.exit_msg = NULL;
     error.add = NULL;
+    error.print = NULL;
+    error.printadd = NULL;
     //On indique que la structure est non inialisée
     error.init = 0;
 }
@@ -78,8 +82,26 @@ void exit_msg(char* msg, int status){
     error.exit_status(status);
 }
 
-void add(char* msg){
-    fprintf(stderr, "%s\n", msg);
+void add(const char* format, ...){
+    va_list arg;
+    va_start(arg, format);
+    vfprintf(stderr, format, arg);
+    va_end(arg);
+}
+
+void printerr(const char* format, ...){
+    va_list arg;
+    va_start(arg, format);
+    vdprintf(error.errfd, format, arg);
+    va_end(arg);
+}
+
+void printadd(const char* format, ...){
+    va_list arg;
+    va_start(arg, format);
+    vdprintf(error.errfd, format, arg);
+    vfprintf(stderr, format, arg);
+    va_end(arg);
 }
 
 /* --- Fonctions publiques --- */
@@ -146,6 +168,8 @@ void error_finit(const char* filename){
     error.exit_status = exit_status;
     error.exit_msg = exit_msg;
     error.add = add;
+    error.print = printerr;
+    error.printadd = printadd;
     //Ecriture 1er ligne
     fprintf(stderr, "----- Log processus %d (timestamp : %ld) -----\n", getpid(), error.start);
     //On indique que l'inialisation a eu lieu

+ 3 - 1
error.h

@@ -46,7 +46,9 @@ typedef struct{
     void (*exit_err)(); //Termine le programme avec EXIT_FAILURE
     void (*exit_status)(int); //Termine le programme avec un status utilisateur
     void (*exit_msg)(char*, int); //Termine le programme avec un status utilisateur et un message dans la sortie standard
-    void (*add)(char*); //Ajoute un message dans le log
+    void (*add)(const char*, ...); //Ajoute un message dans le log
+    void (*print)(const char*, ...); //Affiche un message dans la sortie d'erreur
+    void (*printadd)(const char*, ...); //Fonction print + add
 }Error;
 
 /* --- Extern --- */

+ 5 - 0
mysh.c

@@ -51,6 +51,11 @@ int main(int argc, char* argv[]) {
     int a;
     //Initialisation erreur
     error_finit("mysh.log");
+    
+    error.print("Test erreur\n");
+    fprintf(stderr, "Test erreur2\n");
+    error.exit();
+            
     //Recup ligne
     //printf("%s\n", fgets(str, 500, stdin));&
     memset(str, 0, 500);