Changeset 3509 in MondoRescue for branches/3.2/mondo/src/lib/mr_msg.c


Ignore:
Timestamp:
Feb 13, 2016, 2:57:55 AM (8 years ago)
Author:
Bruno Cornec
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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.