[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 |
|
---|
| 19 | static int mr_loglevel = 0;
|
---|
| 20 | static char *mr_logfile = NULL;
|
---|
[3509] | 21 | static FILE *mr_flog = NULL;
|
---|
[1054] | 22 |
|
---|
[1065] | 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 |
|
---|
[3509] | 28 | /* Function allowing to change loglevel after init */
|
---|
| 29 | void mr_msg_loglevel(int loglevel) {
|
---|
| 30 |
|
---|
| 31 | mr_loglevel = loglevel;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[1054] | 34 | /* Cleanup function for messages */
|
---|
| 35 | void mr_msg_close(void) {
|
---|
[3509] | 36 |
|
---|
| 37 | int res = 0;
|
---|
| 38 | if ((res = fclose(mr_flog)) != 0) {
|
---|
| 39 | fprintf(stderr,"Unable to close %s\n",mr_logfile);
|
---|
| 40 | }
|
---|
[1054] | 41 | free(mr_logfile);
|
---|
[3509] | 42 |
|
---|
[1054] | 43 | mr_logfile = NULL;
|
---|
[1065] | 44 | mr_loglevel = 0;
|
---|
[3509] | 45 | mr_flog = NULL;
|
---|
| 46 | return;
|
---|
[1054] | 47 | }
|
---|
| 48 |
|
---|
| 49 | /* Initialization function for messages */
|
---|
[1132] | 50 | void mr_msg_init(const char *logfile, int loglevel) {
|
---|
[2209] | 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 | }
|
---|
[3509] | 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 | }
|
---|
[1054] | 70 | }
|
---|
[1226] | 71 | mr_msg_loglevel(loglevel);
|
---|
[1054] | 72 | }
|
---|
| 73 |
|
---|
| 74 | /*
|
---|
| 75 | * Function that log a message. Not called directly
|
---|
[1061] | 76 | * but through other functions
|
---|
[1104] | 77 | * fmt needs to be just before ...
|
---|
[3509] | 78 | * level should be >0 to have file and line printed (real debug)
|
---|
[1133] | 79 | * If =0 it's an informative log message
|
---|
[1054] | 80 | */
|
---|
[3509] | 81 | void mr_msg_int(int level, int line, const char *file, char *function, const char *fmt, ...) {
|
---|
[1054] | 82 |
|
---|
[1064] | 83 | va_list args;
|
---|
[1054] | 84 |
|
---|
[3509] | 85 | if ((mr_logfile == NULL) || (mr_flog == NULL)) {
|
---|
[1054] | 86 | return;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[3509] | 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: ");
|
---|
[1054] | 95 | }
|
---|
[1065] | 96 | va_start(args,fmt);
|
---|
[3509] | 97 | if (vfprintf(mr_flog, fmt, args) < 0) {
|
---|
[1054] | 98 | fprintf(stderr,"Unable to print to %s\n",mr_logfile);
|
---|
| 99 | }
|
---|
[3509] | 100 | fprintf(mr_flog,"\n");
|
---|
[1065] | 101 | va_end(args);
|
---|
[1054] | 102 |
|
---|
| 103 | }
|
---|
[3509] | 104 | return;
|
---|
[1054] | 105 | }
|
---|
[3509] | 106 |
|
---|