root/libutil/logging.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. logging_open
  2. logging_printf
  3. logging_arguments

   1 /*
   2  * Copyright (c) 2011 Tama Communications Corporation
   3  *
   4  * This file is part of GNU GLOBAL.
   5  *
   6  * This program is free software: you can redistribute it and/or modify
   7  * it under the terms of the GNU General Public License as published by
   8  * the Free Software Foundation, either version 3 of the License, or
   9  * (at your option) any later version.
  10  * 
  11  * This program is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU General Public License for more details.
  15  * 
  16  * You should have received a copy of the GNU General Public License
  17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18  */
  19 
  20 #ifdef HAVE_CONFIG_H
  21 #include <config.h>
  22 #endif
  23 #ifdef STDC_HEADERS
  24 #include <stdlib.h>
  25 #endif
  26 #ifdef HAVE_UNISTD_H
  27 #include <unistd.h>
  28 #endif
  29 
  30 #include "gparam.h"
  31 #include "logging.h"
  32 
  33 /** @file
  34 
  35 Logging utility:
  36 
  37 @code
  38 +------------------------------------
  39 |main(int argc, char **argv)
  40 |{
  41 |       logging_printf("Start\n");
  42 |       logging_arguments(argc, argv);
  43 |       ...
  44 @endcode
  45 
  46 @code{.sh}
  47 % setenv GTAGSLOGGING /tmp/log
  48 % global -x main
  49 % cat /tmp/log
  50 Start
  51 0: |global|
  52 1: |-x|
  53 2: |main|
  54 % _
  55 @endcode
  56 
  57 See logging_printf() for more details.
  58 */
  59 static FILE *lp;
  60 static int ignore;
  61 
  62 static int
  63 logging_open(void)
  64 {
  65         char *logfile = getenv("GTAGSLOGGING");
  66 
  67         if (logfile == NULL || (lp = fopen(logfile, "a")) == NULL)
  68                 return -1;
  69         return 0;
  70 }
  71 /**
  72  * logging_printf: print a message into the logging file.
  73  *
  74  *      @param[in]      s       @NAME{printf} style format (fmt) string
  75  *
  76  *      @remark
  77  *              Log messages are appended to the logging file; which is opened using 
  78  *              @CODE{'fopen(xx, \"a\")'} on the first call to logging_printf() or
  79  *              logging_arguments(). <br>
  80  *              The logging file's filename should be in the OS environment variable
  81  *              @FILE{GTAGSLOGGING}. <br>
  82  *              If @FILE{GTAGSLOGGING} is not setup or the logging file cannot be
  83  *              opened, logging is disabled; logging_printf() and logging_arguments()
  84  *              then do nothing.
  85  *
  86  *      @note The logging file stays @EMPH{open} for the life of the progam.
  87  */
  88 void
  89 logging_printf(const char *s, ...)
  90 {
  91         va_list ap;
  92 
  93         if (ignore)
  94                 return;
  95         if (lp == NULL && logging_open() < 0) {
  96                 ignore = 1;
  97                 return;
  98         }
  99         va_start(ap, s);
 100         vfprintf(lp, s, ap);
 101         va_end(ap);
 102 }
 103 
 104 /**
 105  * logging_arguments: print arguments into the logging file.
 106  *
 107  *      @param[in]      argc
 108  *      @param[in]      argv
 109  *
 110  *      @par Uses:
 111  *              logging_printf()
 112  */
 113 void
 114 logging_arguments(int argc, char **argv)
 115 {
 116         int i;
 117         char buf[MAXPATHLEN];
 118 
 119         if (getcwd(buf, sizeof(buf)))
 120                 logging_printf("In |%s|\n", buf);
 121         for (i = 0; i < argc; i++) {
 122                 if (ignore)
 123                         break;
 124                 logging_printf("%d: |%s|\n", i, argv[i]);
 125         }
 126 }

/* [previous][next][first][last][top][bottom][index][help] */