--- log.cc 2011-08-08 19:58:18.209399001 -0400 +++ log.new 2011-08-08 19:57:22.579399000 -0400 @@ -253,19 +253,28 @@ void + LogComponentEnable (char const *name, enum LogLevel level) { ComponentList *components = GetComponentList (); - for (ComponentListI i = components->begin (); + ComponentListI i; + for ( i = components->begin (); i != components->end (); i++) { if (i->first.compare (name) == 0) { i->second->Enable (level); - break; + return; } } + if(i == components->end()) { + // nothing matched + std::string ComponentName(name); + std::string errorMessage = "Logging component \""+ComponentName+"\" not found. See above for a list of available log components"; + LogComponentPrintList(); + NS_FATAL_ERROR (errorMessage.c_str()); + } } void @@ -354,9 +363,110 @@ } } +static bool ComponentExists(std::string componentName) +{ + char const*name=componentName.c_str(); + ComponentList *components = GetComponentList (); + ComponentListI i; + for ( i = components->begin (); + i != components->end (); + i++) + { + if (i->first.compare (name) == 0) + { + return true; + } + } + if(i == components->end()) { // nothing matched + return false; + } + else { + NS_ASSERT(1==0); // the control should never reach here. + } +} + +static void checkEnvironmentVariables() { +#ifdef HAVE_GETENV + char *envVar = getenv ("NS_LOG"); + if (envVar == 0) + { + return; + } + std::string env = envVar; + + std::string::size_type cur = 0; + std::string::size_type next = 0; + + while (next != std::string::npos) + { + next = env.find_first_of (":", cur); + std::string tmp = std::string (env, cur, next-cur); + std::string::size_type equal = tmp.find ("="); + std::string component; + if (equal == std::string::npos) // ie no '=' characters found + { + component = tmp; + if (ComponentExists(component) || component == "*") + { + return; + } + else { + LogComponentPrintList(); + std::string errorMessage="Invalid or unregistered component name \""+component+"\" in env variable NS_LOG, see above for a list of valid components"; + NS_FATAL_ERROR(errorMessage.c_str()); + } + } + else + { + component = tmp.substr (0, equal); + if (ComponentExists(component) || component == "*") + { + std::string::size_type cur_lev; + std::string::size_type next_lev = equal; + do + { + cur_lev = next_lev + 1; + next_lev = tmp.find ("|", cur_lev); + std::string lev = tmp.substr (cur_lev, next_lev - cur_lev); + if ( (lev == "error") + || (lev == "warn") + || (lev == "debug") + || (lev == "info") + || (lev == "function") + || (lev == "logic") + || (lev == "all") + || (lev == "prefix_func") + || (lev == "prefix_time") + || (lev == "prefix_node") + || (lev == "level_error") + || (lev == "level_warn") + || (lev == "level_debug") + || (lev == "level_info") + || (lev == "level_function") + || (lev == "level_logic") + || (lev == "level_all") + ) continue; + else { + std::string errorMessage="Invalid log level \""+lev+"\" in env variable NS_LOG for component name " + component; + NS_FATAL_ERROR(errorMessage.c_str()); + } + } while (next_lev != std::string::npos); + } + else + { + LogComponentPrintList(); + std::string errorMessage="Invalid or unregistered component name \""+component+"\" in env variable NS_LOG, see above for a list of valid components"; + NS_FATAL_ERROR(errorMessage.c_str()); + } + } + cur = next + 1; // parse next component + } +#endif +} void LogSetTimePrinter (LogTimePrinter printer) { g_logTimePrinter = printer; + checkEnvironmentVariables(); } LogTimePrinter LogGetTimePrinter (void) {