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> |
David Reiss | a4d7eef | 2008-06-11 01:18:20 +0000 | [diff] [blame] | 21 | #include <cstring> |
Bryan Duxbury | 7a0825c | 2010-09-02 16:42:18 +0000 | [diff] [blame] | 22 | #include <cstdlib> |
David Reiss | a177109 | 2008-04-11 22:36:31 +0000 | [diff] [blame] | 23 | #include <boost/lexical_cast.hpp> |
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 | |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 30 | TOutput GlobalOutput; |
| 31 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 32 | void TOutput::printf(const char* message, ...) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 33 | #ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 34 | // Try to reduce heap usage, even if printf is called rarely. |
| 35 | static const int STACK_BUF_SIZE = 256; |
| 36 | char stack_buf[STACK_BUF_SIZE]; |
| 37 | va_list ap; |
| 38 | |
Roger Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 39 | #ifdef _MSC_VER |
| 40 | va_start(ap, message); |
| 41 | int need = _vscprintf(message, ap); |
| 42 | va_end(ap); |
| 43 | |
| 44 | if (need < STACK_BUF_SIZE) { |
| 45 | va_start(ap, message); |
| 46 | vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap); |
| 47 | va_end(ap); |
| 48 | f_(stack_buf); |
| 49 | return; |
| 50 | } |
| 51 | #else |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 52 | va_start(ap, message); |
| 53 | int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap); |
| 54 | va_end(ap); |
| 55 | |
| 56 | if (need < STACK_BUF_SIZE) { |
| 57 | f_(stack_buf); |
| 58 | return; |
| 59 | } |
Roger Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 60 | #endif |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 61 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 62 | char* heap_buf = (char*)malloc((need + 1) * sizeof(char)); |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 63 | if (heap_buf == NULL) { |
Roger Meier | d65216d | 2013-06-04 22:25:06 +0200 | [diff] [blame] | 64 | #ifdef _MSC_VER |
| 65 | va_start(ap, message); |
| 66 | vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap); |
| 67 | va_end(ap); |
| 68 | #endif |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 69 | // Malloc failed. We might as well print the stack buffer. |
| 70 | f_(stack_buf); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | va_start(ap, message); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 75 | int rval = vsnprintf(heap_buf, need + 1, message, ap); |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 76 | va_end(ap); |
| 77 | // TODO(shigin): inform user |
| 78 | if (rval != -1) { |
| 79 | f_(heap_buf); |
| 80 | } |
| 81 | free(heap_buf); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 82 | #endif |
| 83 | } |
| 84 | |
| 85 | void TOutput::errorTimeWrapper(const char* msg) { |
| 86 | #ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT |
| 87 | time_t now; |
| 88 | char dbgtime[26]; |
| 89 | time(&now); |
| 90 | THRIFT_CTIME_R(&now, dbgtime); |
| 91 | dbgtime[24] = 0; |
| 92 | fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg); |
| 93 | #endif |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 96 | void TOutput::perror(const char* message, int errno_copy) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 97 | std::string out = message + strerror_s(errno_copy); |
| 98 | f_(out.c_str()); |
| 99 | } |
| 100 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 101 | std::string TOutput::strerror_s(int errno_copy) { |
| 102 | #ifndef HAVE_STRERROR_R |
David Reiss | 826e648 | 2008-10-20 21:29:07 +0000 | [diff] [blame] | 103 | return "errno = " + boost::lexical_cast<std::string>(errno_copy); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 104 | #else // HAVE_STRERROR_R |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 105 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 106 | char b_errbuf[1024] = {'\0'}; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 107 | #ifdef STRERROR_R_CHAR_P |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 108 | char* b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf)); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 109 | #else |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 110 | char* b_error = b_errbuf; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 111 | int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf)); |
| 112 | if (rv == -1) { |
| 113 | // strerror_r failed. omgwtfbbq. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 114 | return "XSI-compliant strerror_r() failed with errno = " |
| 115 | + boost::lexical_cast<std::string>(errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 116 | } |
| 117 | #endif |
| 118 | // Can anyone prove that explicit cast is probably not necessary |
| 119 | // to ensure that the string object is constructed before |
| 120 | // b_error becomes invalid? |
| 121 | return std::string(b_error); |
| 122 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 123 | #endif // HAVE_STRERROR_R |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 124 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 125 | } |
| 126 | } // apache::thrift |