Changeset 3509 in MondoRescue for branches/3.2/mondo/src/lib/mr_msg.c
- Timestamp:
- Feb 13, 2016, 2:57:55 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mondo/src/lib/mr_msg.c
r2209 r3509 19 19 static int mr_loglevel = 0; 20 20 static char *mr_logfile = NULL; 21 static FILE *mr_flog = NULL; 21 22 22 23 /* … … 24 25 * It should not depend on any other function of the mr lib 25 26 */ 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 }33 27 34 28 /* Function allowing to change loglevel after init */ … … 38 32 } 39 33 34 /* Cleanup function for messages */ 35 void 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; 46 return; 47 } 48 40 49 /* Initialization function for messages */ 41 50 void mr_msg_init(const char *logfile, int loglevel) { 42 FILE *fout = NULL;43 int res = 0;44 45 51 if (asprintf(&mr_logfile, "%s", logfile) == -1) { 46 52 fprintf(stderr,"Unable to alloc memory\n"); … … 48 54 mr_msg_close(); 49 55 } 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 } 57 70 } 58 71 mr_msg_loglevel(loglevel); … … 63 76 * but through other functions 64 77 * fmt needs to be just before ... 65 * debugshould be >0 to have file and line printed (real debug)78 * level should be >0 to have file and line printed (real debug) 66 79 * If =0 it's an informative log message 67 80 */ 68 void mr_msg_int(int debug, int line, const char *file, const char *fmt, ...) {81 void mr_msg_int(int level, int line, const char *file, char *function, const char *fmt, ...) { 69 82 70 int i = 0;71 int res = 0;72 FILE *fout = NULL;73 83 va_list args; 74 84 75 if ( mr_logfile == NULL) {85 if ((mr_logfile == NULL) || (mr_flog == NULL)) { 76 86 return; 77 87 } 78 88 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: "); 90 95 } 91 96 va_start(args,fmt); 92 if (vfprintf( fout, fmt, args) < 0) {97 if (vfprintf(mr_flog, fmt, args) < 0) { 93 98 fprintf(stderr,"Unable to print to %s\n",mr_logfile); 94 99 } 95 fprintf( fout,"\n");100 fprintf(mr_flog,"\n"); 96 101 va_end(args); 97 102 98 if ((res = fclose(fout)) != 0) {99 fprintf(stderr,"Unable to close %s\n",mr_logfile);100 }101 103 } 104 return; 102 105 } 106
Note:
See TracChangeset
for help on using the changeset viewer.