Add option to turn off logging completely

Add new ETHOSU_LOG_ENABLE variable in the CMakeLists.txt to toggle
logging completely off, or else by the desired severity level.
Turning off the logging will also disable the LOG() macro.

Change-Id: I313a97fb1569000ae22637734ef4a60eedc56f70
Signed-off-by: Jonny Svärd <jonny.svaerd@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a5a4bd3..bb9f22d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -29,6 +29,7 @@
 set(CMSIS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmsis" CACHE PATH "Path to CMSIS.")
 
 set(LOG_NAMES err warning info debug)
+set(ETHOSU_LOG_ENABLE ON CACHE BOOL "Toggle driver logs on/off (Defaults to ON)")
 set(ETHOSU_LOG_SEVERITY "warning" CACHE STRING "Driver log severity level ${LOG_NAMES} (Defaults to 'warning')")
 set(ETHOSU_TARGET_NPU_CONFIG "ethos-u55-128" CACHE STRING "Default NPU configuration")
 set_property(CACHE ETHOSU_LOG_SEVERITY PROPERTY STRINGS ${LOG_NAMES})
@@ -76,7 +77,9 @@
 
 
 # Set the log level for the target
-target_compile_definitions(ethosu_core_driver PRIVATE ETHOSU_LOG_SEVERITY=${LOG_SEVERITY})
+target_compile_definitions(ethosu_core_driver PRIVATE
+    ETHOSU_LOG_SEVERITY=${LOG_SEVERITY}
+    ETHOSU_LOG_ENABLE=$<BOOL:${ETHOSU_LOG_ENABLE}>)
 
 # Install library and include files
 install(TARGETS ethosu_core_driver LIBRARY DESTINATION "lib")
@@ -95,5 +98,6 @@
 message(STATUS "ETHOSU_TARGET_NPU_CONFIG               : ${ETHOSU_TARGET_NPU_CONFIG}")
 message(STATUS "CMAKE_SYSTEM_PROCESSOR                 : ${CMAKE_SYSTEM_PROCESSOR}")
 message(STATUS "CMSIS_PATH                             : ${CMSIS_PATH}")
+message(STATUS "ETHOSU_LOG_ENABLE                      : ${ETHOSU_LOG_ENABLE}")
 message(STATUS "ETHOSU_LOG_SEVERITY                    : ${ETHOSU_LOG_SEVERITY}")
 message(STATUS "*******************************************************")
diff --git a/src/ethosu_log.h b/src/ethosu_log.h
index 582b91d..13f7726 100644
--- a/src/ethosu_log.h
+++ b/src/ethosu_log.h
@@ -1,6 +1,5 @@
 /*
  * SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
- *
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the License); you may
@@ -41,32 +40,43 @@
 #define ETHOSU_LOG_SEVERITY ETHOSU_LOG_WARN
 #endif
 
+// Logs enabled by default
+#ifndef ETHOSU_LOG_ENABLE
+#define ETHOSU_LOG_ENABLE 1
+#endif
+
+#if ETHOSU_LOG_ENABLE
+#define LOG_COMMON(s, f, ...) (void)fprintf(s, f, ##__VA_ARGS__)
+#else
+#define LOG_COMMON(s, f, ...)
+#endif
+
 // Log formatting
-#define LOG(f, ...) (void)fprintf(stdout, f, ##__VA_ARGS__)
+#define LOG(f, ...) LOG_COMMON(stdout, f, ##__VA__ARGS__)
 
 #if ETHOSU_LOG_SEVERITY >= ETHOSU_LOG_ERR
 #define LOG_ERR(f, ...)                                                                                                \
-    (void)fprintf(stderr, "E: " f " (%s:%d)\n", ##__VA_ARGS__, strrchr("/" __FILE__, '/') + 1, __LINE__)
+    LOG_COMMON(stderr, "E: " f " (%s:%d)\n", ##__VA_ARGS__, strrchr("/" __FILE__, '/') + 1, __LINE__)
 #else
 #define LOG_ERR(f, ...)
 #endif
 
 #if ETHOSU_LOG_SEVERITY >= ETHOSU_LOG_WARN
-#define LOG_WARN(f, ...) (void)fprintf(stdout, "W: " f "\n", ##__VA_ARGS__)
+#define LOG_WARN(f, ...) LOG_COMMON(stdout, "W: " f "\n", ##__VA_ARGS__)
 #else
 #define LOG_WARN(f, ...)
 #endif
 
 #if ETHOSU_LOG_SEVERITY >= ETHOSU_LOG_INFO
-#define LOG_INFO(f, ...) (void)fprintf(stdout, "I: " f "\n", ##__VA_ARGS__)
+#define LOG_INFO(f, ...) LOG_COMMON(stdout, "I: " f "\n", ##__VA_ARGS__)
 #else
 #define LOG_INFO(f, ...)
 #endif
 
 #if ETHOSU_LOG_SEVERITY >= ETHOSU_LOG_DEBUG
-#define LOG_DEBUG(f, ...) (void)fprintf(stdout, "D: %s(): " f "\n", __FUNCTION__, ##__VA_ARGS__)
+#define LOG_DEBUG(f, ...) LOG_COMMON(stdout, "D: %s(): " f "\n", __FUNCTION__, ##__VA_ARGS__)
 #else
 #define LOG_DEBUG(f, ...)
 #endif
 
-#endif
\ No newline at end of file
+#endif