2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the
7
* "License"); you may not use this file except in compliance
8
* with the License. You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing,
13
* software distributed under the License is distributed on an
14
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
* KIND, either express or implied. See the License for the
16
* specific language governing permissions and limitations
23
#include <boost/lexical_cast.hpp>
27
namespace apache { namespace thrift {
31
void TOutput::printf(const char *message, ...) {
32
// Try to reduce heap usage, even if printf is called rarely.
33
static const int STACK_BUF_SIZE = 256;
34
char stack_buf[STACK_BUF_SIZE];
37
va_start(ap, message);
38
int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap);
41
if (need < STACK_BUF_SIZE) {
46
char *heap_buf = (char*)malloc((need+1) * sizeof(char));
47
if (heap_buf == NULL) {
48
// Malloc failed. We might as well print the stack buffer.
53
va_start(ap, message);
54
int rval = vsnprintf(heap_buf, need+1, message, ap);
56
// TODO(shigin): inform user
63
void TOutput::perror(const char *message, int errno_copy) {
64
std::string out = message + strerror_s(errno_copy);
68
std::string TOutput::strerror_s(int errno_copy) {
69
#ifndef HAVE_STRERROR_R
70
return "errno = " + boost::lexical_cast<std::string>(errno_copy);
71
#else // HAVE_STRERROR_R
73
char b_errbuf[1024] = { '\0' };
74
#ifdef STRERROR_R_CHAR_P
75
char *b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
77
char *b_error = b_errbuf;
78
int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
80
// strerror_r failed. omgwtfbbq.
81
return "XSI-compliant strerror_r() failed with errno = " +
82
boost::lexical_cast<std::string>(errno_copy);
85
// Can anyone prove that explicit cast is probably not necessary
86
// to ensure that the string object is constructed before
87
// b_error becomes invalid?
88
return std::string(b_error);
90
#endif // HAVE_STRERROR_R