Add MicroSnPrintf (#2343)

@tensorflow/micro

Preparatory work for [b/297590035](https://issuetracker.google.com/297590035)
Add MicroSnprintf
Add MicroVsnprintf
Update debug_log.cc for all required targets
Add debug_log.cc for riscv32_generic target using third-party eyalroz printf code (riscv32_generic stdio formatting does not support %f) 

bug=fixes #2342
diff --git a/tensorflow/lite/micro/BUILD b/tensorflow/lite/micro/BUILD
index 3bde297..60ace25 100644
--- a/tensorflow/lite/micro/BUILD
+++ b/tensorflow/lite/micro/BUILD
@@ -420,6 +420,7 @@
     deps = [
         ":micro_log",
         ":system_setup",
+        "//tensorflow/lite/micro/testing:micro_test",
     ],
 )
 
diff --git a/tensorflow/lite/micro/arc_emsdp/debug_log.cc b/tensorflow/lite/micro/arc_emsdp/debug_log.cc
index 55cf420..a61e3c2 100644
--- a/tensorflow/lite/micro/arc_emsdp/debug_log.cc
+++ b/tensorflow/lite/micro/arc_emsdp/debug_log.cc
@@ -122,4 +122,12 @@
   vsnprintf_(log_buffer, kMaxLogLen, format, args);
   LogDebugString(log_buffer);
 #endif
-}
\ No newline at end of file
+}
+
+#ifndef TF_LITE_STRIP_ERROR_STRINGS
+// Only called from MicroVsnprintf (micro_log.h)
+extern "C" int DebugVsnprintf(char* buffer, size_t buf_size, const char* format,
+                              va_list vlist) {
+  return vsnprintf_(buffer, buf_size, format, vlist);
+}
+#endif
diff --git a/tensorflow/lite/micro/bluepill/debug_log.cc b/tensorflow/lite/micro/bluepill/debug_log.cc
index 6d0d53b..720410b 100644
--- a/tensorflow/lite/micro/bluepill/debug_log.cc
+++ b/tensorflow/lite/micro/bluepill/debug_log.cc
@@ -45,3 +45,11 @@
   SysWriteDebugConsole(log_buffer);
 #endif  // TF_LITE_STRIP_ERROR_STRINGS
 }
+
+#ifndef TF_LITE_STRIP_ERROR_STRINGS
+// Only called from MicroVsnprintf (micro_log.h)
+extern "C" int DebugVsnprintf(char* buffer, size_t buf_size, const char* format,
+                              va_list vlist) {
+  return vsnprintf_(buffer, buf_size, format, vlist);
+}
+#endif
diff --git a/tensorflow/lite/micro/chre/debug_log.cc b/tensorflow/lite/micro/chre/debug_log.cc
index b599ebe..99f7c67 100644
--- a/tensorflow/lite/micro/chre/debug_log.cc
+++ b/tensorflow/lite/micro/chre/debug_log.cc
@@ -29,4 +29,12 @@
   vsnprintf_(log_buffer, kMaxLogLen, format, args);
   chreLog(CHRE_LOG_DEBUG, "[TFL_MICRO] %s", log_buffer);
 #endif
-}
\ No newline at end of file
+}
+
+#ifndef TF_LITE_STRIP_ERROR_STRINGS
+// Only called from MicroVsnprintf (micro_log.h)
+extern "C" int DebugVsnprintf(char* buffer, size_t buf_size, const char* format,
+                              va_list vlist) {
+  return vsnprintf_(buffer, buf_size, format, vlist);
+}
+#endif
diff --git a/tensorflow/lite/micro/cortex_m_generic/debug_log.cc b/tensorflow/lite/micro/cortex_m_generic/debug_log.cc
index 01294f1..0be6018 100644
--- a/tensorflow/lite/micro/cortex_m_generic/debug_log.cc
+++ b/tensorflow/lite/micro/cortex_m_generic/debug_log.cc
@@ -54,6 +54,14 @@
 #endif
 }
 
+#ifndef TF_LITE_STRIP_ERROR_STRINGS
+// Only called from MicroVsnprintf (micro_log.h)
+int DebugVsnprintf(char* buffer, size_t buf_size, const char* format,
+                   va_list vlist) {
+  return vsnprintf(buffer, buf_size, format, vlist);
+}
+#endif
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif  // __cplusplus
diff --git a/tensorflow/lite/micro/debug_log.cc b/tensorflow/lite/micro/debug_log.cc
index a8ef36b..9823196 100644
--- a/tensorflow/lite/micro/debug_log.cc
+++ b/tensorflow/lite/micro/debug_log.cc
@@ -44,3 +44,11 @@
   vfprintf(stderr, format, args);
 #endif
 }
+
+#ifndef TF_LITE_STRIP_ERROR_STRINGS
+// Only called from MicroVsnprintf (micro_log.h)
+extern "C" int DebugVsnprintf(char* buffer, size_t buf_size, const char* format,
+                              va_list vlist) {
+  return vsnprintf(buffer, buf_size, format, vlist);
+}
+#endif
diff --git a/tensorflow/lite/micro/debug_log.h b/tensorflow/lite/micro/debug_log.h
index c0cf699..6e2e69e 100644
--- a/tensorflow/lite/micro/debug_log.h
+++ b/tensorflow/lite/micro/debug_log.h
@@ -17,19 +17,23 @@
 
 #ifdef __cplusplus
 #include <cstdarg>
+#include <cstddef>
 #else
 #include <stdarg.h>
+#include <stddef.h>
 #endif  // __cplusplus
 
 #ifdef __cplusplus
 extern "C" {
 #endif  // __cplusplus
 
-// This function should be implemented by each target platform, and provide a
+// These functions should be implemented by each target platform, and provide a
 // way for strings to be output to some text stream. For more information, see
-// the tensorflow/lite/micro/debug_log.cc file.  This function should support
+// the tensorflow/lite/micro/debug_log.cc file.  These functions should support
 // standard C/C++ stdio style formatting operations.
 void DebugLog(const char* format, va_list args);
+int DebugVsnprintf(char* buffer, size_t buf_size, const char* format,
+                   va_list vlist);
 
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/tensorflow/lite/micro/micro_log.cc b/tensorflow/lite/micro/micro_log.cc
index 45f7051..a08a07d 100644
--- a/tensorflow/lite/micro/micro_log.cc
+++ b/tensorflow/lite/micro/micro_log.cc
@@ -46,4 +46,17 @@
   VMicroPrintf(format, args);
   va_end(args);
 }
+
+int MicroSnprintf(char* buffer, size_t buf_size, const char* format, ...) {
+  va_list args;
+  va_start(args, format);
+  int result = MicroVsnprintf(buffer, buf_size, format, args);
+  va_end(args);
+  return result;
+}
+
+int MicroVsnprintf(char* buffer, size_t buf_size, const char* format,
+                   va_list vlist) {
+  return DebugVsnprintf(buffer, buf_size, format, vlist);
+}
 #endif  // !defined(TF_LITE_STRIP_ERROR_STRINGS)
diff --git a/tensorflow/lite/micro/micro_log.h b/tensorflow/lite/micro/micro_log.h
index 669e9e6..af3c24a 100644
--- a/tensorflow/lite/micro/micro_log.h
+++ b/tensorflow/lite/micro/micro_log.h
@@ -17,15 +17,21 @@
 
 #if !defined(TF_LITE_STRIP_ERROR_STRINGS)
 #include <cstdarg>
+#include <cstddef>
 // These functions can be used independent of the MicroErrorReporter to get
 // printf-like functionalitys and are common to all target platforms.
 void MicroPrintf(const char* format, ...);
 void VMicroPrintf(const char* format, va_list args);
+int MicroSnprintf(char* buffer, size_t buf_size, const char* format, ...);
+int MicroVsnprintf(char* buffer, size_t buf_size, const char* format,
+                   va_list vlist);
 #else
 // We use a #define to ensure that the strings are completely stripped, to
 // prevent an unnecessary increase in the binary size.
 #define MicroPrintf(...) tflite::Unused(__VA_ARGS__)
 #define VMicroPrintf(...) tflite::Unused(__VA_ARGS__)
+#define MicroSnprintf(...) tflite::Unused<int>(__VA_ARGS__)
+#define MicroVsnprintf(...) tflite::Unused<int>(__VA_ARGS__)
 #endif
 
 namespace tflite {
@@ -37,6 +43,12 @@
   (void)(sizeof...(args));
 }
 
+template <typename T, typename... Args>
+T Unused(Args&&... args) {
+  (void)(sizeof...(args));
+  return static_cast<T>(0);
+}
+
 }  // namespace tflite
 
 #endif  // TENSORFLOW_LITE_MICRO_MICRO_LOG_H_
diff --git a/tensorflow/lite/micro/micro_log_test.cc b/tensorflow/lite/micro/micro_log_test.cc
index 97ac8be..7027e01 100644
--- a/tensorflow/lite/micro/micro_log_test.cc
+++ b/tensorflow/lite/micro/micro_log_test.cc
@@ -1,4 +1,4 @@
-/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
+/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -15,18 +15,42 @@
 
 #include "tensorflow/lite/micro/micro_log.h"
 
-#include "tensorflow/lite/micro/system_setup.h"
+#include <cstddef>
+#include <cstring>
 
-namespace tflite {
-inline void InitializeTest() { InitializeTarget(); }
-}  // namespace tflite
+#include "tensorflow/lite/micro/testing/micro_test.h"
 
-int main(int argc, char** argv) {
-  tflite::InitializeTest();
-#ifndef TF_LITE_STRIP_ERROR_STRINGS
-  MicroPrintf("Number: %d", 42);
-  MicroPrintf("Badly-formed format string %");
-  MicroPrintf("Another % badly-formed %% format string");
-  MicroPrintf("~~~%s~~~", "ALL TESTS PASSED");
+namespace {
+
+#if !defined(TF_LITE_STRIP_ERROR_STRINGS)
+constexpr int kMaxBufferSize = 128;
+const char* kFormat = "%2d%6.2f%#5x%5s";
+const char* kExpect = "42 42.42 0x42 \"42\"";
 #endif  // !defined(TF_LITE_STRIP_ERROR_STRINGS)
+
+}  // namespace
+
+TF_LITE_MICRO_TESTS_BEGIN
+
+#if !defined(TF_LITE_STRIP_ERROR_STRINGS)
+
+TF_LITE_MICRO_TEST(MicroPrintfTest) {
+  MicroPrintf("Integer 42: %d", 42);
+  MicroPrintf("Float 42.42: %2.2f", 42.42);
+  MicroPrintf("String \"Hello World!\": %s", "\"Hello World!\"");
+  MicroPrintf("Badly-formed format string %");
+  MicroPrintf("Another %# badly-formed %% format string");
 }
+
+TF_LITE_MICRO_TEST(MicroSnprintf) {
+  char buffer[kMaxBufferSize];
+  buffer[0] = '\0';
+  size_t result =
+      MicroSnprintf(buffer, kMaxBufferSize, kFormat, 42, 42.42, 0x42, "\"42\"");
+  TF_LITE_MICRO_EXPECT_EQ(result, strlen(buffer));
+  TF_LITE_MICRO_EXPECT_STRING_EQ(kExpect, buffer);
+}
+
+#endif  // !defined(TF_LITE_STRIP_ERROR_STRINGS)
+
+TF_LITE_MICRO_TESTS_END
diff --git a/tensorflow/lite/micro/riscv32_generic/debug_log.cc b/tensorflow/lite/micro/riscv32_generic/debug_log.cc
new file mode 100644
index 0000000..1d38fff
--- /dev/null
+++ b/tensorflow/lite/micro/riscv32_generic/debug_log.cc
@@ -0,0 +1,40 @@
+/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+==============================================================================*/
+
+#include "tensorflow/lite/micro/debug_log.h"
+
+#include <cstdio>
+
+#ifndef TF_LITE_STRIP_ERROR_STRINGS
+#include "eyalroz_printf/src/printf/printf.h"
+#endif
+
+extern "C" void DebugLog(const char* format, va_list args) {
+#ifndef TF_LITE_STRIP_ERROR_STRINGS
+  constexpr int kMaxLogLen = 256;
+  char log_buffer[kMaxLogLen];
+
+  vsnprintf_(log_buffer, kMaxLogLen, format, args);
+  std::fputs(log_buffer, stdout);
+#endif
+}
+
+#ifndef TF_LITE_STRIP_ERROR_STRINGS
+// Only called from MicroVsnprintf (micro_log.h)
+extern "C" int DebugVsnprintf(char* buffer, size_t buf_size, const char* format,
+                              va_list vlist) {
+  return vsnprintf_(buffer, buf_size, format, vlist);
+}
+#endif
diff --git a/tensorflow/lite/micro/tools/make/targets/riscv32_generic_makefile.inc b/tensorflow/lite/micro/tools/make/targets/riscv32_generic_makefile.inc
index 933745b..ecdf6b4 100644
--- a/tensorflow/lite/micro/tools/make/targets/riscv32_generic_makefile.inc
+++ b/tensorflow/lite/micro/tools/make/targets/riscv32_generic_makefile.inc
@@ -47,3 +47,4 @@
 SIZE_SCRIPT := ${TENSORFLOW_ROOT}tensorflow/lite/micro/testing/size_riscv32_binary.sh
 RUN_COMMAND := qemu-riscv32 -cpu rv32
 
+include $(MAKEFILE_DIR)/ext_libs/eyalroz_printf.inc