cpp: Eliminate the use of fprintf [THRIFT-77]
Add printf and perror methods to TOutput and use them to
replace uses of fprintf in the C++ library.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@676448 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/Thrift.cpp b/lib/cpp/src/Thrift.cpp
index da21d33..c784015 100644
--- a/lib/cpp/src/Thrift.cpp
+++ b/lib/cpp/src/Thrift.cpp
@@ -8,11 +8,50 @@
#include <cstring>
#include <boost/lexical_cast.hpp>
#include <protocol/TProtocol.h>
+#include <stdarg.h>
+#include <stdio.h>
namespace facebook { namespace thrift {
TOutput GlobalOutput;
+void TOutput::printf(const char *message, ...) {
+ // Try to reduce heap usage, even if printf is called rarely.
+ static const int STACK_BUF_SIZE = 256;
+ char stack_buf[STACK_BUF_SIZE];
+ va_list ap;
+
+ va_start(ap, message);
+ int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap);
+ va_end(ap);
+
+ if (need < STACK_BUF_SIZE) {
+ f_(stack_buf);
+ return;
+ }
+
+ char *heap_buf = (char*)malloc((need+1) * sizeof(char));
+ if (heap_buf == NULL) {
+ // Malloc failed. We might as well print the stack buffer.
+ f_(stack_buf);
+ return;
+ }
+
+ va_start(ap, message);
+ int rval = vsnprintf(heap_buf, need+1, message, ap);
+ va_end(ap);
+ // TODO(shigin): inform user
+ if (rval != -1) {
+ f_(heap_buf);
+ }
+ free(heap_buf);
+}
+
+void TOutput::perror(const char *message, int errno_copy) {
+ std::string out = message + strerror_s(errno_copy);
+ f_(out.c_str());
+}
+
std::string TOutput::strerror_s(int errno_copy) {
#ifndef HAVE_STRERROR_R
return "errno = " + boost::lexical_cast<string>(errno_copy);