File: ecbuild_log.cmake

package info (click to toggle)
magics%2B%2B 2.30.0-5
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 105,040 kB
  • ctags: 32,903
  • sloc: cpp: 185,631; xml: 18,565; ansic: 11,002; perl: 6,357; python: 4,065; sh: 802; f90: 278; asm: 271; makefile: 157
file content (265 lines) | stat: -rw-r--r-- 9,055 bytes parent folder | download | duplicates (4)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# (C) Copyright 1996-2016 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

##############################################################################
#.rst:
#
# Logging
# =======
#
# ecBuild provides functions for logging based on a log level set by the user,
# similar to the Python logging module:
#
# :ecbuild_debug:     logs a ``STATUS`` message if log level <= ``DEBUG``
# :ecbuild_info:      logs a ``STATUS`` message if log level <= ``INFO``
# :ecbuild_warn:      logs a ``WARNING`` message if log level <= ``WARN``
# :ecbuild_error:     logs a ``SEND_ERROR`` message if log level <= ``ERROR``
# :ecbuild_critical:  logs a ``FATAL_ERROR`` message if log level <= ``CRITICAL``
# :ecbuild_deprecate: logs a ``DEPRECATION`` message as a warning
#                     enable CMAKE_ERROR_DEPRECATED to raise an error instead
#                     disable CMAKE_WARN_DEPRECATED to hide deprecations
#
# Furthermore there are auxilliary functions for outputting CMake variables,
# CMake lists and environment variables if the log level is ``DEBUG``:
#
# :ecbuild_debug_var:     logs given CMake variables if log level <= ``DEBUG``
# :ecbuild_debug_list:    logs given CMake lists if log level <= ``DEBUG``
# :ecbuild_debug_env_var: logs given environment variables if log level <= ``DEBUG``
#
# To log a message to the ecBuild log file only at a given log level, use ::
#
#   ecbuild_log( <level> <msg> )
#
# Input variables
# ---------------
#
# CMake variables controlling logging behaviour:
#
# ECBUILD_LOG_FILE : path
#   set the log file, defaults to ``${CMAKE_BINARY_DIR}/ecbuild.log``
#
#   All ecBuild log functions write their messages to this log file with a time
#   stamp. Messages emitted by CMake directly cannot be logged to file.
#
# ECBUILD_LOG_LEVEL : string, one of DEBUG, INFO, WARN, ERROR, CRITICAL, OFF
#   desired log level, defaults to ``INFO``, ``OFF`` to disable logging
#
# ECBUILD_NO_COLOUR : bool
#   if set, does not colour log output (by default log output is coloured)
#
# Usage
# -----
#
# The functions ``ecbuild_debug`` and ``ecbuild_info`` can be used to output
# messages which are not printed by default. Many ecBuild macros use this
# facility to log debugging hints. When debugging a CMake run, users can use
# ``-DECBUILD_LOG_LEVEL=DEBUG`` to get detailed diagnostics.
#
##############################################################################

# Define colour escape sequences (not available on Windows)
if(NOT (WIN32 OR ECBUILD_NO_COLOUR))
  string(ASCII 27 Esc)
  set(ColourReset "${Esc}[m")
  set(ColourBold  "${Esc}[1m")
  set(Red         "${Esc}[31m")
  set(Green       "${Esc}[32m")
  set(Yellow      "${Esc}[33m")
  set(Blue        "${Esc}[34m")
  set(Magenta     "${Esc}[35m")
  set(Cyan        "${Esc}[36m")
  set(White       "${Esc}[37m")
  set(BoldRed     "${Esc}[1;31m")
  set(BoldGreen   "${Esc}[1;32m")
  set(BoldYellow  "${Esc}[1;33m")
  set(BoldBlue    "${Esc}[1;34m")
  set(BoldMagenta "${Esc}[1;35m")
  set(BoldCyan    "${Esc}[1;36m")
  set(BoldWhite   "${Esc}[1;37m")
endif()

set(ECBUILD_DEBUG    10)
set(ECBUILD_INFO     20)
set(ECBUILD_WARN     30)
set(ECBUILD_ERROR    40)
set(ECBUILD_CRITICAL 50)

if( NOT DEFINED ECBUILD_LOG_LEVEL )
  set(ECBUILD_LOG_LEVEL ${ECBUILD_INFO})
elseif( NOT ECBUILD_LOG_LEVEL )
  set(ECBUILD_LOG_LEVEL 60)
elseif( ECBUILD_LOG_LEVEL STREQUAL "DEBUG" )
  set(ECBUILD_LOG_LEVEL ${ECBUILD_DEBUG})
elseif( ECBUILD_LOG_LEVEL STREQUAL "INFO" )
  set(ECBUILD_LOG_LEVEL ${ECBUILD_INFO})
elseif( ECBUILD_LOG_LEVEL STREQUAL "WARN" )
  set(ECBUILD_LOG_LEVEL ${ECBUILD_WARN})
elseif( ECBUILD_LOG_LEVEL STREQUAL "ERROR" )
  set(ECBUILD_LOG_LEVEL ${ECBUILD_ERROR})
elseif( ECBUILD_LOG_LEVEL STREQUAL "CRITICAL" )
  set(ECBUILD_LOG_LEVEL ${ECBUILD_CRITICAL})
else()
  message(WARNING "Unknown log level ${ECBUILD_LOG_LEVEL} (valid are DEBUG, INFO, WARN, ERROR, CRITICAL) - using WARN")
  set(ECBUILD_LOG_LEVEL ${ECBUILD_WARN})
endif()

if( NOT DEFINED ECBUILD_LOG_FILE )
  set( ECBUILD_LOG_FILE ${CMAKE_BINARY_DIR}/ecbuild.log )
endif()
if( NOT DEFINED CMAKE_ERROR_DEPRECATED AND NOT DEFINED CMAKE_WARN_DEPRECATED )
  set( CMAKE_WARN_DEPRECATED ON )
endif()

##############################################################################

function( ecbuild_log LEVEL )
  string( REPLACE ";" " " MSG "${ARGN}" )
  string( TIMESTAMP _time )
  file( APPEND ${ECBUILD_LOG_FILE} "${_time} - ${LEVEL} - ${MSG}\n" )
endfunction( ecbuild_log )

##############################################################################

function( ecbuild_debug )
  string( REPLACE ";" " " MSG "${ARGV}" )
  ecbuild_log(DEBUG "${MSG}")
  if( ECBUILD_LOG_LEVEL LESS 11)
    message(STATUS "${Blue}DEBUG - ${MSG}${ColourReset}")
  endif()
endfunction( ecbuild_debug )

##############################################################################

function( ecbuild_info )
  string( REPLACE ";" " " MSG "${ARGV}" )
  ecbuild_log(INFO "${MSG}")
  if( ECBUILD_LOG_LEVEL LESS 21)
    message(STATUS "${MSG}")
  endif()
endfunction( ecbuild_info )

##############################################################################

function( ecbuild_warn )
  string( REPLACE ";" " " MSG "${ARGV}" )
  ecbuild_log(WARNING "${MSG}")
  if( ECBUILD_LOG_LEVEL LESS 31)
    message(WARNING "${Yellow}WARN - ${MSG}${ColourReset}")
  endif()
endfunction( ecbuild_warn )

##############################################################################

function( ecbuild_error )
  string( REPLACE ";" " " MSG "${ARGV}" )
  ecbuild_log(ERROR "${MSG}")
  if( ECBUILD_LOG_LEVEL LESS 41)
    message(SEND_ERROR "${BoldRed}ERROR - ${MSG}${ColourReset}")
  endif()
endfunction( ecbuild_error )

##############################################################################

function( ecbuild_deprecate )
  string(REPLACE ";" " " MSG ${ARGV})
  ecbuild_log(DEPRECATION "${MSG}")
  # DEPRECATION message type was only introduced in CMake 3.0, provide
  # consistent behaviour for CMake < 3.0
  if( CMAKE_VERSION VERSION_LESS 3.0 )
    if( CMAKE_ERROR_DEPRECATED )
      message(FATAL_ERROR "${BoldRed}DEPRECATION - ${MSG}${ColourReset}")
    elseif( CMAKE_WARN_DEPRECATED )
      message(WARNING "${Yellow}DEPRECATION - ${MSG}${ColourReset}")
    endif()
  else()
    message(DEPRECATION "${BoldRed}${MSG}${ColourReset}")
  endif()
endfunction( ecbuild_deprecate )

##############################################################################

function( ecbuild_critical )
  string(REPLACE ";" " " MSG ${ARGV})
  ecbuild_log(FATAL_ERROR "${MSG}")
  if( ECBUILD_LOG_LEVEL LESS 51)
    message(FATAL_ERROR "${BoldMagenta}CRITICAL - ${MSG}${ColourReset}")
  endif()
endfunction( ecbuild_critical )

##############################################################################
# function for debugging CMake variables

function( ecbuild_debug_var )
  foreach( VAR ${ARGV} )
    ecbuild_log(DEBUG "${VAR} : ${${VAR}}")
    if( ECBUILD_LOG_LEVEL LESS 11)
      message(STATUS "${Blue}DEBUG - ${VAR} : ${${VAR}}${ColourReset}")
    endif()
  endforeach()
endfunction()

##############################################################################
# function for debugging CMake lists

function( ecbuild_debug_list )
  foreach( VAR ${ARGV} )
    ecbuild_log(DEBUG "${VAR} : ${${VAR}}")
    foreach( _elem ${${VAR}} )
      ecbuild_log( DEBUG "  ${_elem}" )
    endforeach()
    if( ECBUILD_LOG_LEVEL LESS 11)
      message( STATUS "${Blue}DEBUG - ${VAR}" )
      foreach( _elem ${${VAR}} )
        message( STATUS "  ${_elem}" )
      endforeach()
      message(STATUS "${ColourReset}")
    endif()
  endforeach()
endfunction()

##############################################################################
# function for debugging environment variables

function( ecbuild_debug_env_var )
  foreach( VAR ${ARGV} )
    ecbuild_log(DEBUG "ENV ${VAR} : $ENV{${VAR}}")
    if( ECBUILD_LOG_LEVEL LESS 11)
      message(STATUS "${Blue}DEBUG - ENV ${VAR} [$ENV{${VAR}}]${ColourReset}")
    endif()
  endforeach()
endfunction()

##############################################################################
# macro for debugging a cmake variable

macro( debug_var VAR )

    message( WARNING "DEPRECATED debug_var() -- ${VAR} [${${VAR}}]" )

endmacro()

##############################################################################
# macro for debugging a cmake list

macro( debug_list VAR )

    message( WARNING "DEPRECATED debug_list() -- ${VAR}:" )
    foreach( _elem ${${VAR}} )
      message( WARNING "  ${_elem}" )
    endforeach()

endmacro()

##############################################################################
# macro for debugging a environment variable within cmake

macro( debug_env_var VAR )

    message( WARNING "DEPRECATED debug_env_var() -- ENV ${VAR} [$ENV{${VAR}}]" )

endmacro()