blob: a30879acd6bf6d9a22f43516b8e2082bbc7b88d5 [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
Mario Emmenlauer04574372020-04-24 11:44:41 +020027#ifdef _WIN32
28#include <windows.h>
29#endif
30
Konrad Grochowski16a23a62014-11-13 15:33:38 +010031namespace apache {
32namespace thrift {
Mark Sleef9831082007-02-20 20:59:21 +000033
cyy8fdb7582019-02-05 02:57:21 +080034THRIFT_EXPORT TOutput GlobalOutput;
boz6ded7752007-06-05 22:41:18 +000035
James E. King III278528c2019-01-11 12:17:44 -050036TOutput::TOutput() : f_(&errorTimeWrapper) {}
37
Konrad Grochowski16a23a62014-11-13 15:33:38 +010038void TOutput::printf(const char* message, ...) {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040039#ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT
David Reiss01e55c12008-07-13 22:18:51 +000040 // Try to reduce heap usage, even if printf is called rarely.
41 static const int STACK_BUF_SIZE = 256;
42 char stack_buf[STACK_BUF_SIZE];
43 va_list ap;
44
Roger Meierd65216d2013-06-04 22:25:06 +020045#ifdef _MSC_VER
46 va_start(ap, message);
47 int need = _vscprintf(message, ap);
48 va_end(ap);
49
50 if (need < STACK_BUF_SIZE) {
51 va_start(ap, message);
52 vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap);
53 va_end(ap);
54 f_(stack_buf);
55 return;
56 }
57#else
David Reiss01e55c12008-07-13 22:18:51 +000058 va_start(ap, message);
59 int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap);
60 va_end(ap);
61
62 if (need < STACK_BUF_SIZE) {
63 f_(stack_buf);
64 return;
65 }
Roger Meierd65216d2013-06-04 22:25:06 +020066#endif
David Reiss01e55c12008-07-13 22:18:51 +000067
Konrad Grochowski16a23a62014-11-13 15:33:38 +010068 char* heap_buf = (char*)malloc((need + 1) * sizeof(char));
Sebastian Zenker042580f2019-01-29 15:48:12 +010069 if (heap_buf == nullptr) {
Roger Meierd65216d2013-06-04 22:25:06 +020070#ifdef _MSC_VER
71 va_start(ap, message);
72 vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap);
73 va_end(ap);
74#endif
David Reiss01e55c12008-07-13 22:18:51 +000075 // Malloc failed. We might as well print the stack buffer.
76 f_(stack_buf);
77 return;
78 }
79
80 va_start(ap, message);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081 int rval = vsnprintf(heap_buf, need + 1, message, ap);
David Reiss01e55c12008-07-13 22:18:51 +000082 va_end(ap);
83 // TODO(shigin): inform user
84 if (rval != -1) {
85 f_(heap_buf);
86 }
87 free(heap_buf);
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040088#endif
89}
90
91void TOutput::errorTimeWrapper(const char* msg) {
92#ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT
93 time_t now;
94 char dbgtime[26];
95 time(&now);
96 THRIFT_CTIME_R(&now, dbgtime);
97 dbgtime[24] = 0;
98 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
99#endif
David Reiss01e55c12008-07-13 22:18:51 +0000100}
101
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100102void TOutput::perror(const char* message, int errno_copy) {
James E. King III9bea32f2018-03-16 16:07:42 -0400103 std::string out = message + std::string(": ") + strerror_s(errno_copy);
David Reiss01e55c12008-07-13 22:18:51 +0000104 f_(out.c_str());
105}
106
David Reiss9b209552008-04-08 06:26:05 +0000107std::string TOutput::strerror_s(int errno_copy) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100108 char b_errbuf[1024] = {'\0'};
Mario Emmenlauer04574372020-04-24 11:44:41 +0200109
110#ifdef HAVE_STRERROR_R
David Reiss9b209552008-04-08 06:26:05 +0000111#ifdef STRERROR_R_CHAR_P
Mario Emmenlauer04574372020-04-24 11:44:41 +0200112 char* b_error = ::strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
113#else // STRERROR_R_CHAR_P
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100114 char* b_error = b_errbuf;
David Reiss9b209552008-04-08 06:26:05 +0000115 int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
116 if (rv == -1) {
117 // strerror_r failed. omgwtfbbq.
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100118 return "XSI-compliant strerror_r() failed with errno = "
Jim Apple117a5cc2017-03-29 20:39:36 -0700119 + to_string(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000120 }
Mario Emmenlauer04574372020-04-24 11:44:41 +0200121#endif // STRERROR_R_CHAR_P
122#else // HAVE_STRERROR_R
123#ifdef _WIN32
124 const size_t size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
zeshuai00726681fb2020-06-03 17:24:38 +0800125 nullptr, errno_copy, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
126 b_errbuf, sizeof(b_errbuf), nullptr);
Mario Emmenlauer04574372020-04-24 11:44:41 +0200127
128 if (size > 2 && b_errbuf[size-2] == '\r' && b_errbuf[size-1] == '\n') {
129 b_errbuf[size-2] = '\0';
130 b_errbuf[size-1] = '\0';
131 }
132#else // _WIN32
133 ::strerror_s(b_errbuf, sizeof(b_errbuf), errno_copy);
134#endif // _WIN32
135 char* b_error = b_errbuf;
136#endif // HAVE_STRERROR_R
137
David Reiss9b209552008-04-08 06:26:05 +0000138 // Can anyone prove that explicit cast is probably not necessary
139 // to ensure that the string object is constructed before
140 // b_error becomes invalid?
141 return std::string(b_error);
David Reiss9b209552008-04-08 06:26:05 +0000142}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100143}
144} // apache::thrift