szaszm commented on a change in pull request #1057:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1057#discussion_r633704910



##########
File path: libminifi/include/core/logging/Logger.h
##########
@@ -68,13 +69,32 @@ inline T conditional_conversion(T t) {
 }
 
 template<typename ... Args>
-inline std::string format_string(char const* format_str, Args&&... args) {
-  char buf[LOG_BUFFER_SIZE];
-  std::snprintf(buf, LOG_BUFFER_SIZE, format_str, 
conditional_conversion(std::forward<Args>(args))...);
-  return std::string(buf);
+inline std::string format_string(int max_size, char const* format_str, 
Args&&... args) {
+  if (0 <= max_size && max_size <= LOG_BUFFER_SIZE) {
+    // use static buffer
+    char buf[LOG_BUFFER_SIZE + 1];
+    std::snprintf(buf, max_size + 1, format_str, 
conditional_conversion(std::forward<Args>(args))...);
+    return std::string(buf);
+  }
+  // use dynamically allocated buffer
+  if (max_size < 0) {
+    // query what buffer size we need
+    int size_needed = std::snprintf(nullptr, 0, format_str, 
conditional_conversion(std::forward<Args>(args))...);

Review comment:
       `snprintf` incurs the full cost of formatting even if the buffer is 
`nullptr` AFAIK. I would prefer a conservative sized stack buffer with fallback 
to allocation, so that at least sometimes it can be faster.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to