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: 2016-06-07 Functions: 3 3 100.0 %
Branches: 4 4 100.0 %

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

Generated by: LCOV version 1.10