LCOV - code coverage report
Current view: top level - server - log_msg.c (source / functions) Hit Total Coverage
Test: lcov_coverage_final.info Lines: 69 72 95.8 %
Date: 2014-11-16 Functions: 4 4 100.0 %
Branches: 41 46 89.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *****************************************************************************
       3                 :            :  *
       4                 :            :  * File:    log_msg.c
       5                 :            :  *
       6                 :            :  * Purpose: General logging routine that can write to syslog and/or stderr
       7                 :            :  *          and can take varibale number of args.
       8                 :            :  *
       9                 :            :  *  Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
      10                 :            :  *  Copyright (C) 2009-2014 fwknop developers and contributors. For a full
      11                 :            :  *  list of contributors, see the file 'CREDITS'.
      12                 :            :  *
      13                 :            :  *  License (GNU General Public License):
      14                 :            :  *
      15                 :            :  *  This program is free software; you can redistribute it and/or
      16                 :            :  *  modify it under the terms of the GNU General Public License
      17                 :            :  *  as published by the Free Software Foundation; either version 2
      18                 :            :  *  of the License, or (at your option) any later version.
      19                 :            :  *
      20                 :            :  *  This program is distributed in the hope that it will be useful,
      21                 :            :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      22                 :            :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      23                 :            :  *  GNU General Public License for more details.
      24                 :            :  *
      25                 :            :  *  You should have received a copy of the GNU General Public License
      26                 :            :  *  along with this program; if not, write to the Free Software
      27                 :            :  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
      28                 :            :  *  USA
      29                 :            :  *
      30                 :            :  *****************************************************************************
      31                 :            : */
      32                 :            : #include "fwknopd_common.h"
      33                 :            : #include "utils.h"
      34                 :            : #include "log_msg.h"
      35                 :            : 
      36                 :            : /* The default log facility (can be overridden via config file directive).
      37                 :            : */
      38                 :            : static int  syslog_fac      = LOG_DAEMON;
      39                 :            : 
      40                 :            : /* This value is or'ed with the log level on all logging calls. This allows
      41                 :            :  * for force log to stderr instead of syslog simply be setting this to the
      42                 :            :  * appropriate value (which is done at init_logging().
      43                 :            : */
      44                 :            : static int  static_log_flag = LOG_STDERR_ONLY;
      45                 :            : 
      46                 :            : /* The name to use for ID in log messages.  This defaults to fwknopd.
      47                 :            : */
      48                 :            : static char *log_name = NULL;
      49                 :            : 
      50                 :            : /* The value of the default verbosity used by the log module */
      51                 :            : static int verbosity = LOG_DEFAULT_VERBOSITY;
      52                 :            : 
      53                 :            : /* Free resources allocated for logging.
      54                 :            : */
      55                 :            : void
      56                 :       8092 : free_logging(void)
      57                 :            : {
      58         [ +  + ]:       8092 :     if(log_name != NULL)
      59                 :       2577 :         free(log_name);
      60                 :       8092 : }
      61                 :            : 
      62                 :            : /* Initialize logging sets the name used for syslog.
      63                 :            : */
      64                 :            : void
      65                 :       2594 : init_logging(fko_srv_options_t *opts) {
      66                 :       2594 :     char       *my_name = NULL;
      67                 :       2594 :     int         is_syslog = 0;
      68                 :            : 
      69                 :            :     /* In case this is a re-init.
      70                 :            :     */
      71                 :       2594 :     free_logging();
      72                 :            : 
      73                 :            :     /* Allocate memory for the log_name and set the my_name to point to the
      74                 :            :      * appropriate name. The name should already be set in the config struct
      75                 :            :      * but if it is not, fallback to the default as defined by 'MY_NAME'.
      76                 :            :     */
      77         [ +  - ]:       2594 :     if(opts->config[CONF_SYSLOG_IDENTITY] != NULL
      78         [ +  - ]:       2594 :       && opts->config[CONF_SYSLOG_IDENTITY][0] != '\0')
      79                 :            :     {
      80                 :       2594 :         my_name  = opts->config[CONF_SYSLOG_IDENTITY];
      81                 :       2594 :         log_name = calloc(1, strlen(opts->config[CONF_SYSLOG_IDENTITY])+1);
      82                 :       2594 :         is_syslog = 1;
      83                 :            :     }
      84                 :            :     else
      85                 :            :     {
      86                 :          0 :         my_name  = (char*)&MY_NAME;
      87                 :          0 :         log_name = calloc(1, strlen(MY_NAME)+1);
      88                 :            :     }
      89                 :            : 
      90         [ +  + ]:       2594 :     if(log_name == NULL)
      91                 :            :     {
      92                 :         11 :         fprintf(stderr, "Memory allocation error setting log_name!\n");
      93                 :         11 :         clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
      94                 :            :     }
      95                 :            : 
      96                 :            :     /* Set our name.
      97                 :            :     */
      98         [ +  - ]:       2583 :     if (is_syslog)
      99                 :       2583 :         strlcpy(log_name, my_name, strlen(opts->config[CONF_SYSLOG_IDENTITY])+1);
     100                 :            :     else
     101                 :          0 :         strlcpy(log_name, my_name, strlen(MY_NAME)+1);
     102                 :            : 
     103                 :       2583 :     static_log_flag = LOG_SYSLOG_ONLY;
     104                 :            : 
     105                 :            :     /* If we are running in the foreground or performing firewall operations,
     106                 :            :      * all logging will go to stderr.
     107                 :            :     */
     108         [ +  + ]:       2583 :     if(opts->foreground != 0
     109         [ +  + ]:        924 :             || opts->fw_flush != 0
     110                 :            :             || opts->fw_list != 0
     111         [ +  + ]:        906 :             || opts->fw_list_all != 0)
     112                 :       2567 :         static_log_flag = LOG_STDERR_ONLY;
     113                 :            : 
     114                 :            :     /* If the user forces syslog using --syslog-enable, we remove the
     115                 :            :      * LOG_WITHOUT_SYSLOG flag. It means all messages will go through syslog */
     116         [ +  + ]:       2583 :     if (opts->syslog_enable != 0)
     117                 :          1 :         static_log_flag &= ~LOG_WITHOUT_SYSLOG;
     118                 :            : 
     119                 :            :     /* Parse the log facility as specified in the config struct. If, for some
     120                 :            :      * reason, it is not, fac will already be set to LOG_DAEMON.
     121                 :            :     */
     122         [ +  - ]:       2583 :     if(opts->config[CONF_SYSLOG_FACILITY] != NULL
     123         [ +  - ]:       2583 :       && opts->config[CONF_SYSLOG_FACILITY][0] != '\0')
     124                 :            :     {
     125         [ +  + ]:       2583 :         if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_DAEMON"))
     126                 :       2574 :             syslog_fac = LOG_DAEMON;
     127         [ +  + ]:          9 :         else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL0"))
     128                 :          1 :             syslog_fac = LOG_LOCAL0;
     129         [ +  + ]:          8 :         else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL1"))
     130                 :          1 :             syslog_fac = LOG_LOCAL1;
     131         [ +  + ]:          7 :         else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL2"))
     132                 :          1 :             syslog_fac = LOG_LOCAL2;
     133         [ +  + ]:          6 :         else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL3"))
     134                 :          1 :             syslog_fac = LOG_LOCAL3;
     135         [ +  + ]:          5 :         else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL4"))
     136                 :          1 :             syslog_fac = LOG_LOCAL4;
     137         [ +  + ]:          4 :         else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL5"))
     138                 :          1 :             syslog_fac = LOG_LOCAL5;
     139         [ +  + ]:          3 :         else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL6"))
     140                 :          1 :             syslog_fac = LOG_LOCAL6;
     141         [ +  + ]:          2 :         else if(!strcasecmp(opts->config[CONF_SYSLOG_FACILITY], "LOG_LOCAL7"))
     142                 :          1 :             syslog_fac = LOG_LOCAL7;
     143                 :            :         else
     144                 :            :         {
     145                 :          1 :             fprintf(stderr, "Invalid SYSLOG_FACILITY setting '%s'\n",
     146                 :            :                     opts->config[CONF_SYSLOG_FACILITY]);
     147                 :          1 :             clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
     148                 :            :         }
     149                 :            :     }
     150                 :            : 
     151                 :       2582 :     verbosity = LOG_DEFAULT_VERBOSITY + opts->verbose;
     152                 :       2582 : }
     153                 :            : 
     154                 :            : /* Syslog message function.  It uses default set at intialization, and also
     155                 :            :  * takes variable args to accomodate printf-like formatting and expansion.
     156                 :            : */
     157                 :            : void
     158                 :     109535 : log_msg(int level, char* msg, ...)
     159                 :            : {
     160                 :            :     va_list ap, apse;
     161                 :            : 
     162                 :            :     /* Make sure the level is in the right range */
     163         [ +  + ]:     109535 :     if ((level & LOG_VERBOSITY_MASK) > verbosity)
     164                 :     109452 :         return;
     165                 :            : 
     166                 :     107817 :     va_start(ap, msg);
     167                 :            : 
     168                 :     107817 :     level |= static_log_flag;
     169                 :            : 
     170                 :            :     /* Print msg to stderr if the level was or'ed with LOG_STDERR
     171                 :            :     */
     172         [ +  + ]:     107817 :     if(LOG_STDERR & level)
     173                 :            :     {
     174                 :            :         /* Need to make a copy of our va_list so we don't screw
     175                 :            :          * up the message going to syslog after we print it to stderr.
     176                 :            :         */
     177                 :     107734 :         va_copy(apse, ap);
     178                 :            : 
     179                 :     107734 :         vfprintf(stderr, msg, apse);
     180                 :     107734 :         fprintf(stderr, "\n");
     181                 :     107734 :         fflush(stderr);
     182                 :            : 
     183                 :     107734 :         va_end(apse);
     184                 :            :     }
     185                 :            : 
     186                 :            :     /* If the message has not to be printed to the syslog, we return */
     187         [ +  + ]:     107817 :     if (LOG_WITHOUT_SYSLOG & level)
     188                 :            :     {
     189                 :     107734 :         va_end(ap);
     190                 :     107734 :         return;
     191                 :            :     }
     192                 :            : 
     193                 :            :     /* Remove the static log flags from the level */
     194                 :         83 :     level &= LOG_VERBOSITY_MASK;
     195                 :            : 
     196                 :            :     /* Send the message to syslog.
     197                 :            :     */
     198                 :         83 :     openlog(log_name, LOG_PID, syslog_fac);
     199                 :            : 
     200                 :            :     vsyslog(level, msg, ap);
     201                 :            : 
     202                 :         83 :     va_end(ap);
     203                 :            : }
     204                 :            : 
     205                 :            : /**
     206                 :            :  * Set the verbosity level for the current context of the log module.
     207                 :            :  *
     208                 :            :  * The verbosity levels used by the module are defined by the syslog module.
     209                 :            :  *
     210                 :            :  * @param level verbosity level to set (LOG_INFO, LOG_NOTICE ...)
     211                 :            :  */
     212                 :            : void
     213                 :       2582 : log_set_verbosity(int level)
     214                 :            : {
     215                 :       2582 :     verbosity = level;
     216                 :       2582 : }
     217                 :            : 
     218                 :            : /***EOF***/

Generated by: LCOV version 1.10