Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

globalutils.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 #include <stdlib.h>  // getenv
00003 #include <string.h>  // strcmp, strcpy, strcat
00004 //#include <stdio.h>   // fprintf
00005 #if defined(_WINNT) || defined(_Windows)
00006 #include <time.h>    // _tzset -- to set the timezone related to GMT
00007 #endif
00008 
00009 #include <worm_environ.h>
00010 #include <worm_exceptions.h>
00011 #include <logger.h>
00012 #include <comfile.h>
00013 #include <globalutils.h>
00014 
00015 //---------------------------------------------------------------------------
00016 // Borland pragma, ignored by other compilers
00017 #pragma package(smart_init)
00018 
00019 //---------------------------------------------------------------------------
00020 
00021 /*
00022 ** STATIC VARIABLES
00023 */
00024 PROGRAM_NAME TGlobalUtils::ProgramName = { "Uninitialized" };
00025 WORM_INSTALLATION_ID TGlobalUtils::ThisInstallation = WORM_INSTALLATION_INVALID;
00026 WORM_MODULE_ID TGlobalUtils::ThisModuleId = WORM_MODULE_INVALID;
00027 LOG_DIRECTORY TGlobalUtils::HomeDirectory;
00028 char TGlobalUtils::Version[12];
00029 long TGlobalUtils::HeartBeatInt = -1;
00030 
00031 // don't set this to WORM_LOG_NONE here
00032 WORM_LOGGING_LEVEL TGlobalUtils::LogLevel = WORM_LOG_ERRORS;
00033 bool TGlobalUtils::DoLogFile = true;
00034 
00035 std::vector<std::string> TGlobalUtils::ConfigFiles;
00036 int  TGlobalUtils::ConfigFileCount  = 0;
00037 
00038 INSTALLATION_MAP TGlobalUtils::InstallIds;
00039 RING_MAP TGlobalUtils::RingIds;
00040 MODULE_MAP TGlobalUtils::ModuleIds;
00041 MESSAGETYPE_MAP TGlobalUtils::MessageTypeIds;
00042 
00043 volatile bool TGlobalUtils::TerminateFlag = false;
00044 
00045 
00046 //---------------------------------------------------------------------------
00047 
00048 
00049 //---------------------------------------------------------------------------
00050 /*
00051 **  p_programname is expected as argv[0], so the path and extension will be
00052 **  stripped off.
00053 */
00054 TGlobalUtils::TGlobalUtils( char* p_programname )
00055 {
00056    // only one bite at the configuration apple
00057    if ( ConfigState == WORM_STAT_NOTINIT )
00058    {
00059       if ( p_programname == NULL )
00060       {
00061          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00062                        , "TGlobalUtils(): program name is NULL\n"
00063                        );
00064       }
00065 
00066 
00067       // Get the config directory from the environment
00068       //
00069       GEN_FILENAME _configdir
00070                  , _configfilename
00071                  ;
00072 
00073       if ( GetEnvironmentValue("WORM_HOME_KEY") == NULL )
00074       {
00075          if ( GetEnvironmentValue("EW_HOME_KEY") == NULL )
00076          {
00077             strcpy( HomeDirectory, "" );
00078          }
00079          else
00080          {
00081             strcpy( HomeDirectory, GetEnvironmentValue("EW_HOME_KEY") );
00082          }
00083       }
00084       else
00085       {
00086          strcpy( HomeDirectory, GetEnvironmentValue("WORM_HOME_KEY") );
00087       }
00088 
00089 
00090       if ( GetEnvironmentValue("WORM_VERSION_KEY") == NULL )
00091       {
00092          if ( GetEnvironmentValue("EW_VERSION_KEY") == NULL )
00093          {
00094             strcpy( Version, "" );
00095          }
00096          else
00097          {
00098             strcpy( Version, GetEnvironmentValue("EW_VERSION_KEY") );
00099          }
00100       }
00101       else
00102       {
00103          strcpy( Version, GetEnvironmentValue("WORM_HOME_KEY") );
00104       }
00105 
00106 
00107 
00108       if ( GetEnvironmentValue( WORM_CONFIG_DIR ) == NULL )
00109       {
00110          if ( GetEnvironmentValue( EW_CONFIG_DIR ) == NULL )
00111          {
00112             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00113                           , "TGlobalUtils(): Neither environment variable <%s> nor <%s> are defined\n                using current directory"
00114                           , WORM_CONFIG_DIR
00115                           , EW_CONFIG_DIR
00116                           );
00117             strcpy( _configdir, "." );
00118          }
00119          else
00120          {
00121             strcpy( _configdir, GetEnvironmentValue( EW_CONFIG_DIR ) );
00122          }
00123       }
00124       else
00125       {
00126          strcpy( _configdir, GetEnvironmentValue( WORM_CONFIG_DIR ) );
00127       }
00128 
00129       int _len = strlen( _configdir );
00130 #if defined(_WINNT) || defined(_Windows)
00131       if( 0 < _len && _configdir[_len-1] != '\\' )
00132       {
00133          strcat( _configdir, "\\" );
00134       }
00135 #else
00136       if( 0 < _len && _configdir[_len-1] != '/' )
00137       {
00138          strcat( _configdir, "/" );
00139       }
00140 #endif
00141 
00142          // Get the config file names, these are later used by
00143          // LoadFiles() and CheckConfig()
00144          //
00145          // NOTE: if the worm configuration files are not specified in
00146          //       the environment, then if the EW params directory is
00147          //       in the environment this is assumed to be an
00148          //       earthworm installation and the earthworm fixed filenames
00149          //       are used.
00150          //
00151          if ( GetEnvironmentValue(WORM_CONFIG_GLOBAL) == NULL )
00152          {
00153             if ( GetEnvironmentValue(EW_CONFIG_DIR) != NULL )
00154             {
00155                 strcpy( _configfilename, _configdir );
00156                 strcat( _configfilename, "earthworm_global.d" );
00157                 ConfigFiles.push_back(_configfilename);
00158                 ConfigFileCount++;
00159             }
00160          }
00161          else
00162          {
00163              strcpy( _configfilename, _configdir );
00164              strcat( _configfilename, GetEnvironmentValue(WORM_CONFIG_GLOBAL) );
00165              ConfigFiles.push_back(_configfilename);
00166              ConfigFileCount++;
00167          }
00168 
00169          if ( GetEnvironmentValue(WORM_CONFIG_SITE) == NULL )
00170          {
00171             if ( GetEnvironmentValue(EW_CONFIG_DIR) != NULL )
00172             {
00173                 strcpy( _configfilename, _configdir );
00174                 strcat( _configfilename, "earthworm.d" );
00175                 ConfigFiles.push_back(_configfilename);
00176                 ConfigFileCount++;
00177             }
00178          }
00179          else
00180          {
00181              strcpy( _configfilename, _configdir );
00182              strcat( _configfilename, GetEnvironmentValue(WORM_CONFIG_SITE) );
00183              ConfigFiles.push_back(_configfilename);
00184              ConfigFileCount++;
00185          }
00186 
00187          char* _ptr;
00188 
00189          // Skip over path if included
00190 #if defined(_WINNT) || defined(_Windows)
00191          if ( GetEnvironmentValue(WORM_TIME_ZONE) != NULL ) _tzset();
00192 
00193          if ((_ptr = strrchr(p_programname, '\\')) != NULL)
00194 #else
00195          if ((_ptr = strrchr(p_programname, '/')) != NULL)
00196 #endif
00197          {
00198             strcpy(ProgramName, _ptr + 1);
00199          }
00200          else
00201          {
00202             strcpy(ProgramName, p_programname);
00203          }
00204 
00205          // eliminate extension if present
00206          _ptr = strchr(ProgramName, '.');
00207          if (_ptr != NULL)
00208          {
00209             *_ptr = '\0';
00210          }
00211       }
00212 
00213       LoadFiles();
00214 
00215 
00216       // Get the institution id from environment
00217       char * _installname = NULL;
00218 
00219       short _installsource = 0;
00220 
00221       if ( (_installname = GetEnvironmentValue(WORM_INSTALLATION_KEY)) == NULL )
00222       {
00223          _installsource = 1;
00224          _installname = GetEnvironmentValue(EW_INSTALLATION_KEY);
00225       }
00226 
00227       if ( _installname == NULL )
00228       {
00229          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00230                        , "TGlobalUtils(): Neither environment variable <%s> nor <%s> is defined\n"
00231                        , WORM_INSTALLATION_KEY
00232                        , EW_INSTALLATION_KEY
00233                        );
00234       }
00235       else
00236       {
00237          if ( strlen(_installname) == 0 )
00238          {
00239             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00240                           , "TGlobalUtils(): Environment variable <%s> is defined but empty\n"
00241                           , ( _installsource == 0 ? WORM_INSTALLATION_KEY : EW_INSTALLATION_KEY )
00242                           );
00243          }
00244          else
00245          {
00246             if ( (ThisInstallation = LookupInstallationId(_installname)) == WORM_INSTALLATION_INVALID)
00247             {
00248                TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00249                              , "TGlobalUtils(): Installation name <%s> not found in global configuration file\n"
00250                              , _installname
00251                              );
00252             }
00253          }
00254       }
00255 
00256 }
00257 //---------------------------------------------------------------------------
00258 char* TGlobalUtils::GetEnvironmentValue( const char* p_key )
00259 {
00260    return getenv(p_key);
00261 }
00262 //---------------------------------------------------------------------------
00263 HANDLE_STATUS TGlobalUtils::HandleConfigLine( ConfigSource * p_parser )
00264 {
00265    HANDLE_STATUS r_status = HANDLER_USED;
00266 
00267    try
00268    {
00269 
00270       do
00271       {
00272          if( p_parser->Its( "TruncLogOnStartup" ) )
00273          {
00274             TLogger::TruncateOnOpen();
00275          }
00276 
00277          //                    WORM                           EARTHWORM
00278          if( p_parser->Its( "ThisModule" ) || p_parser->Its( "MyModuleId" ) )
00279          {
00280             char * _token = p_parser->String();
00281 
00282             if ( strlen(_token) == 0 )
00283             {
00284                throw worm_exception("missing both <ThisModule> and <MyModuleId> values");
00285             }
00286 
00287             if ( (ThisModuleId = LookupModuleId(_token)) == WORM_MODULE_INVALID )
00288             {
00289                char _emsg[70];
00290                sprintf( _emsg
00291                       , "ModuleId '%s' not found in lookup tables for <ThisModule/MyModuleId> command"
00292                       , _token
00293                       );
00294                throw worm_exception(_emsg);
00295             }
00296 
00297             continue;
00298          }
00299 
00300          if ( p_parser->Its("HeartBeatInt") )
00301          {
00302             if ( (HeartBeatInt = p_parser->Long()) == ConfigSource::INVALID_LONG )
00303             {
00304                throw worm_exception("missing <HeartBeatInt> value");
00305             }
00306             continue;
00307          }
00308 
00309          if ( p_parser->Its("NoLogFile") )
00310          {
00311             DoLogFile = false;
00312             continue;
00313          }
00314 
00315          // Allow the global logging level (set through LoadFiles() --> ParseCommand())
00316          // to be overridden by the <module>.d file
00317          //
00318          if ( p_parser->Its("LogLevel") )
00319          {
00320             int _level;
00321             if ( (_level = p_parser->Int()) == ConfigSource::INVALID_INT )
00322             {
00323                throw worm_exception("missing <LogLevel> value");
00324             }
00325 
00326             LogLevel = (WORM_LOGGING_LEVEL)_level;
00327             continue;
00328          }
00329 
00330          r_status = HANDLER_UNUSED;
00331 
00332       } while( false );
00333    }
00334    catch( worm_exception & _we )
00335    {
00336       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00337                     , "TGlobalUtils::HandleConfigLine(): %s from line\n%s\n"
00338                     , _we.what()
00339                     , p_parser->GetCurrentLine()
00340                     );
00341    }
00342 
00343    return r_status;
00344 }
00345 //---------------------------------------------------------------------------
00346 void TGlobalUtils::CheckConfig()
00347 {
00348    // After all files are closed, check init flags for missed commands
00349 
00350    bool _command_missed = false;
00351    if (   RingIds.size() == 0
00352        || ModuleIds.size() == 0
00353        || MessageTypeIds.size() == 0
00354        || InstallIds.size() == 0
00355 //       || ThisModuleId == WORM_MODULE_INVALID
00356       )
00357    {
00358       ConfigState = WORM_STAT_BADSTATE;
00359       _command_missed = true;
00360    }
00361    if ( _command_missed )
00362    {
00363          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00364                        , "TGlobalUtils::CheckConfig(): found no"
00365                        );
00366       if ( RingIds.size() == 0 )
00367          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR, " <Ring>" );
00368       if ( ModuleIds.size() == 0 )
00369          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR, " <Module>" );
00370       if ( MessageTypeIds.size() == 0 )
00371          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR, " <Message>" );
00372       if ( InstallIds.size() == 0 )
00373          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR, " <Installation>" );
00374 //      if ( ThisModuleId == WORM_MODULE_INVALID )
00375 //         TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR, " <ThisModule/MyModuleId>" );
00376 
00377          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR, "line(s) in file(s):\n" );
00378       for( int _f = 0 ; _f < ConfigFileCount ; _f++ )
00379       {
00380          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00381                        , "   <%s>\n"
00382                        , ConfigFiles[_f].c_str()
00383                        );
00384       }
00385    }
00386 
00387    if ( ThisInstallation == WORM_INSTALLATION_INVALID )
00388    {
00389       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00390                     , "TGlobalUtils::CheckConfig(): Neither <%s> nor <%s> environment value set\n"
00391                     , WORM_INSTALLATION_KEY
00392                     , EW_INSTALLATION_KEY
00393                     );
00394       ConfigState = WORM_STAT_BADSTATE;
00395    }
00396 
00397    if ( strlen(HomeDirectory) == 0 )
00398    {
00399       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00400                     , "TGlobalUtils::CheckConfig(): Neither <%s> nor <%s> environment value set\n"
00401                     , WORM_HOME_KEY
00402                     , EW_HOME_KEY
00403                     );
00404       ConfigState = WORM_STAT_BADSTATE;
00405    }
00406 
00407    if ( strlen(Version) == 0 )
00408    {
00409       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00410                     , "TGlobalUtils::CheckConfig(): Neither <%s> nor <%s> environment value set\n"
00411                     , WORM_VERSION_KEY
00412                     , EW_VERSION_KEY
00413                     );
00414       ConfigState = WORM_STAT_BADSTATE;
00415    }
00416 }
00417 //---------------------------------------------------------------------------
00418 bool TGlobalUtils::ParseLookupLine( const char * p_filename, ConfigSource & p_parser )
00419 {
00420    bool r_foundit = false;
00421 
00422    std::string _name;
00423    WORM_RING_ID _ringkey;
00424    int _wrkid;
00425 
00426    // this is a loop solely to allow use of the continue statement
00427    // to make the code cleaner
00428 
00429    do
00430    {
00431       if ( p_parser.Its("Installation") )
00432       {
00433          // get installation key
00434          _name = p_parser.String();
00435 
00436          if ( _name.size() == 0 )
00437          {
00438             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00439                           , "TGlobalUtils::ParseLookupLine(): Installation name missing in <%s>\n"
00440                           , p_filename
00441                           );
00442             continue; // next line
00443          }
00444 
00445          if ( MAX_INSTALLNAME_LEN < _name.size() )
00446          {
00447             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00448                           , "TGlobalUtils::ParseLookupLine(): Installation name <%s> too long in <%s> max=%d chars\n"
00449                           , _name.c_str()
00450                           , p_filename
00451                           , MAX_INSTALLNAME_LEN
00452                           );
00453             continue; // next line
00454          }
00455 
00456          if ( (_wrkid = p_parser.Int()) == ConfigSource::INVALID_INT )
00457          {
00458             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00459                           , "TGlobalUtils::ParseLookupLine(): Installation <%s>'s id missing in <%s>\n"
00460                           , _name.c_str()
00461                           , p_filename
00462                           );
00463             continue; // next line
00464          }
00465 
00466          if ( 0 < InstallIds.count(_name) )
00467          {
00468             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00469                           , "TGlobalUtils::ParseLookupLine(): Duplicate entry for Installation key <%s>\nin file %s\nvalues old: %d new: %d, %s\n"
00470                           , _name.c_str()
00471                           , p_filename
00472                           , (int)InstallIds[_name]
00473                           , (int)_wrkid
00474                           , "new setting ignored"
00475                           );
00476             continue; // next line
00477          }
00478 
00479          InstallIds[_name] = (WORM_INSTALLATION_ID)_wrkid;
00480          r_foundit = true;
00481          continue; // next line
00482       }
00483 
00484       // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00485 
00486       if ( p_parser.Its("Ring") )
00487       {
00488          // get ring name
00489          _name = p_parser.String();
00490 
00491          if ( _name.size() == 0 )
00492          {
00493             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00494                           , "TGlobalUtils::ParseLookupLine(): Ring name missing in <%s>\n"
00495                           , p_filename
00496                           );
00497             continue; // next line
00498          }
00499 
00500          if ( MAX_RINGNAME_LEN < _name.size() )
00501          {
00502             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00503                           , "TGlobalUtils::ParseLookupLine(): Ring name <%s> too long in <%s> max=%d chars\n"
00504                           , _name.c_str()
00505                           , p_filename
00506                           , MAX_RINGNAME_LEN
00507                           );
00508             continue; // next line
00509          }
00510 
00511          if ( (_ringkey = p_parser.Long()) == ConfigSource::INVALID_LONG )
00512          {
00513             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00514                           , "TGlobalUtils::ParseLookupLine(): Ring <%s>'s id missing in <%s>\n"
00515                           , _name.c_str()
00516                           , p_filename
00517                           );
00518             continue; // next line
00519          }
00520 
00521          if ( 0 < RingIds.count(_name) )
00522          {
00523             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00524                           , "TGlobalUtils::ParseLookupLine(): Duplicate entry for Ring key <%s>\nin file %s\nvalues old: %d new: %d, %s\n"
00525                           , _name.c_str()
00526                           , p_filename
00527                           , (int)RingIds[_name]
00528                           , (int)_ringkey
00529                           , "new setting ignored"
00530                           );
00531             continue; // next line
00532          }
00533 
00534          RingIds[_name] = (WORM_RING_ID)_ringkey;
00535          r_foundit = true;
00536          continue; // next line
00537       }
00538 
00539       // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00540 
00541       if ( p_parser.Its("Module") )
00542       {
00543          _name = p_parser.NextToken(); // module name
00544 
00545          if ( _name.size() == 0 )
00546          {
00547             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00548                           , "TGlobalUtils::ParseLookupLine(): Module name missing in <%s>\n"
00549                           , p_filename
00550                           );
00551             continue; // next line
00552          }
00553 
00554          if ( MAX_MODNAME_LEN < _name.size() )
00555          {
00556             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00557                           , "TGlobalUtils::ParseLookupLine(): Module name <%s> too long in <%s> max=%d chars\n"
00558                           , _name.c_str()
00559                           , p_filename
00560                           , MAX_MODNAME_LEN
00561                           );
00562             continue; // next line
00563          }
00564 
00565          if ( (_wrkid = p_parser.Int()) == ConfigSource::INVALID_INT )
00566          {
00567             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00568                           , "TGlobalUtils::ParseLookupLine(): Module <%s>'s id missing in <%s>\n"
00569                           , _name.c_str()
00570                           , p_filename
00571                           );
00572             continue; // next line
00573          }
00574 
00575          if ( 0 < ModuleIds.count(_name) )
00576          {
00577             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00578                           , "TGlobalUtils::ParseLookupLine(): Duplicate entry for Module key <%s>\nin file %s\nvalues old: %d new: %d, %s\n"
00579                           , _name.c_str()
00580                           , p_filename
00581                           , (int)ModuleIds[_name]
00582                           , (int)_wrkid
00583                           , "new setting ignored"
00584                           );
00585             continue; // next line
00586          }
00587 
00588          ModuleIds[_name] = (WORM_MODULE_ID)_wrkid;
00589          r_foundit = true;
00590          continue; // next line
00591       }
00592 
00593       // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00594 
00595       if ( p_parser.Its("Message") )
00596       {
00597          _name = p_parser.NextToken(); // module name
00598 
00599          if ( _name.size() == 0 )
00600          {
00601             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00602                           , "TGlobalUtils::ParseLookupLine(): Message type name missing in <%s>\n"
00603                           , p_filename
00604                           );
00605             continue; // next line
00606          }
00607 
00608          if ( MAX_MSGTYPENAME_LEN < _name.size() )
00609          {
00610             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00611                           , "TGlobalUtils::ParseLookupLine(): Message type name <%s> too long in <%s> max=%d chars\n"
00612                           , _name.c_str()
00613                           , p_filename
00614                           , MAX_MSGTYPENAME_LEN
00615                           );
00616             continue; // next line
00617          }
00618 
00619          if ( (_wrkid = p_parser.Int()) == ConfigSource::INVALID_INT )
00620          {
00621             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00622                           , "TGlobalUtils::ParseLookupLine(): Message type <%s>'s id missing in <%s>\n"
00623                           , _name.c_str()
00624                           , p_filename
00625                           );
00626             continue; // next line
00627          }
00628 
00629          if ( 0 < MessageTypeIds.count(_name) )
00630          {
00631             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00632                           , "TGlobalUtils::ParseLookupLine(): Duplicate entry for Message type <%s>\nin file %s\nvalues old: %d new: %d, %s\n"
00633                           , _name.c_str()
00634                           , p_filename
00635                           , (int)MessageTypeIds[_name]
00636                           , (int)_wrkid
00637                           , "new setting ignored"
00638                           );
00639             continue; // next line
00640          }
00641 
00642          MessageTypeIds[_name] = (WORM_MSGTYPE_ID)_wrkid;
00643          r_foundit = true;
00644          continue; // next line
00645       }
00646 
00647 /*
00648       if ( p_parser.Its("ThisInstallId") )
00649       {
00650          r_foundit = true;
00651 
00652          _name = p_parser.String();
00653 
00654          if ( (ThisInstallation = LookupInstallationId(_name.c_str())) == WORM_INSTALLATION_INVALID )
00655          {
00656             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00657                           , "TGlobalUtils::ParseLookupLine(): <ThisInstallId> value missing in <%s> \n%s\n"
00658                           , p_filename
00659                           , _name.c_str()
00660                           );
00661             continue; // next line
00662          }
00663 
00664          continue; // next line
00665       }
00666 */
00667 
00668       if ( p_parser.Its("LogLevel") )
00669       {
00670          // Although standard practice directs that TGlobalUtils
00671          // should be instantiated and initialized (thus arriving here
00672          // to set the logging level at the global setting)
00673          // before the module reads its own configuration file
00674          // (possibly overriding the global setting), some might
00675          // inappropriately call the local configuration before the global.
00676          // Thus the reason for WORM_LOG_MU, which is used to distinguish
00677          // "haven't seen it yet" from "no logging" (WORM_LOG_NONE).
00678          //
00679          // Don't set debug level from here if previously set from the
00680          // the <module>.d file [through HandleConfigLine()]
00681          r_foundit = true;
00682          if ( LogLevel != WORM_LOG_MU )
00683          {
00684             int _level;
00685             if ( (_level = p_parser.Int()) == ConfigSource::INVALID_INT )
00686             {
00687                TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00688                              , "TGlobalUtils::ParseLookupLine(): missing <LogLevel> value in <%s>\n"
00689                              , p_filename
00690                              );
00691                continue;
00692             }
00693             LogLevel = (WORM_LOGGING_LEVEL)_level;
00694          }
00695          continue;
00696       }
00697 
00698    }
00699    while( false );
00700 
00701    return r_foundit;
00702 }
00703 //---------------------------------------------------------------------------
00704 
00705 void TGlobalUtils::LoadFiles()
00706 {
00707    if ( ConfigState != WORM_STAT_SUCCESS )
00708    {
00709 
00710       TComFileParser _parser;
00711 
00712       // Loop thru the specified configuration files
00713 
00714       for( int _file = 0 ; _file < ConfigFileCount ; _file++ )
00715       {
00716 
00717          if ( ! _parser.Open(ConfigFiles[_file].c_str()) )
00718          {
00719             TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00720                           , "TGlobalUtils::LoadFiles(): Error opening config file\n%s\n"
00721                           , ConfigFiles[_file].c_str()
00722                           );
00723             continue; // try next file
00724          }
00725 
00726          bool _reading = true;
00727 
00728          do
00729          {
00730 
00731             int _read_status = _parser.ReadLine();
00732 
00733             switch( _read_status )
00734             {
00735               case COMFILE_ERROR:
00736                    {
00737                       // Error
00738                       char * _errtext;
00739                       _parser.Error( &_errtext );
00740                       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00741                                     , "TGlobalUtils::LoadFiles(): Error reading line\n%s\nfrom config file: %s\n"
00742                                     , _errtext
00743                                     , ConfigFiles[_file].c_str()
00744                                     );
00745 //                      _file = ConfigFileCount; // leave [outer] for() loop
00746                    }
00747                    _reading = false;
00748                    break; // leave [inner] while() loop (may attempt subsequent file)
00749 
00750               case COMFILE_EOF:
00751 
00752                    _reading = false;
00753                    break;
00754 
00755               case 0: // empty line
00756                    break;
00757 
00758               default:
00759                    // line read from file okay, get command tag string
00760                    //
00761                    _parser.NextToken();
00762 
00763                    if ( ! _parser.IsTokenNull() )
00764                    {
00765                       if ( ! ParseLookupLine( ConfigFiles[_file].c_str(), _parser ) )
00766                       {
00767                          TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00768                                        , "TGlobalUtils::LoadFiles(): Invalid command line in file: %s\n%s\n"
00769                                        , ConfigFiles[_file].c_str()
00770                                        , _parser.GetCurrentLine()
00771                                        );
00772                          _reading = false; // on error, leave the loop
00773                       }
00774                    }
00775                    break;
00776             }   // read status
00777          } while( _reading );  // each line in opened file
00778 
00779          _parser.Close();
00780 
00781       } // each file
00782 
00783    } // not yet configured
00784 }
00785 //---------------------------------------------------------------------------
00786 const WORM_INSTALLATION_ID TGlobalUtils::LookupInstallationId( const char* p_name )
00787 {
00788    WORM_INSTALLATION_ID r_install = WORM_INSTALLATION_INVALID;
00789    if ( p_name == NULL )
00790    {
00791       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00792                     , "TGlobalUtils::LookupInstallationId(): requested name is NULL\n"
00793                     );
00794    }
00795    else
00796    {
00797       if ( 0 < InstallIds.count(p_name) )
00798       {
00799          r_install = InstallIds[p_name];
00800       }
00801    }
00802    return r_install;
00803 }
00804 //---------------------------------------------------------------------------
00805 const WORM_RING_ID TGlobalUtils::LookupRingKey( const char* p_name )
00806 {
00807    WORM_RING_ID r_rkey = WORM_RING_INVALID;
00808    if ( p_name == NULL )
00809    {
00810       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00811                     , "TGlobalUtils::LookupRingKey(): requested name is NULL\n"
00812                     );
00813    }
00814    else
00815    {
00816       if ( 0 < RingIds.count(p_name) )
00817       {
00818          r_rkey = RingIds[p_name];
00819       }
00820    }
00821    return r_rkey;
00822 }
00823 //---------------------------------------------------------------------------
00824 const WORM_MODULE_ID TGlobalUtils::LookupModuleId( const char* p_name )
00825 {
00826    WORM_MODULE_ID r_mod = WORM_MODULE_INVALID;
00827    if ( p_name == NULL )
00828    {
00829       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00830                     , "TGlobalUtils::LookupModuleId(): requested name is NULL\n"
00831                     );
00832    }
00833    else
00834    {
00835       if ( 0 < ModuleIds.count(p_name) )
00836       {
00837          r_mod = ModuleIds[p_name];
00838       }
00839    }
00840    return r_mod;
00841 }
00842 //---------------------------------------------------------------------------
00843 const WORM_MSGTYPE_ID TGlobalUtils::LookupMessageTypeId( const char* p_name )
00844 {
00845    WORM_MSGTYPE_ID r_mtyp = WORM_MSGTYPE_INVALID;
00846    if ( p_name == NULL )
00847    {
00848       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
00849                     , "TGlobalUtils::LookupMessageTypeId(): requested name is NULL\n"
00850                     );
00851    }
00852    else
00853    {
00854       if ( 0 < MessageTypeIds.count(p_name) )
00855       {
00856          r_mtyp = MessageTypeIds[p_name];
00857       }
00858    }
00859    return r_mtyp;
00860 }
00861 

Generated on Tue May 6 09:16:03 2003 for Earthworm Libs by doxygen1.3-rc3