| [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
|
|---|
| [3628] | 15 |
|
|---|
| [1054] | 16 | #include <stdio.h>
|
|---|
| 17 | #include <stdarg.h>
|
|---|
| [1064] | 18 | #include <stdlib.h>
|
|---|
| [3628] | 19 | #include <unistd.h>
|
|---|
| 20 | #include <string.h>
|
|---|
| [1054] | 21 |
|
|---|
| 22 | static int mr_loglevel = 0;
|
|---|
| 23 | static char *mr_logfile = NULL;
|
|---|
| [3509] | 24 | static FILE *mr_flog = NULL;
|
|---|
| [1054] | 25 |
|
|---|
| [1065] | 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 |
|
|---|
| [3509] | 31 | /* Function allowing to change loglevel after init */
|
|---|
| 32 | void mr_msg_loglevel(int loglevel) {
|
|---|
| 33 |
|
|---|
| 34 | mr_loglevel = loglevel;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| [1054] | 37 | /* Cleanup function for messages */
|
|---|
| 38 | void mr_msg_close(void) {
|
|---|
| [3509] | 39 |
|
|---|
| 40 | int res = 0;
|
|---|
| 41 | if ((res = fclose(mr_flog)) != 0) {
|
|---|
| 42 | fprintf(stderr,"Unable to close %s\n",mr_logfile);
|
|---|
| 43 | }
|
|---|
| [1054] | 44 | free(mr_logfile);
|
|---|
| [3509] | 45 |
|
|---|
| [1054] | 46 | mr_logfile = NULL;
|
|---|
| [1065] | 47 | mr_loglevel = 0;
|
|---|
| [3509] | 48 | mr_flog = NULL;
|
|---|
| 49 | return;
|
|---|
| [1054] | 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /* Initialization function for messages */
|
|---|
| [3628] | 53 | void mr_msg_init(const char *logfile, int loglevel, int argc, char *argv[]) {
|
|---|
| 54 |
|
|---|
| 55 | int i=1;
|
|---|
| 56 |
|
|---|
| [2209] | 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 | }
|
|---|
| [3509] | 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 | }
|
|---|
| [1054] | 76 | }
|
|---|
| [3628] | 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 | }
|
|---|
| [3630] | 88 | i++;
|
|---|
| [3628] | 89 | }
|
|---|
| [1226] | 90 | mr_msg_loglevel(loglevel);
|
|---|
| [1054] | 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /*
|
|---|
| 94 | * Function that log a message. Not called directly
|
|---|
| [1061] | 95 | * but through other functions
|
|---|
| [1104] | 96 | * fmt needs to be just before ...
|
|---|
| [3509] | 97 | * level should be >0 to have file and line printed (real debug)
|
|---|
| [1133] | 98 | * If =0 it's an informative log message
|
|---|
| [1054] | 99 | */
|
|---|
| [3509] | 100 | void mr_msg_int(int level, int line, const char *file, char *function, const char *fmt, ...) {
|
|---|
| [1054] | 101 |
|
|---|
| [1064] | 102 | va_list args;
|
|---|
| [1054] | 103 |
|
|---|
| [3509] | 104 | if ((mr_logfile == NULL) || (mr_flog == NULL)) {
|
|---|
| [1054] | 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| [3509] | 108 | if (level <= mr_loglevel) {
|
|---|
| 109 | if (level > 0) {
|
|---|
| 110 | fprintf(mr_flog, "DBG%d: ", level);
|
|---|
| 111 | fprintf(mr_flog, "%s->%s#%d: ", file, function, line);
|
|---|
| 112 | } else {
|
|---|
| 113 | fprintf(mr_flog, "INFO: ");
|
|---|
| [1054] | 114 | }
|
|---|
| [1065] | 115 | va_start(args,fmt);
|
|---|
| [3509] | 116 | if (vfprintf(mr_flog, fmt, args) < 0) {
|
|---|
| [1054] | 117 | fprintf(stderr,"Unable to print to %s\n",mr_logfile);
|
|---|
| 118 | }
|
|---|
| [3509] | 119 | fprintf(mr_flog,"\n");
|
|---|
| [1065] | 120 | va_end(args);
|
|---|
| [1054] | 121 |
|
|---|
| 122 | }
|
|---|
| [3509] | 123 | return;
|
|---|
| [1054] | 124 | }
|
|---|
| [3509] | 125 |
|
|---|