LCOV - code coverage report
Current view: top level - client - log_msg.c (source / functions) Hit Total Coverage
Test: lcov_coverage_final.info Lines: 16 16 100.0 %
Date: 2014-11-16 Functions: 3 3 100.0 %
Branches: 4 4 100.0 %

           Branch data     Line data    Source code
       1                 :            : /**
       2                 :            :  * @file    log_msg.c
       3                 :            :  *
       4                 :            :  * @brief   General logging routine that can write to stderr
       5                 :            :  *          and can take variable number of args.
       6                 :            :  *
       7                 :            :  *  Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
       8                 :            :  *  Copyright (C) 2009-2014 fwknop developers and contributors. For a full
       9                 :            :  *  list of contributors, see the file 'CREDITS'.
      10                 :            :  *
      11                 :            :  *  License (GNU General Public License):
      12                 :            :  *
      13                 :            :  *  This program is free software; you can redistribute it and/or
      14                 :            :  *  modify it under the terms of the GNU General Public License
      15                 :            :  *  as published by the Free Software Foundation; either version 2
      16                 :            :  *  of the License, or (at your option) any later version.
      17                 :            :  *
      18                 :            :  *  This program is distributed in the hope that it will be useful,
      19                 :            :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      20                 :            :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21                 :            :  *  GNU General Public License for more details.
      22                 :            :  *
      23                 :            :  *  You should have received a copy of the GNU General Public License
      24                 :            :  *  along with this program; if not, write to the Free Software
      25                 :            :  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
      26                 :            :  *  USA
      27                 :            :  */
      28                 :            : 
      29                 :            : #include "fwknop_common.h"
      30                 :            : #include "log_msg.h"
      31                 :            : #include <stdarg.h>
      32                 :            : 
      33                 :            : #define LOG_STREAM_STDERR   stderr                  /*!< Error and warning messages are redirected to stderr */
      34                 :            : #define LOG_STREAM_STDOUT   stdout                  /*!< Normal, info and debug messages are redirected to stdout */
      35                 :            : 
      36                 :            : typedef struct
      37                 :            : {
      38                 :            :     int verbosity;                                  /*!< Verbosity level (LOG_VERBOSITY_DEBUG...*/
      39                 :            : } log_ctx_t;
      40                 :            : 
      41                 :            : static log_ctx_t log_ctx;                           /*!< Structure to store the context of the module */
      42                 :            : 
      43                 :            : /**
      44                 :            :  * Set up the context for the log module.
      45                 :            :  *
      46                 :            :  * This function only initialize the verbosity level
      47                 :            :  */
      48                 :            : void
      49                 :       2530 : log_new(void)
      50                 :            : {
      51                 :       2530 :     log_ctx.verbosity = LOG_DEFAULT_VERBOSITY;
      52                 :       2530 : }
      53                 :            : 
      54                 :            : /**
      55                 :            :  * Destroy the context for the log module.
      56                 :            :  *
      57                 :            :  * This function is not used at the moment since the module does not open file
      58                 :            :  * which would require to be closed;
      59                 :            : void
      60                 :            : log_free(void)
      61                 :            : {
      62                 :            : }
      63                 :            :  */
      64                 :            : 
      65                 :            : /**
      66                 :            :  * Set the verbosity level for the current context of the log module.
      67                 :            :  * 
      68                 :            :  * @param level verbosity level to set
      69                 :            :  */
      70                 :            : void
      71                 :       2539 : log_set_verbosity(int level)
      72                 :            : {
      73                 :       2539 :     log_ctx.verbosity = level;
      74                 :       2539 : }
      75                 :            : 
      76                 :            : /**
      77                 :            :  * Log a message
      78                 :            :  *
      79                 :            :  * This function sends a message to the stream dedicated to the priority
      80                 :            :  * set. If the verbosity for the context is higher than the one used for
      81                 :            :  * the message, then the message is discarded.
      82                 :            :  * 
      83                 :            :  * @param level Verbosity level to used for the message.
      84                 :            :  * @param msg   Message to print
      85                 :            :  */
      86                 :            : void
      87                 :      19033 : log_msg(int level, char* msg, ...)
      88                 :            : {
      89                 :            :     va_list ap;
      90                 :            : 
      91         [ +  + ]:      19033 :     if (level <= log_ctx.verbosity)
      92                 :            :     {
      93                 :      18729 :         va_start(ap, msg);
      94                 :            :         
      95         [ +  + ]:      18729 :         switch (level)
      96                 :            :         {
      97                 :            :             case LOG_VERBOSITY_ERROR:
      98                 :            :             case LOG_VERBOSITY_WARNING:
      99                 :        921 :                 vfprintf(LOG_STREAM_STDERR, msg, ap);
     100                 :        921 :                 fprintf(LOG_STREAM_STDERR, "\n");
     101                 :            :                 break;
     102                 :            :             case LOG_VERBOSITY_NORMAL:
     103                 :            :             case LOG_VERBOSITY_INFO:
     104                 :            :             case LOG_VERBOSITY_DEBUG:
     105                 :            :             default : 
     106                 :      17808 :                 vfprintf(LOG_STREAM_STDOUT, msg, ap);
     107                 :      17808 :                 fprintf(LOG_STREAM_STDOUT, "\n");
     108                 :            :                 break;
     109                 :            :         }
     110                 :            : 
     111                 :      18729 :         va_end(ap);
     112                 :            :     }
     113                 :            :     else;
     114                 :      19033 : }
     115                 :            : 
     116                 :            : /***EOF***/

Generated by: LCOV version 1.10