Changeset 3509


Ignore:
Timestamp:
Feb 13, 2016, 2:57:55 AM (22 months ago)
Author:
bruno
Message:
  • Change mr_msg_int interface by adding the FUNCTION macro
  • Use mr_msg in mr_system
  • Ability to use that new log system everywhere now
Location:
branches/3.2/mondo/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mondo/src/include/mr_msg.h

    r2208 r3509  
    1616/* functions (public methods) */
    1717
    18 #define mr_msg(x,y,args...) {mr_msg_int(x,__LINE__,__FILE__,y,## args);}
    19 extern void mr_msg_init(const char *configfile, int loglevel);
     18#define mr_msg(lvl,fmt,args...) {mr_msg_int(lvl,__LINE__,__FILE__,__FUNCTION__,fmt,## args);}
     19extern void mr_msg_init(const char *logfile, int loglevel);
    2020extern void mr_msg_close(void);
    2121extern void mr_msg_loglevel(int loglevel);
     
    2323/* Internal function bringing debuging info
    2424 * called indirectly through macros */
    25 extern inline void mr_msg_int(int debug,int line, const char *file, const char *fmt, ...);
     25extern inline void mr_msg_int(int debug, int line, const char *file, const char *function, const char *fmt, ...);
    2626
    2727#endif                          /* MR_MSG_H */
  • branches/3.2/mondo/src/include/mr_sys.h

    r3498 r3509  
    1414#include <stdarg.h>
    1515#include <stdio.h>
    16 
    17 /* functions (public methods) */
    18 
    19 #define mr_system(y,args...) mr_system_int(__LINE__,__FILE__,y,## args)
     16#include "mr_msg.h"
    2017
    2118/* Internal function bringing debuging info
     
    2320extern inline int mr_system_int(int line, char *file, const char *fmt, ...);
    2421
     22/* functions (public methods) */
     23#define mr_system(y,args...) mr_system_int(__LINE__,__FILE__,y,## args)
     24
     25/*  Global variables */
     26
     27extern int mr_loglevel;
     28extern char *mr_logfile;
     29
    2530#endif                          /* MR_SYS_H */
  • branches/3.2/mondo/src/lib/mr_conf.c

    r3294 r3509  
    409409        switch (error_code) {
    410410        case MRCONF_BAD_FILE:
    411             mr_msg_int(1,line,file,"%s (%s) %s %s", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_BAD_FILE,
     411            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s %s", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_BAD_FILE,
    412412                   add_line);
    413413            break;
    414414
    415415        case MRCONF_READING_FAILED:
    416             mr_msg_int(1,line,file,"%s (%s) %s", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_READING_FAILED);
     416            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_READING_FAILED);
    417417            break;
    418418
    419419        case MRCONF_FIELD_NOT_FOUND:
    420             mr_msg_int(1,line,file,"%s (%s) %s \"%s\"", MRCONF_STR_WARNING, mr_conf_filename, MRCONF_STR_FIELD_NOT_FOUND, add_line);
     420            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s \"%s\"", MRCONF_STR_WARNING, mr_conf_filename, MRCONF_STR_FIELD_NOT_FOUND, add_line);
    421421            break;
    422422
    423423        case MRCONF_FIELD_NO_VALUE:
    424             mr_msg_int(1,line,file,"%s (%s) %s \"%s\"", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_FIELD_NO_VALUE, add_line);
    425             mr_msg_int(1,line,file,"%s (%s) %s", MRCONF_STR_WARNING, mr_conf_filename, MRCONF_STR_IGNORE);
     424            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s \"%s\"", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_FIELD_NO_VALUE, add_line);
     425            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s", MRCONF_STR_WARNING, mr_conf_filename, MRCONF_STR_IGNORE);
    426426            break;
    427427
    428428        case MRCONF_CLOSE_BUT_NOT_OPEN:
    429             mr_msg_int(1,line,file,"%s (%s) %s", MRCONF_STR_WARNING, mr_conf_filename, MRCONF_STR_CLOSE_BUT_NOT_OPEN);
     429            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s", MRCONF_STR_WARNING, mr_conf_filename, MRCONF_STR_CLOSE_BUT_NOT_OPEN);
    430430            break;
    431431
    432432        case MRCONF_CALL_BUT_NOT_OPEN:
    433             mr_msg_int(1,line,file,"%s (%s) %s", MRCONF_STR_WARNING, mr_conf_filename, MRCONF_STR_CALL_BUT_NOT_OPEN);
     433            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s", MRCONF_STR_WARNING, mr_conf_filename, MRCONF_STR_CALL_BUT_NOT_OPEN);
    434434            break;
    435435
    436436        case MRCONF_OPEN_OPENED:
    437             mr_msg_int(1,line,file,"%s (%s) %s", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_OPEN_OPENED);
     437            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_OPEN_OPENED);
    438438            break;
    439439
    440440        case MRCONF_STRING_QUOTE:
    441             mr_msg_int(1,line,file,"%s: %s (%s) %s", add_line, MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_STRING_QUOTE);
     441            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s: %s (%s) %s", add_line, MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_STRING_QUOTE);
    442442            break;
    443443
    444444        case MRCONF_STRING_ENDQUOTE:
    445             mr_msg_int(1,line,file,"%s: %s (%s) %s", add_line, MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_STRING_ENDQUOTE);
     445            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s: %s (%s) %s", add_line, MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_STRING_ENDQUOTE);
    446446            break;
    447447
    448448        default:
    449             mr_msg_int(1,line,file,"%s (%s) %s", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_DEFAULT_ERROR);
    450             break;
    451         }
    452     }
    453 }
     449            mr_msg_int(1,line,file,"mr_conf_error_msg_int","%s (%s) %s", MRCONF_STR_ERROR, mr_conf_filename, MRCONF_STR_DEFAULT_ERROR);
     450            break;
     451        }
     452    }
     453}
  • branches/3.2/mondo/src/lib/mr_file.c

    r1136 r3509  
    2323
    2424    if ((fd = fopen(path, mode)) == NULL) {
    25         mr_msg_int(1,line,file,"Unable to open %s",path);
     25        mr_msg_int(1,line,file,"mr_fopen_int","Unable to open %s",path);
    2626        mr_exit(-1,"Exiting");
    2727    }
     
    3434
    3535    if (fd == NULL) {
    36         mr_msg_int(1,line,file,"fd is NULL.\nShould NOT happen.");
     36        mr_msg_int(1,line,file,"mr_fprintf_int","fd is NULL.\nShould NOT happen.");
    3737        mr_exit(-1,"Exiting");
    3838    }
    3939    va_start(args,fmt);
    4040    if (vfprintf(fd, fmt, args) < 0) {
    41         mr_msg_int(1,line,file,"Unable to print to fd");
     41        mr_msg_int(1,line,file,"mr_fprintf_int","Unable to print to fd");
    4242        mr_exit(-1,"Exiting");
    4343    }
     
    4848
    4949    if (fd == NULL) {
    50         mr_msg_int(1,line,file,"fd is NULL.\nShould NOT happen.");
     50        mr_msg_int(1,line,file,"mr_fclose_int","fd is NULL.\nShould NOT happen.");
    5151        mr_exit(-1,"Exiting");
    5252    }
    5353    if (*fd == NULL) {
    54         mr_msg_int(1,line,file,"File descriptor is NULL.\nShould NOT happen.");
     54        mr_msg_int(1,line,file,"mr_fclose_int","File descriptor is NULL.\nShould NOT happen.");
    5555        mr_exit(-1,"Exiting");
    5656    }
    5757    if (fclose(*fd) < 0) {
    58         mr_msg_int(1,line,file,"Unable to close File Descriptor");
     58        mr_msg_int(1,line,file,"mr_fclose_int","Unable to close File Descriptor");
    5959    }
    6060    *fd = NULL;
     
    6464
    6565    if (mkdir(pathname,mode) != 0) {
    66         mr_msg_int(1,line,file,"Unable to create directory %s",pathname);
     66        mr_msg_int(1,line,file,"mr_mkdir_int","Unable to create directory %s",pathname);
    6767        mr_exit(-1,"Exiting");
    6868    }
  • branches/3.2/mondo/src/lib/mr_mem.c

    r3294 r3509  
    3636        *allocated = NULL;
    3737    } else {
    38         mr_msg_int(8,line,file,"Attempt to free NULL pointer");
     38        mr_msg_int(8,line,file,"mr_asprintf","Attempt to free NULL pointer");
    3939    }
    4040}
     
    4747    ret = malloc(size);
    4848    if (ret == NULL) {
    49         mr_msg_int(1,line,file,"Unable to alloc memory in mr_malloc\nExiting...");
     49        mr_msg_int(1,line,file,"mr_asprintf","Unable to alloc memory in mr_malloc\nExiting...");
    5050        mr_exit(-1,"Unable to alloc memory in mr_malloc");
    5151    }
     
    6262    res = vasprintf(strp, fmt, args);
    6363    if (res == -1) {
    64         mr_msg_int(1,line,file,"Unable to alloc memory in mr_asprintf\nExiting...");
     64        mr_msg_int(1,line,file,"mr_asprintf","Unable to alloc memory in mr_asprintf\nExiting...");
    6565        mr_exit(-1,"Unable to alloc memory in mr_asprintf");
    6666    }
     
    7676    if (*lineptr != NULL) {
    7777        /* free a variable which should be already free */
    78         mr_msg_int(1,line,file,"mr_getline_ found a non freed variable here");
     78        mr_msg_int(1,line,file,"mr_getline_int","mr_getline_ found a non freed variable here");
    7979        mr_free_int((void **)lineptr,line,file);
    8080    }
    8181    ret = getline(lineptr,&n,fd);
    8282    if ((ret == -1) && (! feof(fd))) {
    83         mr_msg_int(1,line,file,"Unable to alloc memory in mr_getline\nExiting...");
     83        mr_msg_int(1,line,file,"mr_getline_int","Unable to alloc memory in mr_getline\nExiting...");
    8484        mr_exit(-1,"Unable to alloc memory in mr_getline");
    8585    }
     
    109109   
    110110    if (name == NULL) {
    111         mr_msg_int(1,line,file,"Unable to setenv a NULL variable\nExiting...");
     111        mr_msg_int(1,line,file,"mr_setenv_int","Unable to setenv a NULL variable\nExiting...");
    112112        mr_exit(-1, "Unable to setenv a NULL variable");
    113113    }
    114114    if (value == NULL) {
    115         mr_msg_int(1,line,file,"Unable to affect NULL to %s\nExiting...", name);
     115        mr_msg_int(1,line,file,"mr_setenv_int","Unable to affect NULL to %s\nExiting...", name);
    116116        mr_exit(-1, "Unable to affect a NULL variable");
    117117    }
    118118    if (setenv(name, value, 1) != 0) {
    119         mr_msg_int(1,line,file,"Unable to put %s in environment", name);
     119        mr_msg_int(1,line,file,"mr_setenv_int","Unable to put %s in environment", name);
    120120        mr_exit(-1,"Unable to put in environment");
    121121    }
     
    136136    }
    137137    if (in == NULL) {
    138         mr_msg_int(1,line,file,"Unable to add to NULL pointer\nExiting...");
     138        mr_msg_int(1,line,file,"mr_strcat_int","Unable to add to NULL pointer\nExiting...");
    139139        mr_exit(-1, "Unable to add to a NULL pointer");
    140140    }
     
    143143        res = vasprintf(in, fmt, args);
    144144        if (res == -1) {
    145             mr_msg_int(1,line,file,"Unable to alloc memory in mr_strcat\nExiting...");
     145            mr_msg_int(1,line,file,"mr_strcat_int","Unable to alloc memory in mr_strcat\nExiting...");
    146146            mr_exit(-1,"Unable to alloc memory in mr_strcat");
    147147        }
     
    151151        res = vasprintf(in, fmt2, args);
    152152        if (res == -1) {
    153             mr_msg_int(1,line,file,"Unable to alloc memory in mr_strcat\nExiting...");
     153            mr_msg_int(1,line,file,"mr_strcat_int","Unable to alloc memory in mr_strcat\nExiting...");
    154154            mr_exit(-1,"Unable to alloc memory in mr_strcat");
    155155        }
  • branches/3.2/mondo/src/lib/mr_msg.c

    r2209 r3509  
    1919static int mr_loglevel = 0;
    2020static char *mr_logfile = NULL;
     21static FILE *mr_flog = NULL;
    2122
    2223/*
     
    2425 * It should not depend on any other function of the mr lib
    2526 */
    26 
    27 /* Cleanup function for messages */
    28 void mr_msg_close(void) {
    29     free(mr_logfile);
    30     mr_logfile = NULL;
    31     mr_loglevel = 0;
    32 }
    3327
    3428/* Function allowing to change loglevel after init */
     
    3832}
    3933
     34/* Cleanup function for messages */
     35void mr_msg_close(void) {
     36
     37    int res = 0;
     38    if ((res = fclose(mr_flog)) != 0) {
     39        fprintf(stderr,"Unable to close %s\n",mr_logfile);
     40    }
     41    free(mr_logfile);
     42
     43    mr_logfile = NULL;
     44    mr_loglevel = 0;
     45    mr_flog = NULL;
     46return;
     47}
     48
    4049/* Initialization function for messages */
    4150void mr_msg_init(const char *logfile, int loglevel) {
    42     FILE *fout = NULL;
    43     int res = 0;
    44 
    4551    if (asprintf(&mr_logfile, "%s", logfile) == -1) {
    4652        fprintf(stderr,"Unable to alloc memory\n");
     
    4854        mr_msg_close();
    4955    }
    50     if ((fout = fopen(mr_logfile, "w")) == NULL) {
    51         fprintf(stderr,"Unable to write to %s\n",mr_logfile);
    52         fprintf(stderr,"Logging desactivated\n");
    53         mr_msg_close();
    54     }
    55     if ((res = fclose(fout)) != 0) {
    56         fprintf(stderr,"Unable to close %s\n",mr_logfile);
     56    if (fopen(mr_logfile, "r") == NULL) {
     57        /*  log file doesn't exist yet, so creating it */
     58        if ((mr_flog = fopen(mr_logfile, "w")) == NULL) {
     59            fprintf(stderr,"Unable to write to %s\n",mr_logfile);
     60            fprintf(stderr,"Logging desactivated\n");
     61            mr_msg_close();
     62        }
     63    } else {
     64        /*  If it exists try to append to it */
     65        if ((mr_flog = fopen(mr_logfile, "a")) == NULL) {
     66            fprintf(stderr,"Unable to write to %s\n",mr_logfile);
     67            fprintf(stderr,"Logging desactivated\n");
     68            mr_msg_close();
     69        }
    5770    }
    5871    mr_msg_loglevel(loglevel);
     
    6376 * but through other functions
    6477 * fmt needs to be just before ...
    65  * debug should be >0 to have file and line printed (real debug)
     78 * level should be >0 to have file and line printed (real debug)
    6679 * If =0 it's an informative log message
    6780 */
    68 void mr_msg_int(int debug, int line, const char *file, const char *fmt, ...) {
     81void mr_msg_int(int level, int line, const char *file, char *function, const char *fmt, ...) {
    6982
    70     int i = 0;
    71     int res = 0;
    72     FILE *fout = NULL;
    7383    va_list args;
    7484
    75     if (mr_logfile == NULL) {
     85    if ((mr_logfile == NULL) || (mr_flog == NULL)) {
    7686        return;
    7787    }
    7888
    79     if (debug <= mr_loglevel) {
    80         if ((fout = fopen(mr_logfile, "a")) == NULL) {
    81             fprintf(stderr,"Unable to append to %s\n",mr_logfile);
    82             return;
    83         }
    84 
    85         // add 2 spaces to distinguish log levels
    86         if (debug > 0) {
    87             for (i = 1; i < debug; i++)
    88                 fprintf(fout, "  ");
    89             fprintf(fout, "%s #%d: ", file, line);
     89    if (level <= mr_loglevel) {
     90        if (level > 0) {
     91            fprintf(mr_flog, "DBG%d: ", level);
     92            fprintf(mr_flog, "%s->%s#%d: ", file, function, line);
     93        } else {
     94            fprintf(mr_flog, "INFO: ");
    9095        }
    9196        va_start(args,fmt);
    92         if (vfprintf(fout, fmt, args) < 0) {
     97        if (vfprintf(mr_flog, fmt, args) < 0) {
    9398            fprintf(stderr,"Unable to print to %s\n",mr_logfile);
    9499        }
    95         fprintf(fout,"\n");
     100        fprintf(mr_flog,"\n");
    96101        va_end(args);
    97102
    98         if ((res = fclose(fout)) != 0) {
    99             fprintf(stderr,"Unable to close %s\n",mr_logfile);
    100         }
    101103    }
     104    return;
    102105}
     106
  • branches/3.2/mondo/src/lib/mr_sys.c

    r3498 r3509  
    3636    res = vasprintf(&cmd, fmt, args);
    3737    if (res == -1) {
    38         mr_msg_int(1,line,file,"Unable to alloc memory in mr_asprintf\nExiting...");
    39         mr_exit(-1,"Unable to alloc memory in mr_asprintf");
     38        mr_msg_int(1,line,file,"mr_system_int","Unable to alloc memory in mr_system_int\nExiting...");
     39        mr_exit(-1,"Unable to alloc memory in mr_system_int");
    4040    }
    4141    /*
     
    4343    */
    4444    res = system(cmd);
     45    if (res < 0 ) {
     46        mr_msg(4, "Unable to execute %s",cmd);
     47    }
    4548    mr_free(cmd);
    4649    va_end(args);
  • branches/3.2/mondo/src/mondoarchive/mondoarchive.c

    r3478 r3509  
    322322    }
    323323
     324    /*  setup log level for new log system as well */
     325    mr_msg_init(MONDO_LOGFILE,g_loglevel);
     326
    324327    if (pre_param_configuration()) {
    325328        fatal_error("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
     
    448451        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
    449452    }
     453    mr_msg_close();
    450454    finish(retval);
    451455
  • branches/3.2/mondo/src/mondorestore/mondorestore.c

    r3472 r3509  
    28052805    }
    28062806
     2807    /*  setup log level for new log system as well */
     2808    mr_msg_init(MONDO_LOGFILE,g_loglevel);
     2809
    28072810    // Handle params here first
    28082811    handle_incoming_parameters(argc,argv);
     
    30043007    unlink("/tmp/filelist.full.gz");
    30053008
     3009    mr_msg_close();
    30063010    exit(retval);
    30073011}
Note: See TracChangeset for help on using the changeset viewer.