LogConfigParser.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #include <cstdio>
00018 
00019 #include "../Log.h"
00020 #include "LogConfigParser.h"
00021 
00022 namespace oasys {
00023 
00024 //----------------------------------------------------------------------------
00025 LogConfigParser::LogConfigParser(const char* filename, 
00026                                  RuleSet*    rs,
00027                                  Option*     opts)
00028     : filename_(filename),
00029       rs_(rs),
00030       opts_(opts)
00031 {}
00032 
00033 //----------------------------------------------------------------------------
00034 int
00035 LogConfigParser::parse()
00036 {
00037     FILE* fp = fopen(filename_, "r");
00038     if (fp == 0) { 
00039         return;
00040     }
00041 
00042     char buf[256];
00043     int linenum = 0;
00044 
00045     while (!feof(fp)) 
00046     {
00047         if (fgets(buf, sizeof(buf), fp) > 0) 
00048         {
00049             char *line = buf;
00050             char *logpath;
00051             char *level;
00052             char *rest;
00053 
00054             ++linenum;
00055             logpath = line;
00056 
00057             // skip leading whitespace
00058             while (*logpath && isspace(*logpath)) 
00059             {
00060                 ++logpath;
00061             }
00062 
00063             if (! *logpath) 
00064             {
00065                 // blank line
00066                 continue;
00067             }
00068 
00069             // skip comment lines
00070             if (logpath[0] == '#') 
00071             {
00072                 continue;
00073             }
00074 
00075             // options
00076             if (logpath[0] == '%') 
00077             {
00078                 for (Option* opt = opts_; opt->option_str_ != 0; ++opt)
00079                 {
00080                     if (strstr(logpath, opt->option_str_) != 0) {
00081                         // If the option starts with no, then we
00082                         // disable the flag
00083                         if (opt->option_str[0] == 'n' &&
00084                             opt->option_str[1] == 'o')
00085                         {
00086                             flags_ &= ~opt->flag_value_;
00087                         }
00088                         else
00089                         {
00090                             flags_ |= opt->flag_value_;
00091                         }
00092                     }
00093                 }
00094                 continue;
00095             }
00096 
00097             // find the end of path and null terminate
00098             level = logpath;
00099             while (*level && !isspace(*level)) 
00100             {
00101                 ++level;
00102             }
00103             *level = '\0';
00104             ++level;
00105 
00106             // skip any other whitespace
00107             while (level && isspace(*level)) 
00108             {
00109                 ++level;
00110             }
00111 
00112             if (!level) 
00113             {
00114                 goto parse_err;
00115             }
00116 
00117             // null terminate the level
00118             rest = level;
00119             while (rest && !isspace(*rest)) 
00120             {
00121                 ++rest;
00122             }
00123 
00124             int priority = 1000;
00125             if (rest) 
00126             {
00127                 *rest = '\0';
00128                 ++rest;
00129                 // Handle glob expressions with optional priorities
00130                 if (logpath[0] == '=') 
00131                 {
00132                     priority = atoi(rest);
00133                     if (priority == 0) 
00134                     {
00135                         priority = 1000;
00136                     }
00137                 }
00138             }
00139 
00140             log_level_t threshold = Log::str2level(level);
00141             if (threshold == LOG_INVALID) 
00142             {
00143                 goto parse_err;
00144             }
00145 
00146 #ifdef NDEBUG
00147             if (threshold == LOG_DEBUG) 
00148             {
00149                 fprintf(stderr, "WARNING: debug level log rule for path %s "
00150                         "ignored in non-debugging build\n",
00151                         logpath);
00152                 continue;
00153             }
00154 #endif
00155             
00156             if (logpath[0] == '=')
00157             {
00158                 rs_->add_glob_rule(logpath + 1, threshold, priority);
00159             }
00160             else
00161             {
00162                 rs_->add_prefix_rule(logpath, threshold);
00163             }
00164         }
00165 
00166       parse_err:
00167         fprintf(stderr, "Error in log configuration %s line %d\n",
00168                 debug_path, linenum);
00169     } // while(feof...)
00170 
00171     fclose(fp);
00172 }
00173 
00174 } // namespace oasys

Generated on Thu Jun 7 12:54:28 2007 for DTN Reference Implementation by  doxygen 1.5.1