Changeset 1074 in MondoRescue for trunk/mondo/src/lib/mr_msg.c


Ignore:
Timestamp:
Jan 25, 2007, 4:18:12 PM (17 years ago)
Author:
Bruno Cornec
Message:

This version of trunk desn't seg fault on mr_msg anymore.
Still not ready for 3.0.0 but improvements ongoing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/src/lib/mr_msg.c

    r900 r1074  
    1010 */
    1111
     12#ifndef _GNU_SOURCE
     13#define _GNU_SOURCE
     14#endif
    1215#include <stdio.h>
    1316#include <stdarg.h>
     17#include <stdlib.h>
    1418
    1519static int mr_loglevel = 0;
    1620static char *mr_logfile = NULL;
    1721
     22/*
     23 * This function is in the lowest part of the tree
     24 * It should not depend on any other function of the mr lib
     25 */
     26
     27/* Cleanup function for messages */
     28void mr_msg_close(void) {
     29    free(mr_logfile);
     30    mr_logfile = NULL;
     31    mr_loglevel = 0;
     32}
     33
    1834/* Initialization function for messages */
    19 void mr_msg_init(void) {
     35void mr_msg_init(const char *configfile, int loglevel) {
     36    FILE *fout = NULL;
     37    int res = 0;
    2038
     39    asprintf(&mr_logfile,configfile);
     40    if ((fout = fopen(mr_logfile, "w")) == NULL) {
     41        fprintf(stderr,"Unable to write to %s\n",mr_logfile);
     42        fprintf(stderr,"Logging desactivated\n");
     43        mr_msg_close();
     44    }
     45    if ((res = fclose(fout)) != 0) {
     46        fprintf(stderr,"Unable to close %s\n",mr_logfile);
     47    }
     48    mr_loglevel = loglevel;
    2149}
    2250
    2351/*
    2452 * Function that log a message. Not called directly
    25  * but through macros in mr_msg.h
     53 * but through other functions
    2654 */
    27 void _mr_msg(int debug, const char *file, const char *function, int line, const char *fmt, ...) {
     55void mr_msg(int debug, const char *fmt, ...) {
    2856
    29     va_list args;
    3057    int i = 0;
    3158    int res = 0;
    3259    FILE *fout = NULL;
     60    va_list args;
     61
     62    if (mr_logfile == NULL) {
     63        return;
     64    }
    3365
    3466    if (debug <= mr_loglevel) {
    35         va_start(args, fmt);
    36         if (!(fout = fopen(mr_logfile, "a"))) {
     67        if ((fout = fopen(mr_logfile, "a")) == NULL) {
    3768            fprintf(stderr,"Unable to append to %s\n",mr_logfile);
    3869            return;
     
    4374            for (i = 1; i < debug; i++)
    4475                fprintf(fout, "  ");
    45             fprintf(fout, "%s->%s#%d: ", file, function, line);
     76            fprintf(fout, "%s->%s#%d: ", __FILE__, __FUNCTION__, __LINE__);
    4677        }
    47         vfprintf(fout, fmt, args);
     78        va_start(args,fmt);
     79        if (vfprintf(fout, fmt, args) < 0) {
     80            fprintf(stderr,"Unable to print to %s\n",mr_logfile);
     81        }
     82        va_end(args);
    4883
    49         va_end(args);
    50         fprintf(fout, "\n");
    5184        if ((res = fclose(fout)) != 0) {
    5285            fprintf(stderr,"Unable to close %s\n",mr_logfile);
Note: See TracChangeset for help on using the changeset viewer.