LogConfigParser.cc

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

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1