Skip to content

Commit

Permalink
Make TVMLogf platform-independent (apache#6916)
Browse files Browse the repository at this point in the history
* Make TVMLogf platform-independent.

 * Some platforms need to use an alternate printf() to support basic
   things like %zu. Since %zu is platform-specific, we prefer to
   use a printf() that supports it or allow the platform to fix it up
   as needed.

* git-clang-format
  • Loading branch information
areusch authored and trevor-m committed Dec 4, 2020
1 parent 6b8d1ce commit 0030275
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
17 changes: 17 additions & 0 deletions include/tvm/runtime/crt/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef TVM_RUNTIME_CRT_PLATFORM_H_
#define TVM_RUNTIME_CRT_PLATFORM_H_

#include <stdarg.h>
#include <stddef.h>
#include <tvm/runtime/crt/error_codes.h>

#ifdef __cplusplus
Expand All @@ -39,6 +41,21 @@ extern "C" {
*/
void __attribute__((noreturn)) TVMPlatformAbort(tvm_crt_error_t code);

/*! \brief Called by the microTVM RPC server to implement TVMLogf.
*
* Not required to be implemented when the RPC server is not linked into the binary. This
* function's signature matches that of vsnprintf, so trivial implementations can just call
* vsnprintf.
*
* \param out_buf A char buffer where the formatted string should be written.
* \param out_buf_size_bytes Number of bytes available for writing in out_buf.
* \param fmt The printf-style formatstring.
* \param args extra arguments to be formatted.
* \return number of bytes written.
*/
size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const char* fmt,
va_list args);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/crt/host/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ ssize_t UTvmWriteFunc(void* context, const uint8_t* data, size_t num_bytes) {
return to_return;
}

size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const char* fmt,
va_list args) {
return vsnprintf(out_buf, out_buf_size_bytes, fmt, args);
}

void TVMPlatformAbort(tvm_crt_error_t error_code) {
std::cerr << "TVMPlatformAbort: " << error_code << std::endl;
throw "Aborted";
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/crt/utvm_rpc_server/rpc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void TVMLogf(const char* format, ...) {
va_list args;
char log_buffer[256];
va_start(args, format);
size_t num_bytes_logged = vsnprintf(log_buffer, sizeof(log_buffer), format, args);
size_t num_bytes_logged = TVMPlatformFormatMessage(log_buffer, sizeof(log_buffer), format, args);
va_end(args);

// Most header-based logging frameworks tend to insert '\n' at the end of the log message.
Expand Down
5 changes: 5 additions & 0 deletions tests/micro/qemu/zephyr-runtime/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ ssize_t write_serial(void* unused_context, const uint8_t* data, size_t size) {
return size;
}

size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const char* fmt,
va_list args) {
return vsnprintk(out_buf, out_buf_size_bytes, fmt, args);
}

void TVMPlatformAbort(tvm_crt_error_t error) {
sys_reboot(SYS_REBOOT_COLD);
for (;;)
Expand Down

0 comments on commit 0030275

Please sign in to comment.