source: branches/3.3/mondo/src/lib/mr_msg.c @ 3630

Last change on this file since 3630 was 3630, checked in by Bruno Cornec, 4 years ago

Fix some autoconf warnings
Fix loop error in mr_msg_init

  • Property svn:eol-style set to native
File size: 2.7 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
16#include <stdio.h>
17#include <stdarg.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21
22static int mr_loglevel = 0;
23static char *mr_logfile = NULL;
24static FILE *mr_flog = NULL;
25
26/*
27 * This function is in the lowest part of the tree
28 * It should not depend on any other function of the mr lib
29 */
30
31/* Function allowing to change loglevel after init */
32void mr_msg_loglevel(int loglevel) {
33
34    mr_loglevel = loglevel;
35}
36
37/* Cleanup function for messages */
38void mr_msg_close(void) {
39
40    int res = 0;
41    if ((res = fclose(mr_flog)) != 0) {
42        fprintf(stderr,"Unable to close %s\n",mr_logfile);
43    }
44    free(mr_logfile);
45
46    mr_logfile = NULL;
47    mr_loglevel = 0;
48    mr_flog = NULL;
49return;
50}
51
52/* Initialization function for messages */
53void mr_msg_init(const char *logfile, int loglevel, int argc, char *argv[]) {
54
55    int i=1;
56
57    if (asprintf(&mr_logfile, "%s", logfile) == -1) {
58        fprintf(stderr,"Unable to alloc memory\n");
59        fprintf(stderr,"Logging desactivated\n");
60        mr_msg_close();
61    }
62    if (fopen(mr_logfile, "r") == NULL) {
63        /*  log file doesn't exist yet, so creating it */
64        if ((mr_flog = fopen(mr_logfile, "w")) == NULL) {
65            fprintf(stderr,"Unable to write to %s\n",mr_logfile);
66            fprintf(stderr,"Logging desactivated\n");
67            mr_msg_close();
68        }
69    } else {
70        /*  If it exists try to append to it */
71        if ((mr_flog = fopen(mr_logfile, "a")) == NULL) {
72            fprintf(stderr,"Unable to write to %s\n",mr_logfile);
73            fprintf(stderr,"Logging desactivated\n");
74            mr_msg_close();
75        }
76    }
77    while (i < argc) {
78        if (strcmp(argv[i],"-K") == 0) {
79            i++;
80            if (argv[i]) {
81                loglevel = atoi(argv[i]);
82            } else {
83                fprintf(stderr,"-K option requires a loglevel\n");
84                mr_msg_close();
85                exit(-1);
86            }
87        }
88        i++;
89    }
90    mr_msg_loglevel(loglevel);
91}
92
93/*
94 * Function that log a message. Not called directly
95 * but through other functions
96 * fmt needs to be just before ...
97 * level should be >0 to have file and line printed (real debug)
98 * If =0 it's an informative log message
99 */
100void mr_msg_int(int level, int line, const char *file, char *function, const char *fmt, ...) {
101
102    va_list args;
103
104    if ((mr_logfile == NULL) || (mr_flog == NULL)) {
105        return;
106    }
107
108    if (level <= mr_loglevel) {
109        if (level > 0) {
110            fprintf(mr_flog, "DBG%d: ", level);
111            fprintf(mr_flog, "%s->%s#%d: ", file, function, line);
112        } else {
113            fprintf(mr_flog, "INFO: ");
114        }
115        va_start(args,fmt);
116        if (vfprintf(mr_flog, fmt, args) < 0) {
117            fprintf(stderr,"Unable to print to %s\n",mr_logfile);
118        }
119        fprintf(mr_flog,"\n");
120        va_end(args);
121
122    }
123    return;
124}
125
Note: See TracBrowser for help on using the repository browser.