IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 7, 2006, 12:30:33 PM (20 years ago)
Author:
Paul Price
Message:

Reorganising so that the trace and logging stuff is loaded the
earliest (so that we print the trace messages from the configuration
system). Adding environment variable substitution for the PATH in the
site configuration.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/config/pmConfig.c

    r9762 r9894  
    44 *  @author EAM (IfA)
    55 *
    6  *  @version $Revision: 1.51 $ $Name: not supported by cvs2svn $
    7  *  @date $Date: 2006-10-27 02:22:03 $
     6 *  @version $Revision: 1.52 $ $Name: not supported by cvs2svn $
     7 *  @date $Date: 2006-11-07 22:30:33 $
    88 *
    99 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    6565}
    6666
     67// Resolve environment variables within a directory name; returns the resolved directory string.
     68// The returned string is likely a new pointer; the old pointer should be freed by psStringSubstitute.
     69static psString resolveEnvVar(psString dir // Directory to check for environment variables
     70                             )
     71{
     72    char *envStart;                     // Start of any environment variable
     73    while ((envStart = strchr(dir, '$'))) {
     74        char *envName = envStart + 1;   // Start of the environment variable name
     75        if (envName[0] == '\0') {
     76            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Path %s contains a bad environment variable.\n", dir);
     77            return NULL;
     78        }
     79        if (envName[0] == '{') {
     80            envName++;
     81            if (envName[0] == '\0') {
     82                psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     83                        "Path %s contains a bad environment variable.\n", dir);
     84                return NULL;
     85            }
     86        }
     87        char *envStop = strpbrk(envStart, "}/"); // End of the environment variable
     88        ssize_t nameLength = envStop ? envStop - envName : strlen(envName); // Length of the name
     89        psString name = psStringNCopy(envName, nameLength); // The environment variable name
     90        char *value = getenv(name);     // Value of the environment variable
     91        psFree(name);
     92        psString valueSlash = NULL;    // Value with appended slash
     93        psStringAppend(&valueSlash, "%s/", value);
     94
     95        ssize_t envvarLength = envStop ? envStop - envStart : strlen(envStart); // Length, w/o '}'
     96        psString envvar = psStringNCopy(envStart, envvarLength + 1);  // Environment variable, with $, {, }
     97
     98        psTrace("psModules.config", 7, "Replacing %s with %s in directory %s\n", envvar, valueSlash, dir);
     99        dir = psStringSubstitute(dir, valueSlash, envvar);
     100        psFree(envvar);
     101        psFree(valueSlash);
     102    }
     103
     104    return dir;
     105}
     106
    67107void pmConfigSet(const char *path)
    68108{
     
    71111    psList *list = psStringSplit(path, ":", false);
    72112    configPath = psListToArray(list);
     113    // Resolve environment variables
     114    for (long i = 0; i < configPath->n; i++) {
     115        configPath->data[i] = resolveEnvVar(configPath->data[i]);
     116        psTrace("psModules.config", 4, "Path %ld: %s\n", i, (char*)configPath->data[i]);
     117    }
    73118    psFree(list);
    74119}
     
    220265    }
    221266
     267
     268
     269    // Set options based on the site configuration.
     270    {
     271        bool mdok = true;   // Status of MD lookup result
     272
     273        // Initialise the psLib time handling
     274        psString timeName = psMetadataLookupStr(&mdok, config->site, "TIME");
     275        if (mdok && timeName)
     276        {
     277            psTrace("psModules.config", 7, "Initialising psTime with file %s\n", timeName);
     278            // XXX: PAP had a call to psLibInit is PRODUCTION not set.  Why?
     279            psTimeInitialize(timeName);
     280        }
     281
     282
     283        // Set logging level
     284        int logLevel = psMetadataLookupS32(&mdok, config->site, "LOGLEVEL");
     285        if (mdok && logLevel >= 0)
     286        {
     287            psTrace("psModules.config", 7, "Setting log level to %d\n", logLevel);
     288            psLogSetLevel(logLevel);
     289        }
     290
     291
     292        // Set logging format
     293        psString logFormat = psMetadataLookupStr(&mdok, config->site, "LOGFORMAT");
     294        if (mdok && logFormat)
     295        {
     296            psTrace("psModules.config", 7, "Setting log format to %s\n", logFormat);
     297            psLogSetFormat(logFormat);
     298        }
     299
     300        argNum = psArgumentGet(*config->argc, config->argv, "-log");
     301        if (argNum > 0)
     302        {
     303            psArgumentRemove(argNum, config->argc, config->argv);
     304            if (argNum >= *config->argc) {
     305                psLogMsg(__func__, PS_LOG_WARN, "-log command-line switch provided without the "
     306                         "required log destination --- ignored.\n");
     307            } else {
     308                if (!psLogSetDestination(psMessageDestination(config->argv[argNum]))) {
     309                    psLogMsg(__func__, PS_LOG_WARN, "Unable to set log destination to %s\n",
     310                             config->argv[argNum]);
     311                }
     312                psArgumentRemove(argNum, config->argc, config->argv);
     313            }
     314        } else
     315        {
     316            // Set logging destination
     317            psString logDest = psMetadataLookupStr(&mdok, config->site, "LOGDEST");
     318            if (mdok && logDest && strlen(logDest) > 0) {
     319                // XXX: Only stdout and stderr are provided for now; this section should be
     320                // expanded in the future to do files, and perhaps even sockets.
     321                if (!psLogSetDestination(psMessageDestination(logDest))) {
     322                    psLogMsg(__func__, PS_LOG_WARN, "Unable to set log destination to %s\n",
     323                             config->argv[argNum]);
     324                }
     325            }
     326        }
     327
     328        // Set trace levels
     329        psMetadata *trace = psMetadataLookupMetadata(&mdok, config->site, "TRACE");
     330        if (mdok && trace)
     331        {
     332            psMetadataIterator *traceIter = psMetadataIteratorAlloc(trace, PS_LIST_HEAD, NULL); // Iterator
     333            psMetadataItem *traceItem = NULL; // Item from MD iteration
     334            while ((traceItem = psMetadataGetAndIncrement(traceIter))) {
     335                if (traceItem->type != PS_DATA_S32) {
     336                    psLogMsg(__func__, PS_LOG_WARN,
     337                             "The level for trace component %s is not of type S32 (%x)\n",
     338                             traceItem->name, traceItem->type);
     339                    continue;
     340                }
     341                psTrace("psModules.config", 7, "Setting trace level for %s to %d\n", traceItem->name,
     342                        traceItem->data.S32);
     343                psTraceSetLevel(traceItem->name, traceItem->data.S32);
     344            }
     345            psFree(traceIter);
     346        }
     347
     348        // Set trace formats
     349        psString traceFormat = psMetadataLookupStr(&mdok, config->site, "TRACEFORMAT");
     350        if (mdok && traceFormat)
     351        {
     352            psTrace("psModules.config", 7, "Setting trace format to %s\n", traceFormat);
     353            psTraceSetFormat(traceFormat);
     354        }
     355
     356        // Set trace destinations
     357        psString traceDest = psMetadataLookupStr(&mdok, config->site, "TRACEDEST");
     358        if (mdok && traceDest && strlen(traceDest) > 0)
     359        {
     360            psTrace("psModules.config", 7, "Setting trace destination to %s\n", traceDest);
     361            // XXX: Only stdout and stderr are provided for now; this section should be
     362            // expanded in the future to do files, and perhaps even sockets.
     363            if (!psTraceSetDestination(psMessageDestination(traceDest))) {
     364                psLogMsg(__func__, PS_LOG_WARN, "Unable to set trace destination to %s\n",
     365                         config->argv[argNum]);
     366            }
     367        }
     368
     369        // Allow command line options to override defaults for logging.
     370        // XXX: Is it appropriate to use the ArgVerbosity function for this?
     371        //   A: it removes the options from the command line.
     372        //   B: will the pmConfigRead function always be called on initialization.
     373        //
     374        psS32 saveLogLevel = psLogGetLevel();
     375        psArgumentVerbosity(config->argc, config->argv);
     376        // XXX: substitute the string for the default log level for "2".
     377        if (2 == psLogGetLevel())
     378        {
     379            psLogSetLevel(saveLogLevel);
     380        }
     381    }
     382
    222383    // define the config-file search path (configPath).  Ensure that
    223384    // it contains the directory where we found the config file in
     
    278439        psFree(config);
    279440        return NULL;
    280     }
    281 
    282     //
    283     // We now have the config, camera, and recipe files parsed and stored in
    284     // metadata.  Now, we can look into the site configuration and do
    285     // the required stuff.
    286     //
    287     bool mdok = true;   // Status of MD lookup result
    288 
    289     //
    290     // If TIME is specified in the configuration file, then we must initialize
    291     // with a call to psTimeInitialize.
    292     //
    293     psString timeName = psMetadataLookupStr(&mdok, config->site, "TIME");
    294     if (mdok && timeName) {
    295         psTrace("psModules.config", 7, "Initialising psTime with file %s\n", timeName);
    296         // XXX: PAP had a call to psLibInit is PRODUCTION not set.  Why?
    297         psTimeInitialize(timeName);
    298     }
    299 
    300 
    301     //
    302     // If LOGLEVEL is specified in the configuration file, then we must initialize
    303     // with a call to psLogSetLevel().
    304     //
    305     int logLevel = psMetadataLookupS32(&mdok, config->site, "LOGLEVEL");
    306     if (mdok && logLevel >= 0) {
    307         psTrace("psModules.config", 7, "Setting log level to %d\n", logLevel);
    308         psLogSetLevel(logLevel);
    309     }
    310 
    311 
    312     //
    313     // If LOGFORMAT is specified in the configuration file, then we must initialize
    314     // with a call to psLogSetFormat().
    315     //
    316     psString logFormat = psMetadataLookupStr(&mdok, config->site, "LOGFORMAT");
    317     if (mdok && logFormat) {
    318         psTrace("psModules.config", 7, "Setting log format to %s\n", logFormat);
    319         psLogSetFormat(logFormat);
    320     }
    321 
    322 
    323     argNum = psArgumentGet(*config->argc, config->argv, "-log");
    324     if (argNum > 0) {
    325         psArgumentRemove(argNum, config->argc, config->argv);
    326         if (argNum >= *config->argc) {
    327             psLogMsg(__func__, PS_LOG_WARN,
    328                      "-log command-line switch provided without the required log destination --- ignored.\n");
    329         } else {
    330             if (!psLogSetDestination(psMessageDestination(config->argv[argNum]))) {
    331                 psLogMsg(__func__, PS_LOG_WARN, "Unable to set log destination to %s\n",
    332                          config->argv[argNum]);
    333             }
    334             psArgumentRemove(argNum, config->argc, config->argv);
    335         }
    336     } else {
    337         //
    338         // If LOGDEST is specified in the configuration file, then we must initialize
    339         // with a call to psLogSetDestination().
    340         // XXX: This is not spec'ed in the SDRS.
    341         //
    342         psString logDest = psMetadataLookupStr(&mdok, config->site, "LOGDEST");
    343         if (mdok && logDest && strlen(logDest) > 0) {
    344             // XXX: Only stdout and stderr are provided for now; this section should be
    345             // expanded in the future to do files, and perhaps even sockets.
    346             if (!psLogSetDestination(psMessageDestination(logDest))) {
    347                 psLogMsg(__func__, PS_LOG_WARN, "Unable to set log destination to %s\n",
    348                          config->argv[argNum]);
    349             }
    350         }
    351     }
    352 
    353     //
    354     // If TRACE is specified in the configuration file, then we must initialize
    355     // with a call to psTraceSetLevel().
    356     // XXX: This is not spec'ed in the SDRS.
    357     //
    358     psMetadata *trace = psMetadataLookupMetadata(&mdok, config->site, "TRACE");
    359     if (mdok && trace) {
    360         psMetadataIterator *traceIter = psMetadataIteratorAlloc(trace, PS_LIST_HEAD, NULL); // Iterator
    361         psMetadataItem *traceItem = NULL; // Item from MD iteration
    362         while ((traceItem = psMetadataGetAndIncrement(traceIter))) {
    363             if (traceItem->type != PS_DATA_S32) {
    364                 psLogMsg(__func__, PS_LOG_WARN, "The level for trace component %s is not of type S32 (%x)\n",
    365                          traceItem->name, traceItem->type);
    366                 continue;
    367             }
    368             psTrace("psModules.config", 7, "Setting trace level for %s to %d\n", traceItem->name,
    369                     traceItem->data.S32);
    370             psTraceSetLevel(traceItem->name, traceItem->data.S32);
    371         }
    372         psFree(traceIter);
    373     }
    374     //
    375     // If TRACEFORMAT is specified in the configuration file, then we must
    376     // initialize with a call to psTraceSetFormat().
    377     //
    378     psString traceFormat = psMetadataLookupStr(&mdok, config->site, "TRACEFORMAT");
    379     if (mdok && traceFormat) {
    380         psTrace("psModules.config", 7, "Setting trace format to %s\n", traceFormat);
    381         psTraceSetFormat(traceFormat);
    382     }
    383 
    384     //
    385     // If TRACEDEST is specified in the configuration file, then we must initialize
    386     // with a call to psLogSetDestination().
    387     // XXX: This is not spec'ed in the SDRS.
    388     //
    389     psString traceDest = psMetadataLookupStr(&mdok, config->site, "TRACEDEST");
    390     if (mdok && traceDest && strlen(traceDest) > 0) {
    391         psTrace("psModules.config", 7, "Setting trace destination to %s\n", traceDest);
    392         // XXX: Only stdout and stderr are provided for now; this section should be
    393         // expanded in the future to do files, and perhaps even sockets.
    394         if (!psTraceSetDestination(psMessageDestination(traceDest))) {
    395             psLogMsg(__func__, PS_LOG_WARN, "Unable to set trace destination to %s\n",
    396                      config->argv[argNum]);
    397         }
    398     }
    399 
    400     //
    401     // Allow command line options to override defaults for logging.
    402     // XXX: Is it appropriate to use the ArgVerbosity function for this?
    403     //   A: it removes the options from the command line.
    404     //   B: will the pmConfigRead function always be called on initialization.
    405     //
    406     psS32 saveLogLevel = psLogGetLevel();
    407     psArgumentVerbosity(config->argc, config->argv);
    408     // XXX: substitute the string for the default log level for "2".
    409     if (2 == psLogGetLevel()) {
    410         psLogSetLevel(saveLogLevel);
    411441    }
    412442
Note: See TracChangeset for help on using the changeset viewer.