source: MondoRescue/branches/2.2.9/mondo/src/lib/mr_msg.c @ 2209

Last change on this file since 2209 was 2209, checked in by Bruno Cornec, 11 years ago
  • Use new libmr.a in build process
  • Use mr_strcat to fix a memory allocation bug when mountlist contains lots of references unavailable at restore time, creating a error when memory is statically allocated (more usage is expected now).
  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1/*
2 * $Id$
3 *
4 * Code (c)2006 Bruno Cornec <bruno@mondorescue.org>
5 *
6 *     Main file of mr_msg : a very small and simple
7 *     library for messages management
8 *
9 * Provided under the GPLv2
10 */
11
12#ifndef _GNU_SOURCE
13#define _GNU_SOURCE
14#endif
15#include <stdio.h>
16#include <stdarg.h>
17#include <stdlib.h>
18
19static int mr_loglevel = 0;
20static char *mr_logfile = NULL;
21
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
34/* Function allowing to change loglevel after init */
35void mr_msg_loglevel(int loglevel) {
36
37    mr_loglevel = loglevel;
38}
39
40/* Initialization function for messages */
41void mr_msg_init(const char *logfile, int loglevel) {
42    FILE *fout = NULL;
43    int res = 0;
44
45    if (asprintf(&mr_logfile, "%s", logfile) == -1) {
46        fprintf(stderr,"Unable to alloc memory\n");
47        fprintf(stderr,"Logging desactivated\n");
48        mr_msg_close();
49    }
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);
57    }
58    mr_msg_loglevel(loglevel);
59}
60
61/*
62 * Function that log a message. Not called directly
63 * but through other functions
64 * fmt needs to be just before ...
65 * debug should be >0 to have file and line printed (real debug)
66 * If =0 it's an informative log message
67 */
68void mr_msg_int(int debug, int line, const char *file, const char *fmt, ...) {
69
70    int i = 0;
71    int res = 0;
72    FILE *fout = NULL;
73    va_list args;
74
75    if (mr_logfile == NULL) {
76        return;
77    }
78
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);
90        }
91        va_start(args,fmt);
92        if (vfprintf(fout, fmt, args) < 0) {
93            fprintf(stderr,"Unable to print to %s\n",mr_logfile);
94        }
95        fprintf(fout,"\n");
96        va_end(args);
97
98        if ((res = fclose(fout)) != 0) {
99            fprintf(stderr,"Unable to close %s\n",mr_logfile);
100        }
101    }
102}
Note: See TracBrowser for help on using the repository browser.