set(_FUNCTION_STACK "(root)") function(push_fnstack fn_name) list(APPEND _FUNCTION_STACK ${fn_name}) endfunction() function(pop_fnstack) list(POP_BACK _FUNCTION_STACK) endfunction() function(top_fnstack out_var) list(POP_BACK _FUNCTION_STACK ${out_var}) list(APPEND _FUNCTION_STACK ${out_var}) endfunction() macro(log) cmake_parse_arguments(_LOG_ARGS "NO_STACK" "TYPE;MESSAGE" "FAKE_STACK;MODULES" ${ARGN}) set(_CUR_LOG_OUTPUT ${message}) if(NOT ${_LOG_ARGS_NO_STACK}) foreach(FN_STACK_ITEM IN ITEMS ${_FUNCTION_STACK}) set(_CUR_LOG_OUTPUT "[" + ${FN_STACK_ITEM} + "] " + ${_CUR_LOG_OUTPUT}) endforeach() endif() if (${_LOG_ARGS_FAKE_STACK}) unset(_LOG_ARGS_FAKE_STACK) foreach(FAKE_STACK_ITEM IN ITEMS ${_LOG_ARGS_FAKE_STACK}) set(_CUR_LOG_OUTPUT "[" + ${FAKE_STACK_ITEM} + "] " + ${_CUR_LOG_OUTPUT}) endforeach() endif() if (${_LOG_ARGS_MODULE}) foreach(MODULE_ITEM IN ITEMS ${_LOG_ARGS_MODULES}) set(_CUR_LOG_OUTPUT "[Module: " + ${MODULE_ITEM} + "]" + ${_CUR_LOG_OUTPUT}) endforeach() endif() if(${_LOG_ARGS_TYPE}) set(_CUR_LOG_OUTPUT "[" + ${_LOG_ARGS_TYPE} + "] " + ${_CUR_LOG_OUTPUT}) endif() message(_CUR_LOG_OUTPUT) unset(_CUR_LOG_OUTPUT) endmacro() function(append_if_true OUTPUT_VAR ITEM_TO_APPEND VALUE) if (${VALUE}) list(APPEND ${OUTPUT_VAR} ${ITEM_TO_APPEND}) endif() endfunction() set(LOG_TYPES_IN_ORDER DEBUG INFO WARNING ERROR FATAL) option(LOG_VERBOSITY INFO) function(_log_message TYPE ARGS) cmake_parse_arguments(_LOG_PARENT_ARGS "NO_STACK" "MESSAGE" "FAKE_STACK;MODULES" ${ARGS}) set(_LOG_PARENT_ARGS MESSAGE ${_LOG_PARENT_ARGS_MESSAGE} FAKE_STACK ${_LOG_PARENT_ARGS_FAKE_STACK} MODULES ${_LOG_PARENT_ARGS_MODULES}) append_if_true(_LOG_PARENT_ARGS NO_STACK ${_LOG_PARENT_ARGS_NO_STACK}) list(FIND LOG_TYPES_IN_ORDER ${LOG_VERBOSITY} _LOG_VERBOSITY_IDX) list(FIND LOG_TYPES_IN_ORDER ${TYPE} _LOG_TYPE_IDX) if (${_LOG_VERBOSITY_IDX} GREATER_EQUAL ${_LOG_TYPE_IDX}) log(TYPE ${TYPE} ${_LOG_PARENT_ARGS}) endif() unset(_LOG_TYPE_IDX) endfunction() macro(warning) _log_message(WARNING ${ARGN}) endmacro() macro(error) _log_message(ERROR ${ARGN}) endmacro() macro(debug) _log_message(DEBUG ${ARGN}) endmacro() macro(fatal) _log_message(FATAL ${ARGN}) endmacro() macro(info) _log_message(INFO ${ARGN}) endmacro()