github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/variablesubstitution/DebugHelper.groovy (about)

     1  package com.sap.piper.variablesubstitution
     2  
     3  /**
     4   * Very simple debug helper. Declared as a Field
     5   * and passed the configuration with a call to `setConfig(Map config)`
     6   * in the body of a `call(...)` block, once
     7   * the configuration is available.
     8   *
     9   * If `config.verbose` is set to `true` a message
    10   * issued with `debug(String)` will be logged. Otherwise it will silently be omitted.
    11   */
    12  class DebugHelper {
    13      /**
    14       * The script which will be used to echo debug messages.
    15       */
    16      private Script script
    17      /**
    18       * The configuration which will be scanned for a `verbose` flag.
    19       * Only if this is true, will debug messages be written.
    20       */
    21      private Map config
    22  
    23      /**
    24       * Creates a new instance using the given script to issue `echo` commands.
    25       * The given config's `verbose` flag will decide if a message will be logged or not.
    26       * @param script - the script whose `echo` command will be used.
    27       * @param config - the configuration whose `verbose` flag is inspected before logging debug statements.
    28       */
    29      DebugHelper(Script script, Map config) {
    30          if(!script) {
    31              throw new IllegalArgumentException("[DebugHelper] Script parameter must not be null.")
    32          }
    33  
    34          if(!config) {
    35              throw new IllegalArgumentException("[DebugHelper] Config map parameter must not be null.")
    36          }
    37  
    38          this.script = script
    39          this.config = config
    40      }
    41  
    42      /**
    43       * Logs a debug message if a configuration
    44       * indicates that the `verbose` flag
    45       * is set to `true`
    46       * @param message - the message to log.
    47       */
    48      void debug(String message) {
    49          if(config.verbose) {
    50              script.echo message
    51          }
    52      }
    53  }