blob: 8d163a941832c17f017671a241ec93692f06ffa2 [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>
Jim Apple117a5cc2017-03-29 20:39:36 -070021#include <thrift/TToString.h>
David Reissa4d7eef2008-06-11 01:18:20 +000022#include <cstring>
Bryan Duxbury7a0825c2010-09-02 16:42:18 +000023#include <cstdlib>
David Reiss01e55c12008-07-13 22:18:51 +000024#include <stdarg.h>
25#include <stdio.h>
Mark Sleef9831082007-02-20 20:59:21 +000026
Konrad Grochowski16a23a62014-11-13 15:33:38 +010027namespace apache {
28namespace thrift {
Mark Sleef9831082007-02-20 20:59:21 +000029
cyy8fdb7582019-02-05 02:57:21 +080030THRIFT_EXPORT TOutput GlobalOutput;
boz6ded7752007-06-05 22:41:18 +000031
James E. King III278528c2019-01-11 12:17:44 -050032TOutput::TOutput() : f_(&errorTimeWrapper) {}
33
Konrad Grochowski16a23a62014-11-13 15:33:38 +010034void TOutput::printf(const char* message, ...) {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040035#ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT
David Reiss01e55c12008-07-13 22:18:51 +000036 // Try to reduce heap usage, even if printf is called rarely.
37 static const int STACK_BUF_SIZE = 256;
38 char stack_buf[STACK_BUF_SIZE];
39 va_list ap;
40
Roger Meierd65216d2013-06-04 22:25:06 +020041#ifdef _MSC_VER
42 va_start(ap, message);
43 int need = _vscprintf(message, ap);
44 va_end(ap);
45
46 if (need < STACK_BUF_SIZE) {
47 va_start(ap, message);
48 vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap);
49 va_end(ap);
50 f_(stack_buf);
51 return;
52 }
53#else
David Reiss01e55c12008-07-13 22:18:51 +000054 va_start(ap, message);
55 int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap);
56 va_end(ap);
57
58 if (need < STACK_BUF_SIZE) {
59 f_(stack_buf);
60 return;
61 }
Roger Meierd65216d2013-06-04 22:25:06 +020062#endif
David Reiss01e55c12008-07-13 22:18:51 +000063
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064 char* heap_buf = (char*)malloc((need + 1) * sizeof(char));
Sebastian Zenker042580f2019-01-29 15:48:12 +010065 if (heap_buf == nullptr) {
Roger Meierd65216d2013-06-04 22:25:06 +020066#ifdef _MSC_VER
67 va_start(ap, message);
68 vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap);
69 va_end(ap);
70#endif
David Reiss01e55c12008-07-13 22:18:51 +000071 // Malloc failed. We might as well print the stack buffer.
72 f_(stack_buf);
73 return;
74 }
75
76 va_start(ap, message);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010077 int rval = vsnprintf(heap_buf, need + 1, message, ap);
David Reiss01e55c12008-07-13 22:18:51 +000078 va_end(ap);
79 // TODO(shigin): inform user
80 if (rval != -1) {
81 f_(heap_buf);
82 }
83 free(heap_buf);
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040084#endif
85}
86
87void TOutput::errorTimeWrapper(const char* msg) {
88#ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT
89 time_t now;
90 char dbgtime[26];
91 time(&now);
92 THRIFT_CTIME_R(&now, dbgtime);
93 dbgtime[24] = 0;
94 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
95#endif
David Reiss01e55c12008-07-13 22:18:51 +000096}
97
Konrad Grochowski16a23a62014-11-13 15:33:38 +010098void TOutput::perror(const char* message, int errno_copy) {
James E. King III9bea32f2018-03-16 16:07:42 -040099 std::string out = message + std::string(": ") + strerror_s(errno_copy);
David Reiss01e55c12008-07-13 22:18:51 +0000100 f_(out.c_str());
101}
102
David Reiss9b209552008-04-08 06:26:05 +0000103std::string TOutput::strerror_s(int errno_copy) {
104#ifndef HAVE_STRERROR_R
Jim Apple117a5cc2017-03-29 20:39:36 -0700105 return "errno = " + to_string(errno_copy);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100106#else // HAVE_STRERROR_R
David Reiss9b209552008-04-08 06:26:05 +0000107
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100108 char b_errbuf[1024] = {'\0'};
David Reiss9b209552008-04-08 06:26:05 +0000109#ifdef STRERROR_R_CHAR_P
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100110 char* b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
David Reiss9b209552008-04-08 06:26:05 +0000111#else
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100112 char* b_error = b_errbuf;
David Reiss9b209552008-04-08 06:26:05 +0000113 int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
114 if (rv == -1) {
115 // strerror_r failed. omgwtfbbq.
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100116 return "XSI-compliant strerror_r() failed with errno = "
Jim Apple117a5cc2017-03-29 20:39:36 -0700117 + to_string(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000118 }
119#endif
120 // Can anyone prove that explicit cast is probably not necessary
121 // to ensure that the string object is constructed before
122 // b_error becomes invalid?
123 return std::string(b_error);
124
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100125#endif // HAVE_STRERROR_R
David Reiss9b209552008-04-08 06:26:05 +0000126}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100127}
128} // apache::thrift