source: MondoRescue/branches/stable/mondo/src/lib/mr_msg.c@ 2208

Last change on this file since 2208 was 1226, checked in by Bruno Cornec, 17 years ago

Addition of a mr_msg_loglevel function in order to be able to change
the loglevel dynamically (useful for mondoarchive)

  • Property svn:eol-style set to native
File size: 2.1 KB
RevLine 
[1054]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
[1064]12#ifndef _GNU_SOURCE
13#define _GNU_SOURCE
14#endif
[1054]15#include <stdio.h>
16#include <stdarg.h>
[1064]17#include <stdlib.h>
[1054]18
19static int mr_loglevel = 0;
20static char *mr_logfile = NULL;
21
[1065]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
[1054]27/* Cleanup function for messages */
28void mr_msg_close(void) {
29 free(mr_logfile);
30 mr_logfile = NULL;
[1065]31 mr_loglevel = 0;
[1054]32}
33
[1226]34/* Function allowing to change loglevel after init */
35void mr_msg_loglevel(int loglevel) {
36
37 mr_loglevel = loglevel;
38}
39
[1054]40/* Initialization function for messages */
[1132]41void mr_msg_init(const char *logfile, int loglevel) {
[1054]42 FILE *fout = NULL;
43 int res = 0;
44
[1132]45 asprintf(&mr_logfile,logfile);
[1054]46 if ((fout = fopen(mr_logfile, "w")) == NULL) {
47 fprintf(stderr,"Unable to write to %s\n",mr_logfile);
[1061]48 fprintf(stderr,"Logging desactivated\n");
[1054]49 mr_msg_close();
50 }
51 if ((res = fclose(fout)) != 0) {
52 fprintf(stderr,"Unable to close %s\n",mr_logfile);
53 }
[1226]54 mr_msg_loglevel(loglevel);
[1054]55}
56
57/*
58 * Function that log a message. Not called directly
[1061]59 * but through other functions
[1104]60 * fmt needs to be just before ...
[1133]61 * debug should be >0 to have file and line printed (real debug)
62 * If =0 it's an informative log message
[1054]63 */
[1104]64void mr_msg_int(int debug, int line, const char *file, const char *fmt, ...) {
[1054]65
66 int i = 0;
67 int res = 0;
68 FILE *fout = NULL;
[1064]69 va_list args;
[1054]70
71 if (mr_logfile == NULL) {
72 return;
73 }
74
75 if (debug <= mr_loglevel) {
76 if ((fout = fopen(mr_logfile, "a")) == NULL) {
77 fprintf(stderr,"Unable to append to %s\n",mr_logfile);
78 return;
79 }
80
81 // add 2 spaces to distinguish log levels
82 if (debug > 0) {
83 for (i = 1; i < debug; i++)
84 fprintf(fout, " ");
[1104]85 fprintf(fout, "%s #%d: ", file, line);
[1054]86 }
[1065]87 va_start(args,fmt);
[1134]88 if (vfprintf(fout, fmt, args) < 0) {
[1054]89 fprintf(stderr,"Unable to print to %s\n",mr_logfile);
90 }
[1134]91 fprintf(fout,"\n");
[1065]92 va_end(args);
[1054]93
94 if ((res = fclose(fout)) != 0) {
95 fprintf(stderr,"Unable to close %s\n",mr_logfile);
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.