Beispiel #1
0
/**
 * @brief initialize the log sections
 *
 * This writes a list of all available and all enabled sections to the log.
 *
 * Log sections can be enabled using the configuration key "LogSections",
 * or the environment variable "SPRING_LOG_SECTIONS".
 *
 * Both specify a comma-separated list of sections that should be enabled.
 * The lists from both sources are combined, there is no overriding.
 *
 * A section that is enabled by default, can not be disabled.
 */
static void InitializeLogSections()
{
    // the new systems (ILog.h) log-sub-systems are called sections
    const std::set<const char*>& registeredSections = log_filter_section_getRegisteredSet();

    // enabled sections is a superset of the ones specified in the
    // environment and the ones specified in the configuration file.
    const std::map<std::string, int>& enabledSections = GetEnabledSections();

    std::stringstream availableLogSectionsStr;
    std::stringstream enabledLogSectionsStr;

    availableLogSectionsStr << "Available log sections: ";
    enabledLogSectionsStr << "Enabled log sections: ";

    unsigned int numRegisteredSections = 0;
    unsigned int numEnabledSections = 0;

    for (auto si = registeredSections.begin(); si != registeredSections.end(); ++si) {
        numRegisteredSections++;

        availableLogSectionsStr << ((numRegisteredSections > 1)? ", ": "");
        availableLogSectionsStr << *si;

        // enabled sections (keys) are in lower-case
        const auto sectionIter = enabledSections.find(StringToLower(*si));

        // skip if section is registered but not enabled
        if (sectionIter == enabledSections.end())
            continue;

        // user-specified wanted level for this section
        const int sectionLevel = sectionIter->second;

        if (sectionLevel >= LOG_LEVEL_NONE)
            continue;

        // find the nearest lower known log-level (in descending order)
        const int logLevel = log_util_getNearestLevel(sectionLevel);

        // levels can't go lower than this
        if (logLevel < 0)
            continue;

        log_filter_section_setMinLevel(*si, logLevel);

        enabledLogSectionsStr << ((numEnabledSections > 0)? ", ": "");
        enabledLogSectionsStr << *si << "(" << log_util_levelToString(logLevel) << ")";

        numEnabledSections++;
    }

    LOG("%s", (availableLogSectionsStr.str()).c_str());
    LOG("%s", (enabledLogSectionsStr.str()).c_str());

    LOG("Enable or disable log sections using the LogSections configuration key");
    LOG("  or the SPRING_LOG_SECTIONS environment variable (both comma separated).");
    LOG("  Use \"none\" to disable the default log sections.");
}
Beispiel #2
0
void CLogOutput::InitializeSections()
{
    // the new systems (ILog.h) log-sub-systems are called sections:
    const std::set<const char*> sections = log_filter_section_getRegisteredSet();

    {
        std::stringstream logSectionsStr;
        logSectionsStr << "Available log sections: ";
        int numSec = 0;
        std::set<const char*>::const_iterator si;
        for (si = sections.begin(); si != sections.end(); ++si) {
            if (numSec > 0) {
                logSectionsStr << ", ";
            }
            logSectionsStr << *si;
            numSec++;
        }
        LOG("%s", logSectionsStr.str().c_str());
    }

    // enabled sections is a superset of the ones specified in the environment
    // and the ones specified in the configuration file.
    // configHandler cannot be accessed here in unitsync, as it may not exist.
    std::string enabledSections = ",";
#if defined(UNITSYNC)
#if defined(DEBUG)
    // unitsync logging in debug mode always on
    enabledSections += "unitsync,ArchiveScanner,";
#endif
#else
#if defined(DEDICATED)
    enabledSections += "DedicatedServer,";
#endif
#if !defined(DEBUG)
    // Always show at least INFO level of these sections
    enabledSections += "Sound,";
#endif
    enabledSections += StringToLower(configHandler->GetString("LogSections")) + ",";
#endif

    const char* const envSec = getenv("SPRING_LOG_SECTIONS");
    std::string env;
    if (envSec != NULL) {
        env += ",";
        env += envSec;
    }

    if (!env.empty()) {
        // this allows to disable all sections from the env var
        std::string envSections(StringToLower(env));
        if (envSections == std::string("none")) {
            enabledSections = "";
        } else {
            enabledSections += envSections + ",";
        }
    }
    const std::string enabledSectionsLC = StringToLower(enabledSections);

    {
        std::stringstream enabledLogSectionsStr;
        enabledLogSectionsStr << "Enabled log sections: ";
        int numSec = 0;

        // new log sections
        std::set<const char*>::const_iterator si;
        for (si = sections.begin(); si != sections.end(); ++si) {
            const std::string name = StringToLower(*si);
            const bool found = (enabledSectionsLC.find("," + name + ",") != std::string::npos);

            if (found) {
                if (numSec > 0) {
                    enabledLogSectionsStr << ", ";
                }
#if       defined(DEBUG)
                log_filter_section_setMinLevel(*si, LOG_LEVEL_DEBUG);
                enabledLogSectionsStr << *si << "(LOG_LEVEL_DEBUG)";
#else  // defined(DEBUG)
                log_filter_section_setMinLevel(*si, LOG_LEVEL_INFO);
                enabledLogSectionsStr << *si << "(LOG_LEVEL_INFO)";
#endif // defined(DEBUG)
                numSec++;
            }
        }
        LOG("%s", enabledLogSectionsStr.str().c_str());
    }

    LOG("Enable or disable log sections using the LogSections configuration key");
    LOG("  or the SPRING_LOG_SECTIONS environment variable (both comma separated).");
    LOG("  Use \"none\" to disable the default log sections.");
}