Running and output

Output text from recipes

Use the self.output attribute to output text from the recipes. Do not use Python’s print() function. The self.output attribute has the following methods to express the level of the printed message:

warning(msg, warn_tag=None)

These output functions will only output if the verbosity level with which Conan was launched is the same or higher than the message, so running with -vwarning will output calls to warning() and error(), but not info() (Additionally, the highlight() and success() methods have a -vnotice verbosity level)

Note that these methods return the output object again, so that you can chain output calls if needed.

Using the core:warnings_as_errors conf, you can make Conan raise an exception when either errors or a tagged warning matching any of the given patterns is printed. This is useful to make sure that recipes are not printing unexpected warnings or errors. Additionally, you can skip which warnings trigger an exception with the *core:skip_warnings* conf.

# Raise an exception if any warning or error is printed
# But skip the deprecation warnings

Both confs accept a list of patterns to match against the warning tags. A special unknown value can be used to match any warning without a tag.

To tag a warning, use the warn_tag argument of the warning() method in your recipes:

self.output.warning("Extra warning", warn_tag="custom_tag")

Running commands

run(self, command, stdout=None, cwd=None, ignore_errors=False, env="", quiet=False, shell=True, scope="build", stderr=None) is a helper to run system commands while injecting the calls to activate the appropriate environment, and throw exceptions when errors occur so that command errors do not pass unnoticed. It also wraps the commands with the results of the command wrapper plugin.

  • command should be specified as a string which is passed to the system shell.

  • When the argument quiet is set to true, the invocation of will not print the command to be executed.

Use the stdout and stderr arguments to redirect the output of the command to a file-like object instead of the console.

# Redirect stdout to a file
with open("ninja_stdout.log", "w") as stdout:
    # Redirect stderr to a StringIO object to be able to read it later
    stderr = StringIO()"ninja ...", stdout=stdout, stderr=stderr)