source: MondoRescue/branches/3.2/mondo/src/lib/mr_msg.c@ 3510

Last change on this file since 3510 was 3509, checked in by Bruno Cornec, 8 years ago
  • 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
  • Property svn:eol-style set to native
File size: 2.4 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;
21static FILE *mr_flog = NULL;
22
23/*
24 * This function is in the lowest part of the tree
25 * It should not depend on any other function of the mr lib
26 */
27
28/* Function allowing to change loglevel after init */
29void mr_msg_loglevel(int loglevel) {
30
31 mr_loglevel = loglevel;
32}
33
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
49/* Initialization function for messages */
50void mr_msg_init(const char *logfile, int loglevel) {
51 if (asprintf(&mr_logfile, "%s", logfile) == -1) {
52 fprintf(stderr,"Unable to alloc memory\n");
53 fprintf(stderr,"Logging desactivated\n");
54 mr_msg_close();
55 }
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 }
70 }
71 mr_msg_loglevel(loglevel);
72}
73
74/*
75 * Function that log a message. Not called directly
76 * but through other functions
77 * fmt needs to be just before ...
78 * level should be >0 to have file and line printed (real debug)
79 * If =0 it's an informative log message
80 */
81void mr_msg_int(int level, int line, const char *file, char *function, const char *fmt, ...) {
82
83 va_list args;
84
85 if ((mr_logfile == NULL) || (mr_flog == NULL)) {
86 return;
87 }
88
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: ");
95 }
96 va_start(args,fmt);
97 if (vfprintf(mr_flog, fmt, args) < 0) {
98 fprintf(stderr,"Unable to print to %s\n",mr_logfile);
99 }
100 fprintf(mr_flog,"\n");
101 va_end(args);
102
103 }
104 return;
105}
106
Note: See TracBrowser for help on using the repository browser.