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 |
|
---|
22 | static int mr_loglevel = 0;
|
---|
23 | static char *mr_logfile = NULL;
|
---|
24 | static 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 */
|
---|
32 | void mr_msg_loglevel(int loglevel) {
|
---|
33 |
|
---|
34 | mr_loglevel = loglevel;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /* Cleanup function for messages */
|
---|
38 | void 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;
|
---|
49 | return;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* Initialization function for messages */
|
---|
53 | void 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 | }
|
---|
89 | mr_msg_loglevel(loglevel);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Function that log a message. Not called directly
|
---|
94 | * but through other functions
|
---|
95 | * fmt needs to be just before ...
|
---|
96 | * level should be >0 to have file and line printed (real debug)
|
---|
97 | * If =0 it's an informative log message
|
---|
98 | */
|
---|
99 | void mr_msg_int(int level, int line, const char *file, char *function, const char *fmt, ...) {
|
---|
100 |
|
---|
101 | va_list args;
|
---|
102 |
|
---|
103 | if ((mr_logfile == NULL) || (mr_flog == NULL)) {
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (level <= mr_loglevel) {
|
---|
108 | if (level > 0) {
|
---|
109 | fprintf(mr_flog, "DBG%d: ", level);
|
---|
110 | fprintf(mr_flog, "%s->%s#%d: ", file, function, line);
|
---|
111 | } else {
|
---|
112 | fprintf(mr_flog, "INFO: ");
|
---|
113 | }
|
---|
114 | va_start(args,fmt);
|
---|
115 | if (vfprintf(mr_flog, fmt, args) < 0) {
|
---|
116 | fprintf(stderr,"Unable to print to %s\n",mr_logfile);
|
---|
117 | }
|
---|
118 | fprintf(mr_flog,"\n");
|
---|
119 | va_end(args);
|
---|
120 |
|
---|
121 | }
|
---|
122 | return;
|
---|
123 | }
|
---|
124 |
|
---|