adamdebreceni commented on a change in pull request #1057: URL: https://github.com/apache/nifi-minifi-cpp/pull/1057#discussion_r635261808
########## 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))...); + if (size_needed < 0) { + // error + return std::string("Error while formatting log message"); + } + std::vector<char> buffer(size_needed + 1); // extra '\0' character + std::snprintf(buffer.data(), buffer.size(), format_str, conditional_conversion(std::forward<Args>(args))...); + return std::string(buffer.data()); + } + // use dynamic but fix-sized buffer + std::vector<char> buffer(max_size); + std::snprintf(buffer.data(), buffer.size(), format_str, conditional_conversion(std::forward<Args>(args))...); + return std::string(buffer.data()); Review comment: changed them to use either the `(const char*, size_t)` or the `(It first, It last)` constructors ########## 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: done -- 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