Repository: bitcoin/bitcoin
File: src/init/common.cpp (C++)

Code before:

util::Result<void> SetLoggingCategories(const ArgsManager& args)
{
        const std::vector<std::string> categories = args.GetArgs("-debug");

        // Special-case: Disregard any debugging categories appearing before -debug=0/none
        const auto last_negated = std::find_if(categories.rbegin(), categories.rend(),
                                               [](const std::string& cat) { return cat == "0" || cat == "none"; });

        const auto categories_to_process = (last_negated == categories.rend()) ? categories : std::ranges::subrange(last_negated.base(), categories.end());

        for (const auto& cat : categories_to_process) {
            if (!LogInstance().EnableCategory(cat)) {
                return util::Error{strprintf(_("Unsupported logging category %s=%s."), "-debug", cat)};
            }
        }

    // Now remove the logging categories which were explicitly excluded
    for (const std::string& cat : args.GetArgs("-debugexclude")) {
        if (!LogInstance().DisableCategory(cat)) {
            return util::Error{strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat)};
        }
    }

    if (LogInstance().GetCategoryMask() != BCLog::NONE) {
        LogInfo("Debug logging is enabled (-debug). Debug logs may contain privacy-sensitive information (e.g., transaction IDs, network addresses) and should not be shared publicly.");
    }

    return {};
}

bool StartLogging(const ArgsManager& args)
{
    if (LogInstance().m_print_to_file) {
        if (args.GetBoolArg("-shrinkdebugfile", LogInstance().DefaultShrinkDebugFile())) {
            // Do this first since it both loads a bunch of debug.log into memory,
            // and because this needs to happen before any other debug.log printing
            LogInstance().ShrinkDebugFile();
        }
    }
    if (!LogInstance().StartLogging()) {
            return InitError(Untranslated(strprintf("Could not open debug log file %s",
                fs::PathToString(LogInstance().m_file_path))));
    }

    if (!LogInstance().m_log_timestamps) {
        LogInfo("Startup time: %s", FormatISO8601DateTime(GetTime()));
    }
    LogInfo("Default data directory %s", fs::PathToString(GetDefaultDataDir()));
    LogInfo("Using data directory %s", fs::PathToString(gArgs.GetDataDirNet()));


Review comment (bug):
> ... network addresses ...

Those are controlled by `-logips`. So, maybe remove "network addresses".

Offtopic, the description of `-logips` reads:

> Include IP addresses in debug output

however in some places we would log the addresses if `-logips=1` even in non-debug output (`LogInfo()` or `LogError()`). So, I guess the part "in debug output" is outdated.

Code after:

util::Result<void> SetLoggingCategories(const ArgsManager& args)
{
        const std::vector<std::string> categories = args.GetArgs("-debug");

        // Special-case: Disregard any debugging categories appearing before -debug=0/none
        const auto last_negated = std::find_if(categories.rbegin(), categories.rend(),
                                               [](const std::string& cat) { return cat == "0" || cat == "none"; });

        const auto categories_to_process = (last_negated == categories.rend()) ? categories : std::ranges::subrange(last_negated.base(), categories.end());

        for (const auto& cat : categories_to_process) {
            if (!LogInstance().EnableCategory(cat)) {
                return util::Error{strprintf(_("Unsupported logging category %s=%s."), "-debug", cat)};
            }
        }

    // Now remove the logging categories which were explicitly excluded
    for (const std::string& cat : args.GetArgs("-debugexclude")) {
        if (!LogInstance().DisableCategory(cat)) {
            return util::Error{strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat)};
        }
    }

    if (LogInstance().GetCategoryMask() != BCLog::NONE) {
        LogInfo("Debug logging is enabled (-debug). Additional log output may contain privacy-sensitive information. Be cautious when sharing logs.");
    }

    return {};
}

bool StartLogging(const ArgsManager& args)
{
    if (LogInstance().m_print_to_file) {
        if (args.GetBoolArg("-shrinkdebugfile", LogInstance().DefaultShrinkDebugFile())) {
            // Do this first since it both loads a bunch of debug.log into memory,
            // and because this needs to happen before any other debug.log printing
            LogInstance().ShrinkDebugFile();
        }
    }
    if (!LogInstance().StartLogging()) {
            return InitError(Untranslated(strprintf("Could not open debug log file %s",
                fs::PathToString(LogInstance().m_file_path))));
    }

    if (!LogInstance().m_log_timestamps) {
        LogInfo("Startup time: %s", FormatISO8601DateTime(GetTime()));
    }
    LogInfo("Default data directory %s", fs::PathToString(GetDefaultDataDir()));
    LogInfo("Using data directory %s", fs::PathToString(gArgs.GetDataDirNet()));