Search in sources :

Example 1 with FilePatternResolver

use of com.teamscale.jacoco.agent.options.FilePatternResolver in project teamscale-jacoco-agent by cqse.

the class AgentBase method initializeFallbackLogging.

/**
 * Initializes fallback logging in case of an error during the parsing of the options to {@link #premain(String,
 * Instrumentation)} (see TS-23151). This tries to extract the logging configuration and use this and falls back to
 * the default logger.
 */
private static LoggingResources initializeFallbackLogging(String premainOptions, DelayedLogger delayedLogger) {
    for (String optionPart : premainOptions.split(",")) {
        if (optionPart.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=")) {
            return createFallbackLoggerFromConfig(optionPart.split("=", 2)[1], delayedLogger);
        }
        if (optionPart.startsWith(AgentOptionsParser.CONFIG_FILE_OPTION + "=")) {
            String configFileValue = optionPart.split("=", 2)[1];
            Optional<String> loggingConfigLine = Optional.empty();
            try {
                File configFile = new FilePatternResolver(delayedLogger).parsePath(AgentOptionsParser.CONFIG_FILE_OPTION, configFileValue).toFile();
                loggingConfigLine = FileSystemUtils.readLinesUTF8(configFile).stream().filter(line -> line.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=")).findFirst();
            } catch (IOException | AgentOptionParseException e) {
                delayedLogger.error("Failed to load configuration from " + configFileValue + ": " + e.getMessage(), e);
            }
            if (loggingConfigLine.isPresent()) {
                return createFallbackLoggerFromConfig(loggingConfigLine.get().split("=", 2)[1], delayedLogger);
            }
        }
    }
    return LoggingUtils.initializeDefaultLogging();
}
Also used : FilePatternResolver(com.teamscale.jacoco.agent.options.FilePatternResolver) AgentOptionParseException(com.teamscale.jacoco.agent.options.AgentOptionParseException) IOException(java.io.IOException) File(java.io.File)

Aggregations

AgentOptionParseException (com.teamscale.jacoco.agent.options.AgentOptionParseException)1 FilePatternResolver (com.teamscale.jacoco.agent.options.FilePatternResolver)1 File (java.io.File)1 IOException (java.io.IOException)1