David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 20 | #include <thrift/Thrift.h> |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 21 | #include <thrift/TToString.h> |
David Reiss | a4d7eef | 2008-06-11 01:18:20 +0000 | [diff] [blame] | 22 | #include <cstring> |
Bryan Duxbury | 7a0825c | 2010-09-02 16:42:18 +0000 | [diff] [blame] | 23 | #include <cstdlib> |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 26 | |
Mario Emmenlauer | 0457437 | 2020-04-24 11:44:41 +0200 | [diff] [blame] | 27 | #ifdef _WIN32 |
| 28 | #include <windows.h> |
| 29 | #endif |
| 30 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 31 | namespace apache { |
| 32 | namespace thrift { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 33 | |
cyy | 8fdb758 | 2019-02-05 02:57:21 +0800 | [diff] [blame] | 34 | THRIFT_EXPORT TOutput GlobalOutput; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 35 | |
James E. King III | 278528c | 2019-01-11 12:17:44 -0500 | [diff] [blame] | 36 | TOutput::TOutput() : f_(&errorTimeWrapper) {} |
| 37 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 38 | void TOutput::printf(const char* message, ...) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 39 | #ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 40 | // 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 Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 45 | #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 Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 58 | 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 Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 66 | #endif |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 67 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 68 | char* heap_buf = (char*)malloc((need + 1) * sizeof(char)); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 69 | if (heap_buf == nullptr) { |
Roger Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 70 | #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 Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 75 | // Malloc failed. We might as well print the stack buffer. |
| 76 | f_(stack_buf); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | va_start(ap, message); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 81 | int rval = vsnprintf(heap_buf, need + 1, message, ap); |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 82 | va_end(ap); |
| 83 | // TODO(shigin): inform user |
| 84 | if (rval != -1) { |
| 85 | f_(heap_buf); |
| 86 | } |
| 87 | free(heap_buf); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 88 | #endif |
| 89 | } |
| 90 | |
| 91 | void 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 Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 102 | void TOutput::perror(const char* message, int errno_copy) { |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 103 | std::string out = message + std::string(": ") + strerror_s(errno_copy); |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 104 | f_(out.c_str()); |
| 105 | } |
| 106 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 107 | std::string TOutput::strerror_s(int errno_copy) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 108 | char b_errbuf[1024] = {'\0'}; |
Mario Emmenlauer | 0457437 | 2020-04-24 11:44:41 +0200 | [diff] [blame] | 109 | |
| 110 | #ifdef HAVE_STRERROR_R |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 111 | #ifdef STRERROR_R_CHAR_P |
Mario Emmenlauer | 0457437 | 2020-04-24 11:44:41 +0200 | [diff] [blame] | 112 | char* b_error = ::strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf)); |
| 113 | #else // STRERROR_R_CHAR_P |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 114 | char* b_error = b_errbuf; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 115 | int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf)); |
| 116 | if (rv == -1) { |
| 117 | // strerror_r failed. omgwtfbbq. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 118 | return "XSI-compliant strerror_r() failed with errno = " |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 119 | + to_string(errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 120 | } |
Mario Emmenlauer | 0457437 | 2020-04-24 11:44:41 +0200 | [diff] [blame] | 121 | #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, |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 125 | nullptr, errno_copy, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 126 | b_errbuf, sizeof(b_errbuf), nullptr); |
Mario Emmenlauer | 0457437 | 2020-04-24 11:44:41 +0200 | [diff] [blame] | 127 | |
| 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 Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 138 | // 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 Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 142 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 143 | } |
| 144 | } // apache::thrift |