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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 27 | namespace apache { |
| 28 | namespace thrift { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 29 | |
cyy | 8fdb758 | 2019-02-05 02:57:21 +0800 | [diff] [blame^] | 30 | THRIFT_EXPORT TOutput GlobalOutput; |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 31 | |
James E. King III | 278528c | 2019-01-11 12:17:44 -0500 | [diff] [blame] | 32 | TOutput::TOutput() : f_(&errorTimeWrapper) {} |
| 33 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 34 | void TOutput::printf(const char* message, ...) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 35 | #ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 36 | // 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 Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 41 | #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 Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 54 | 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 Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 62 | #endif |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 63 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 64 | char* heap_buf = (char*)malloc((need + 1) * sizeof(char)); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 65 | if (heap_buf == nullptr) { |
Roger Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 66 | #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 Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 71 | // Malloc failed. We might as well print the stack buffer. |
| 72 | f_(stack_buf); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | va_start(ap, message); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 77 | int rval = vsnprintf(heap_buf, need + 1, message, ap); |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 78 | va_end(ap); |
| 79 | // TODO(shigin): inform user |
| 80 | if (rval != -1) { |
| 81 | f_(heap_buf); |
| 82 | } |
| 83 | free(heap_buf); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 84 | #endif |
| 85 | } |
| 86 | |
| 87 | void 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 Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 98 | void TOutput::perror(const char* message, int errno_copy) { |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 99 | std::string out = message + std::string(": ") + strerror_s(errno_copy); |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 100 | f_(out.c_str()); |
| 101 | } |
| 102 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 103 | std::string TOutput::strerror_s(int errno_copy) { |
| 104 | #ifndef HAVE_STRERROR_R |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 105 | return "errno = " + to_string(errno_copy); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 106 | #else // HAVE_STRERROR_R |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 107 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 108 | char b_errbuf[1024] = {'\0'}; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 109 | #ifdef STRERROR_R_CHAR_P |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 110 | char* b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf)); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 111 | #else |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 112 | char* b_error = b_errbuf; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 113 | int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf)); |
| 114 | if (rv == -1) { |
| 115 | // strerror_r failed. omgwtfbbq. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 116 | return "XSI-compliant strerror_r() failed with errno = " |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 117 | + to_string(errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 118 | } |
| 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 125 | #endif // HAVE_STRERROR_R |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 126 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 127 | } |
| 128 | } // apache::thrift |