MLBEDSW-2596 Add support to set driver log severity

The driver can log at one of the following levels: 0=emerg,
1=alert, 2=crit, 3=err, 4=warning, 5=notice, 6=info, 7=debug.

The logs at or below the set level are printed whereas
the higher level logs are not printed.

Change-Id: I05a498d4c8c78112207187d9dceaa2386b138c5d
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4651cf2..0dab28f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,9 +25,9 @@
 #
 
 option(DRIVER_PMU_AUTOINIT "Enable PMU boot auto-initialization" OFF)
-option(DRIVER_LOG_SUPPORT "Enable logging." OFF)
 
 set(CMSIS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmsis" CACHE PATH "Path to CMSIS.")
+set(DRIVER_LOG_SEVERITY "6" CACHE STRING "Driver log severity level 0=emerg, 1=alert, 2=crit, 3=err, 4=warning, 5=notice, 6=info, 7=debug")
 
 #
 # Global settings
@@ -49,9 +49,12 @@
     message(FATAL_ERROR "Unsupported compiler ${CMAKE_SYSTEM_PROCESSOR}.")
 endif()
 
-# Enable logging support
-if(DRIVER_LOG_SUPPORT)
-    add_compile_definitions(LOG_ENABLED)
+# Check that DRIVER_LOG_SEVERITY has one of the supported
+# levels.
+set(LOG_SEVERITY_VALUE 0 1 2 3 4 5 6 7)
+set(LOG_SEVERITY_NAME EMERG ALERT CRIT ERR WARNING NOTICE INFO DEBUG)
+if(NOT ${DRIVER_LOG_SEVERITY} IN_LIST LOG_SEVERITY_VALUE)
+    message(FATAL_ERROR "Unsupported driver log severity level ${DRIVER_LOG_SEVERITY}")
 endif()
 
 # Enable PMU boot auto-initialization
@@ -72,6 +75,19 @@
 target_include_directories(ethosu_core_driver PUBLIC include)
 target_sources(ethosu_core_driver PRIVATE src/ethosu_driver.c src/ethosu_device.c src/ethosu_pmu.c)
 
+# Set the DRIVER_LOG_SEVERITY level for the target
+target_compile_definitions(ethosu_core_driver PRIVATE DRIVER_LOG_SEVERITY=${DRIVER_LOG_SEVERITY})
+
+foreach(S IN ZIP_LISTS LOG_SEVERITY_VALUE LOG_SEVERITY_NAME)
+    # This will add a define in the form of LOG_SEVERITY_INFO=6.
+    # This is to make the conditional check like
+    # (DRIVER_LOG_SEVERITY >= LOG_SEVERITY_INFO) possible.
+    target_compile_definitions(ethosu_core_driver PRIVATE LOG_SEVERITY_${S_1}=${S_0})
+    if(${DRIVER_LOG_SEVERITY} STREQUAL ${S_0})
+        set(DRIVER_LOG_SEVERITY_NAME ${S_1})
+    endif()
+endforeach()
+
 #
 # Print build status
 #
@@ -80,5 +96,5 @@
 message(STATUS "PROJECT_NAME                           : ${PROJECT_NAME}")
 message(STATUS "CMAKE_SYSTEM_PROCESSOR                 : ${CMAKE_SYSTEM_PROCESSOR}")
 message(STATUS "CMSIS_PATH                             : ${CMSIS_PATH}")
-message(STATUS "DRIVER_LOG_SUPPORT                     : ${DRIVER_LOG_SUPPORT}")
+message(STATUS "DRIVER_LOG_SEVERITY                    : ${DRIVER_LOG_SEVERITY} (${DRIVER_LOG_SEVERITY_NAME})")
 message(STATUS "*******************************************************")