[900] | 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 |
|
---|
[1074] | 12 | #ifndef _GNU_SOURCE
|
---|
| 13 | #define _GNU_SOURCE
|
---|
| 14 | #endif
|
---|
[900] | 15 | #include <stdio.h>
|
---|
| 16 | #include <stdarg.h>
|
---|
[1074] | 17 | #include <stdlib.h>
|
---|
[900] | 18 |
|
---|
| 19 | static int mr_loglevel = 0;
|
---|
| 20 | static char *mr_logfile = NULL;
|
---|
| 21 |
|
---|
[1074] | 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 |
|
---|
| 27 | /* Cleanup function for messages */
|
---|
| 28 | void mr_msg_close(void) {
|
---|
| 29 | free(mr_logfile);
|
---|
| 30 | mr_logfile = NULL;
|
---|
| 31 | mr_loglevel = 0;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[900] | 34 | /* Initialization function for messages */
|
---|
[1074] | 35 | void mr_msg_init(const char *configfile, int loglevel) {
|
---|
| 36 | FILE *fout = NULL;
|
---|
| 37 | int res = 0;
|
---|
[900] | 38 |
|
---|
[1074] | 39 | asprintf(&mr_logfile,configfile);
|
---|
| 40 | if ((fout = fopen(mr_logfile, "w")) == NULL) {
|
---|
| 41 | fprintf(stderr,"Unable to write to %s\n",mr_logfile);
|
---|
| 42 | fprintf(stderr,"Logging desactivated\n");
|
---|
| 43 | mr_msg_close();
|
---|
| 44 | }
|
---|
| 45 | if ((res = fclose(fout)) != 0) {
|
---|
| 46 | fprintf(stderr,"Unable to close %s\n",mr_logfile);
|
---|
| 47 | }
|
---|
| 48 | mr_loglevel = loglevel;
|
---|
[900] | 49 | }
|
---|
| 50 |
|
---|
| 51 | /*
|
---|
| 52 | * Function that log a message. Not called directly
|
---|
[1074] | 53 | * but through other functions
|
---|
[1106] | 54 | * fmt needs to be just before ...
|
---|
[900] | 55 | */
|
---|
[1106] | 56 | void mr_msg_int(int debug, int line, const char *file, const char *fmt, ...) {
|
---|
[900] | 57 |
|
---|
| 58 | int i = 0;
|
---|
| 59 | int res = 0;
|
---|
| 60 | FILE *fout = NULL;
|
---|
[1074] | 61 | va_list args;
|
---|
[900] | 62 |
|
---|
[1074] | 63 | if (mr_logfile == NULL) {
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[900] | 67 | if (debug <= mr_loglevel) {
|
---|
[1074] | 68 | if ((fout = fopen(mr_logfile, "a")) == NULL) {
|
---|
[900] | 69 | fprintf(stderr,"Unable to append to %s\n",mr_logfile);
|
---|
| 70 | return;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // add 2 spaces to distinguish log levels
|
---|
| 74 | if (debug > 0) {
|
---|
| 75 | for (i = 1; i < debug; i++)
|
---|
| 76 | fprintf(fout, " ");
|
---|
[1106] | 77 | fprintf(fout, "%s #%d: ", file, line);
|
---|
[900] | 78 | }
|
---|
[1074] | 79 | va_start(args,fmt);
|
---|
| 80 | if (vfprintf(fout, fmt, args) < 0) {
|
---|
| 81 | fprintf(stderr,"Unable to print to %s\n",mr_logfile);
|
---|
| 82 | }
|
---|
| 83 | va_end(args);
|
---|
[900] | 84 |
|
---|
| 85 | if ((res = fclose(fout)) != 0) {
|
---|
| 86 | fprintf(stderr,"Unable to close %s\n",mr_logfile);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|