blob: b1e138622b5fd9d6bb31bb86687606d5a3e85242 [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
Roger Meier49ff8b12012-04-13 09:12:31 +000020#include <thrift/Thrift.h>
David Reissa4d7eef2008-06-11 01:18:20 +000021#include <cstring>
Bryan Duxbury7a0825c2010-09-02 16:42:18 +000022#include <cstdlib>
David Reissa1771092008-04-11 22:36:31 +000023#include <boost/lexical_cast.hpp>
David Reiss01e55c12008-07-13 22:18:51 +000024#include <stdarg.h>
25#include <stdio.h>
Mark Sleef9831082007-02-20 20:59:21 +000026
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift {
Mark Sleef9831082007-02-20 20:59:21 +000028
boz6ded7752007-06-05 22:41:18 +000029TOutput GlobalOutput;
30
David Reiss01e55c12008-07-13 22:18:51 +000031void TOutput::printf(const char *message, ...) {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040032#ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT
David Reiss01e55c12008-07-13 22:18:51 +000033 // Try to reduce heap usage, even if printf is called rarely.
34 static const int STACK_BUF_SIZE = 256;
35 char stack_buf[STACK_BUF_SIZE];
36 va_list ap;
37
Roger Meierd65216d2013-06-04 22:25:06 +020038#ifdef _MSC_VER
39 va_start(ap, message);
40 int need = _vscprintf(message, ap);
41 va_end(ap);
42
43 if (need < STACK_BUF_SIZE) {
44 va_start(ap, message);
45 vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap);
46 va_end(ap);
47 f_(stack_buf);
48 return;
49 }
50#else
David Reiss01e55c12008-07-13 22:18:51 +000051 va_start(ap, message);
52 int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap);
53 va_end(ap);
54
55 if (need < STACK_BUF_SIZE) {
56 f_(stack_buf);
57 return;
58 }
Roger Meierd65216d2013-06-04 22:25:06 +020059#endif
David Reiss01e55c12008-07-13 22:18:51 +000060
61 char *heap_buf = (char*)malloc((need+1) * sizeof(char));
62 if (heap_buf == NULL) {
Roger Meierd65216d2013-06-04 22:25:06 +020063#ifdef _MSC_VER
64 va_start(ap, message);
65 vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap);
66 va_end(ap);
67#endif
David Reiss01e55c12008-07-13 22:18:51 +000068 // Malloc failed. We might as well print the stack buffer.
69 f_(stack_buf);
70 return;
71 }
72
73 va_start(ap, message);
74 int rval = vsnprintf(heap_buf, need+1, message, ap);
75 va_end(ap);
76 // TODO(shigin): inform user
77 if (rval != -1) {
78 f_(heap_buf);
79 }
80 free(heap_buf);
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040081#endif
82}
83
84void TOutput::errorTimeWrapper(const char* msg) {
85#ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT
86 time_t now;
87 char dbgtime[26];
88 time(&now);
89 THRIFT_CTIME_R(&now, dbgtime);
90 dbgtime[24] = 0;
91 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
92#endif
David Reiss01e55c12008-07-13 22:18:51 +000093}
94
95void TOutput::perror(const char *message, int errno_copy) {
96 std::string out = message + strerror_s(errno_copy);
97 f_(out.c_str());
98}
99
David Reiss9b209552008-04-08 06:26:05 +0000100std::string TOutput::strerror_s(int errno_copy) {
101#ifndef HAVE_STRERROR_R
David Reiss826e6482008-10-20 21:29:07 +0000102 return "errno = " + boost::lexical_cast<std::string>(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000103#else // HAVE_STRERROR_R
104
105 char b_errbuf[1024] = { '\0' };
106#ifdef STRERROR_R_CHAR_P
107 char *b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
108#else
109 char *b_error = b_errbuf;
110 int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
111 if (rv == -1) {
112 // strerror_r failed. omgwtfbbq.
113 return "XSI-compliant strerror_r() failed with errno = " +
David Reissa1771092008-04-11 22:36:31 +0000114 boost::lexical_cast<std::string>(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000115 }
116#endif
117 // Can anyone prove that explicit cast is probably not necessary
118 // to ensure that the string object is constructed before
119 // b_error becomes invalid?
120 return std::string(b_error);
121
122#endif // HAVE_STRERROR_R
123}
124
T Jake Lucianib5e62212009-01-31 22:36:20 +0000125}} // apache::thrift