blob: cc0a01bf4879f12107a843ca6718899c2950e79b [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef9831082007-02-20 20:59:21 +000020#include <Thrift.h>
David Reissa4d7eef2008-06-11 01:18:20 +000021#include <cstring>
David Reissa1771092008-04-11 22:36:31 +000022#include <boost/lexical_cast.hpp>
David Reiss01e55c12008-07-13 22:18:51 +000023#include <stdarg.h>
24#include <stdio.h>
Mark Sleef9831082007-02-20 20:59:21 +000025
T Jake Lucianib5e62212009-01-31 22:36:20 +000026namespace apache { namespace thrift {
Mark Sleef9831082007-02-20 20:59:21 +000027
boz6ded7752007-06-05 22:41:18 +000028TOutput GlobalOutput;
29
David Reiss01e55c12008-07-13 22:18:51 +000030void TOutput::printf(const char *message, ...) {
31 // Try to reduce heap usage, even if printf is called rarely.
32 static const int STACK_BUF_SIZE = 256;
33 char stack_buf[STACK_BUF_SIZE];
34 va_list ap;
35
36 va_start(ap, message);
37 int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap);
38 va_end(ap);
39
40 if (need < STACK_BUF_SIZE) {
41 f_(stack_buf);
42 return;
43 }
44
45 char *heap_buf = (char*)malloc((need+1) * sizeof(char));
46 if (heap_buf == NULL) {
47 // Malloc failed. We might as well print the stack buffer.
48 f_(stack_buf);
49 return;
50 }
51
52 va_start(ap, message);
53 int rval = vsnprintf(heap_buf, need+1, message, ap);
54 va_end(ap);
55 // TODO(shigin): inform user
56 if (rval != -1) {
57 f_(heap_buf);
58 }
59 free(heap_buf);
60}
61
62void TOutput::perror(const char *message, int errno_copy) {
63 std::string out = message + strerror_s(errno_copy);
64 f_(out.c_str());
65}
66
David Reiss9b209552008-04-08 06:26:05 +000067std::string TOutput::strerror_s(int errno_copy) {
68#ifndef HAVE_STRERROR_R
David Reiss826e6482008-10-20 21:29:07 +000069 return "errno = " + boost::lexical_cast<std::string>(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +000070#else // HAVE_STRERROR_R
71
72 char b_errbuf[1024] = { '\0' };
73#ifdef STRERROR_R_CHAR_P
74 char *b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
75#else
76 char *b_error = b_errbuf;
77 int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
78 if (rv == -1) {
79 // strerror_r failed. omgwtfbbq.
80 return "XSI-compliant strerror_r() failed with errno = " +
David Reissa1771092008-04-11 22:36:31 +000081 boost::lexical_cast<std::string>(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +000082 }
83#endif
84 // Can anyone prove that explicit cast is probably not necessary
85 // to ensure that the string object is constructed before
86 // b_error becomes invalid?
87 return std::string(b_error);
88
89#endif // HAVE_STRERROR_R
90}
91
T Jake Lucianib5e62212009-01-31 22:36:20 +000092}} // apache::thrift