summaryrefslogtreecommitdiffstats
path: root/Help/variable/CMAKE_MESSAGE_CONTEXT.rst
blob: 41ace436fca8cb1e2eb6040c87f1b6ebe5541c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
CMAKE_MESSAGE_CONTEXT
---------------------

.. versionadded:: 3.17

When enabled by the :manual:`cmake <cmake(1)>` ``--log-context`` command line
option or the :variable:`CMAKE_MESSAGE_CONTEXT_SHOW` variable, the
:command:`message` command converts the ``CMAKE_MESSAGE_CONTEXT`` list into a
dot-separated string surrounded by square brackets and prepends it to each line
for messages of log levels ``NOTICE`` and below.

For logging contexts to work effectively, projects should generally
``APPEND`` and ``POP_BACK`` an item to the current value of
``CMAKE_MESSAGE_CONTEXT`` rather than replace it.
Projects should not assume the message context at the top of the source tree
is empty, as there are scenarios where the context might have already been set
(e.g. hierarchical projects).

.. warning::

  Valid context names are restricted to anything that could be used
  as a CMake variable name.  All names that begin with an underscore
  or the string ``cmake_`` are also reserved for use by CMake and
  should not be used by projects.

Example:

.. code-block:: cmake

  function(bar)
    list(APPEND CMAKE_MESSAGE_CONTEXT "bar")
    message(VERBOSE "bar VERBOSE message")
  endfunction()

  function(baz)
    list(APPEND CMAKE_MESSAGE_CONTEXT "baz")
    message(DEBUG "baz DEBUG message")
  endfunction()

  function(foo)
    list(APPEND CMAKE_MESSAGE_CONTEXT "foo")
    bar()
    message(TRACE "foo TRACE message")
    baz()
  endfunction()

  list(APPEND CMAKE_MESSAGE_CONTEXT "top")

  message(VERBOSE "Before `foo`")
  foo()
  message(VERBOSE "After `foo`")

  list(POP_BACK CMAKE_MESSAGE_CONTEXT)


Which results in the following output:

.. code-block:: none

  -- [top] Before `foo`
  -- [top.foo.bar] bar VERBOSE message
  -- [top.foo] foo TRACE message
  -- [top.foo.baz] baz DEBUG message
  -- [top] After `foo`