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

Last change on this file since 1054 was 1054, checked in by Bruno Cornec, 17 years ago
  • Backporting more trunk content into stable
  • Adding the test and lib directories
  • Preparing for l10n
  • Property svn:eol-style set to native
File size: 1.9 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#include <stdio.h>
13#include <stdarg.h>
14
15static int mr_loglevel = 0;
16static char *mr_logfile = NULL;
17
18/* Cleanup function for messages */
19void mr_msg_close(void) {
20 free(mr_logfile);
21 mr_logfile = NULL;
22}
23
24/* Initialization function for messages */
25void mr_msg_init(const char *configfile, int loglevel) {
26 FILE *fout = NULL;
27 int res = 0;
28
29 asprintf(&mr_logfile,configfile);
30 if ((fout = fopen(mr_logfile, "w")) == NULL) {
31 fprintf(stderr,"Unable to write to %s\n",mr_logfile);
32 fprintf(stderr,"Logging desactivated\n",mr_logfile);
33 mr_msg_close();
34 }
35 if ((res = fclose(fout)) != 0) {
36 fprintf(stderr,"Unable to close %s\n",mr_logfile);
37 }
38 mr_loglevel = loglevel;
39}
40
41/*
42 * Function that log a message. Not called directly
43 * but through macros in mr_msg.h
44 */
45void _mr_msg(int debug, const char *file, const char *function, int line, const char *fmt, ...) {
46
47 int i = 0;
48 int res = 0;
49 FILE *fout = NULL;
50 va_list args;
51
52 if (mr_logfile == NULL) {
53 return;
54 }
55
56 if (debug <= mr_loglevel) {
57 if ((fout = fopen(mr_logfile, "a")) == NULL) {
58 fprintf(stderr,"Unable to append to %s\n",mr_logfile);
59 return;
60 }
61
62 // add 2 spaces to distinguish log levels
63 if (debug > 0) {
64 for (i = 1; i < debug; i++)
65 fprintf(fout, " ");
66 fprintf(fout, "%s->%s#%d: ", file, function, line);
67 }
68 va_start(args, fmt);
69 if (vfprintf(fout, fmt, args) < 0) {
70 fprintf(stderr,"Unable to print to %s\n",mr_logfile);
71 }
72 va_end(args);
73
74 fprintf(fout, "\n");
75 if ((res = fclose(fout)) != 0) {
76 fprintf(stderr,"Unable to close %s\n",mr_logfile);
77 }
78 }
79}
80
81void mr_msg(int level, const char *format, ...) {
82
83 va_list args;
84 va_start(args, format);
85 _mr_msg(level, __FILE__, __FUNCTION__, __LINE__, format, args);
86 va_end(args);
87}
Note: See TracBrowser for help on using the repository browser.