"Fossies" - the Fresh Open Source Software Archive

Member "minidlna-1.3.0/log.c" (24 Nov 2020, 4010 Bytes) of package /linux/privat/minidlna-1.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "log.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.1_vs_1.3.0.

    1 /* MiniDLNA media server
    2  * Copyright (C) 2008-2010 NETGEAR, Inc. All Rights Reserved.
    3  *
    4  * This file is part of MiniDLNA.
    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 2 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 along
   17  * with this program; if not, write to the Free Software Foundation, Inc.,
   18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   19  */
   20 #include "config.h"
   21 
   22 #include <stdlib.h>
   23 #include <stdio.h>
   24 #include <stdarg.h>
   25 #include <string.h>
   26 #include <time.h>
   27 
   28 #include "upnpglobalvars.h"
   29 #include "log.h"
   30 
   31 static FILE *log_fp = NULL;
   32 static const int _default_log_level = E_WARN;
   33 int log_level[L_MAX];
   34 
   35 const char *facility_name[] = {
   36     "general",
   37     "artwork",
   38     "database",
   39     "inotify",
   40     "scanner",
   41     "metadata",
   42     "http",
   43     "ssdp",
   44     "tivo",
   45     0
   46 };
   47 
   48 const char *level_name[] = {
   49     "off",                  // E_OFF
   50     "fatal",                // E_FATAL
   51     "error",                // E_ERROR
   52     "warn",                 // E_WARN
   53     "info",                 // E_INFO
   54     "debug",                // E_DEBUG
   55     "maxdebug",             // E_MAXDEBUG
   56     0
   57 };
   58 
   59 void
   60 log_close(void)
   61 {
   62     if (log_fp)
   63         fclose(log_fp);
   64 }
   65 
   66 void
   67 log_reopen(void)
   68 {
   69     if (log_path[0] && log_fp)
   70     {
   71         char logfile[1048];
   72         snprintf(logfile, sizeof(logfile), "%s/" LOGFILE_NAME, log_path);
   73         fclose(log_fp);
   74         log_fp = fopen(logfile, "a");
   75         DPRINTF(E_INFO, L_GENERAL, "Reopened log file\n");
   76     }
   77 }
   78 
   79 int find_matching_name(const char* str, const char* names[])
   80 {
   81     const char *start;
   82     int level, c;
   83 
   84     if (!str)
   85         return -1;
   86 
   87     start = strpbrk(str, ",=");
   88     c = start ? start - str : strlen(str);
   89     for (level = 0; names[level] != 0; level++) {
   90         if (!strncasecmp(names[level], str, c))
   91             return level;
   92     }
   93     return -1;
   94 }
   95 
   96 int
   97 log_init(const char *debug)
   98 {
   99     int i;
  100     FILE *fp = NULL;
  101 
  102     int level = find_matching_name(debug, level_name);
  103     int default_log_level = (level == -1) ? _default_log_level : level;
  104 
  105     for (i = 0; i < L_MAX; i++)
  106         log_level[i] = default_log_level;
  107 
  108     if (debug)
  109     {
  110         const char *rhs, *lhs, *nlhs;
  111         int level, facility;
  112 
  113         rhs = nlhs = debug;
  114         while (rhs && (rhs = strchr(rhs, '='))) {
  115             rhs++;
  116             level = find_matching_name(rhs, level_name);
  117             if (level == -1) {
  118                 DPRINTF(E_WARN, L_GENERAL, "unknown level in debug string: %s", debug);
  119                 continue;
  120             }
  121 
  122             lhs = nlhs;
  123             rhs = nlhs = strchr(rhs, ',');
  124             do {
  125                 if (*lhs==',') lhs++;
  126                 facility = find_matching_name(lhs, facility_name);
  127                 if (facility == -1) {
  128                     DPRINTF(E_WARN, L_GENERAL, "unknown debug facility in debug string: %s", debug);
  129                 } else {
  130                     log_level[facility] = level;
  131                 }
  132 
  133                 lhs = strpbrk(lhs, ",=");
  134             } while (*lhs && *lhs==',');
  135         }
  136     }
  137 
  138     if (log_path[0])
  139     {
  140         char logfile[1048];
  141         snprintf(logfile, sizeof(logfile), "%s/" LOGFILE_NAME, log_path);
  142         if (!(fp = fopen(logfile, "a")))
  143             return -1;
  144     }
  145     log_fp = fp;
  146 
  147     return 0;
  148 }
  149 
  150 void
  151 log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
  152 {
  153     va_list ap;
  154 
  155     if (level && level>log_level[facility] && level>E_FATAL)
  156         return;
  157 
  158     if (!log_fp)
  159         log_fp = stdout;
  160 
  161     // timestamp
  162     if (!GETFLAG(SYSTEMD_MASK))
  163     {
  164         time_t t;
  165         struct tm *tm;
  166         t = time(NULL);
  167         tm = localtime(&t);
  168         fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
  169                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
  170                 tm->tm_hour, tm->tm_min, tm->tm_sec);
  171     }
  172 
  173     if (level)
  174         fprintf(log_fp, "%s:%d: %s: ", fname, lineno, level_name[level]);
  175     else
  176         fprintf(log_fp, "%s:%d: ", fname, lineno);
  177 
  178     // user log
  179     va_start(ap, fmt);
  180     if (vfprintf(log_fp, fmt, ap) == -1)
  181     {
  182         va_end(ap);
  183         return;
  184     }
  185     va_end(ap);
  186 
  187     fflush(log_fp);
  188 
  189     if (level==E_FATAL)
  190         exit(-1);
  191 
  192     return;
  193 }