static void ParseForLogging()

in ctsTraffic/ctsConfig.cpp [1690:1860]


static void ParseForLogging(vector<const wchar_t*>& args)
{
    // ReSharper disable once CppTooWideScopeInitStatement
    const auto foundVerbosity = ranges::find_if(args, [](const wchar_t* parameter) -> bool {
        const auto* const value = ParseArgument(parameter, L"-ConsoleVerbosity");
        return value != nullptr;
    });
    if (foundVerbosity != end(args))
    {
        g_consoleVerbosity = ConvertToIntegral<uint32_t>(ParseArgument(*foundVerbosity, L"-ConsoleVerbosity"));
        if (g_consoleVerbosity > 6)
        {
            throw invalid_argument("-ConsoleVerbosity");
        }
        // always remove the arg from our vector
        args.erase(foundVerbosity);
    }

    // ReSharper disable once CppTooWideScopeInitStatement
    const auto foundStatusUpdate = ranges::find_if(args, [](const wchar_t* parameter) -> bool {
        const auto* const value = ParseArgument(parameter, L"-StatusUpdate");
        return value != nullptr;
    });
    if (foundStatusUpdate != end(args))
    {
        g_configSettings->StatusUpdateFrequencyMilliseconds = ConvertToIntegral<uint32_t>(ParseArgument(*foundStatusUpdate, L"-StatusUpdate"));
        if (0 == g_configSettings->StatusUpdateFrequencyMilliseconds)
        {
            throw invalid_argument("-StatusUpdate");
        }
        // always remove the arg from our vector
        args.erase(foundStatusUpdate);
    }

    wstring connectionFilename;
    wstring errorFilename;
    wstring statusFilename;
    wstring jitterFilename;

    // ReSharper disable once CppTooWideScopeInitStatement
    const auto foundConnectionFilename = ranges::find_if(args, [](const wchar_t* parameter) -> bool {
        const auto* const value = ParseArgument(parameter, L"-ConnectionFilename");
        return value != nullptr;
    });
    if (foundConnectionFilename != end(args))
    {
        connectionFilename = ParseArgument(*foundConnectionFilename, L"-ConnectionFilename");
        // always remove the arg from our vector
        args.erase(foundConnectionFilename);
    }

    // ReSharper disable once CppTooWideScopeInitStatement
    const auto foundErrorFilename = ranges::find_if(args, [](const wchar_t* parameter) -> bool {
        const auto* const value = ParseArgument(parameter, L"-ErrorFilename");
        return value != nullptr;
    });
    if (foundErrorFilename != end(args))
    {
        errorFilename = ParseArgument(*foundErrorFilename, L"-ErrorFilename");
        // always remove the arg from our vector
        args.erase(foundErrorFilename);
    }

    // ReSharper disable once CppTooWideScopeInitStatement
    const auto foundStatusFilename = ranges::find_if(args, [](const wchar_t* parameter) -> bool {
        const auto* const value = ParseArgument(parameter, L"-StatusFilename");
        return value != nullptr;
    });
    if (foundStatusFilename != end(args))
    {
        statusFilename = ParseArgument(*foundStatusFilename, L"-StatusFilename");
        // always remove the arg from our vector
        args.erase(foundStatusFilename);
    }

    // ReSharper disable once CppTooWideScopeInitStatement
    const auto foundJitterFilename = ranges::find_if(args, [](const wchar_t* parameter) -> bool {
        const auto* const value = ParseArgument(parameter, L"-JitterFilename");
        return value != nullptr;
    });
    if (foundJitterFilename != end(args))
    {
        jitterFilename = ParseArgument(*foundJitterFilename, L"-JitterFilename");
        // always remove the arg from our vector
        args.erase(foundJitterFilename);
    }

    // since CSV files each have their own header, we cannot allow the same CSV filename to be used
    // for different loggers, as opposed to txt files, which can be shared across different loggers

    if (!connectionFilename.empty())
    {
        if (ctString::ctOrdinalEndsWithCaseInsensative(connectionFilename, L".csv"))
        {
            g_connectionLogger = make_shared<ctsTextLogger>(connectionFilename.c_str(), StatusFormatting::Csv);
        }
        else
        {
            g_connectionLogger = make_shared<ctsTextLogger>(connectionFilename.c_str(), StatusFormatting::ClearText);
        }
    }

    if (!errorFilename.empty())
    {
        if (ctString::ctOrdinalEqualsCaseInsensative(connectionFilename, errorFilename))
        {
            if (g_connectionLogger->IsCsvFormat())
            {
                throw invalid_argument("The error logfile cannot be of csv format");
            }
            g_errorLogger = g_connectionLogger;
        }
        else
        {
            if (ctString::ctOrdinalEndsWithCaseInsensative(errorFilename, L".csv"))
            {
                throw invalid_argument("The error logfile cannot be of csv format");
            }
            g_errorLogger = make_shared<ctsTextLogger>(errorFilename.c_str(), StatusFormatting::ClearText);
        }
    }

    if (!statusFilename.empty())
    {
        if (ctString::ctOrdinalEqualsCaseInsensative(connectionFilename, statusFilename))
        {
            if (g_connectionLogger->IsCsvFormat())
            {
                throw invalid_argument("The same csv filename cannot be used for different loggers");
            }
            g_statusLogger = g_connectionLogger;
        }
        else if (ctString::ctOrdinalEqualsCaseInsensative(errorFilename, statusFilename))
        {
            if (g_errorLogger->IsCsvFormat())
            {
                throw invalid_argument("The same csv filename cannot be used for different loggers");
            }
            g_statusLogger = g_errorLogger;
        }
        else
        {
            if (ctString::ctOrdinalEndsWithCaseInsensative(statusFilename, L".csv"))
            {
                g_statusLogger = make_shared<ctsTextLogger>(statusFilename.c_str(), StatusFormatting::Csv);
            }
            else
            {
                g_statusLogger = make_shared<ctsTextLogger>(statusFilename.c_str(), StatusFormatting::ClearText);
            }
        }
    }

    if (!jitterFilename.empty())
    {
        if (ctString::ctOrdinalEndsWithCaseInsensative(jitterFilename, L".csv"))
        {
            if (ctString::ctOrdinalEqualsCaseInsensative(connectionFilename, jitterFilename) ||
                ctString::ctOrdinalEqualsCaseInsensative(errorFilename, jitterFilename) ||
                ctString::ctOrdinalEqualsCaseInsensative(statusFilename, jitterFilename))
            {
                throw invalid_argument("The same csv filename cannot be used for different loggers");
            }
            g_jitterLogger = make_shared<ctsTextLogger>(jitterFilename.c_str(), StatusFormatting::Csv);
        }
        else
        {
            throw invalid_argument("Jitter can only be logged using a csv format");
        }
    }
}