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 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 20 | #include <Thrift.h> |
David Reiss | a4d7eef | 2008-06-11 01:18:20 +0000 | [diff] [blame] | 21 | #include <cstring> |
David Reiss | a177109 | 2008-04-11 22:36:31 +0000 | [diff] [blame] | 22 | #include <boost/lexical_cast.hpp> |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 23 | #include <protocol/TProtocol.h> |
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 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 27 | namespace apache { namespace thrift { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 28 | |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 29 | TOutput GlobalOutput; |
| 30 | |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 31 | void TOutput::printf(const char *message, ...) { |
| 32 | // Try to reduce heap usage, even if printf is called rarely. |
| 33 | static const int STACK_BUF_SIZE = 256; |
| 34 | char stack_buf[STACK_BUF_SIZE]; |
| 35 | va_list ap; |
| 36 | |
| 37 | va_start(ap, message); |
| 38 | int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap); |
| 39 | va_end(ap); |
| 40 | |
| 41 | if (need < STACK_BUF_SIZE) { |
| 42 | f_(stack_buf); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | char *heap_buf = (char*)malloc((need+1) * sizeof(char)); |
| 47 | if (heap_buf == NULL) { |
| 48 | // Malloc failed. We might as well print the stack buffer. |
| 49 | f_(stack_buf); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | va_start(ap, message); |
| 54 | int rval = vsnprintf(heap_buf, need+1, message, ap); |
| 55 | va_end(ap); |
| 56 | // TODO(shigin): inform user |
| 57 | if (rval != -1) { |
| 58 | f_(heap_buf); |
| 59 | } |
| 60 | free(heap_buf); |
| 61 | } |
| 62 | |
| 63 | void TOutput::perror(const char *message, int errno_copy) { |
| 64 | std::string out = message + strerror_s(errno_copy); |
| 65 | f_(out.c_str()); |
| 66 | } |
| 67 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 68 | std::string TOutput::strerror_s(int errno_copy) { |
| 69 | #ifndef HAVE_STRERROR_R |
David Reiss | 826e648 | 2008-10-20 21:29:07 +0000 | [diff] [blame] | 70 | return "errno = " + boost::lexical_cast<std::string>(errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 71 | #else // HAVE_STRERROR_R |
| 72 | |
| 73 | char b_errbuf[1024] = { '\0' }; |
| 74 | #ifdef STRERROR_R_CHAR_P |
| 75 | char *b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf)); |
| 76 | #else |
| 77 | char *b_error = b_errbuf; |
| 78 | int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf)); |
| 79 | if (rv == -1) { |
| 80 | // strerror_r failed. omgwtfbbq. |
| 81 | return "XSI-compliant strerror_r() failed with errno = " + |
David Reiss | a177109 | 2008-04-11 22:36:31 +0000 | [diff] [blame] | 82 | boost::lexical_cast<std::string>(errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 83 | } |
| 84 | #endif |
| 85 | // Can anyone prove that explicit cast is probably not necessary |
| 86 | // to ensure that the string object is constructed before |
| 87 | // b_error becomes invalid? |
| 88 | return std::string(b_error); |
| 89 | |
| 90 | #endif // HAVE_STRERROR_R |
| 91 | } |
| 92 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 93 | uint32_t TApplicationException::read(apache::thrift::protocol::TProtocol* iprot) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 94 | uint32_t xfer = 0; |
| 95 | std::string fname; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 96 | apache::thrift::protocol::TType ftype; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 97 | int16_t fid; |
| 98 | |
| 99 | xfer += iprot->readStructBegin(fname); |
| 100 | |
| 101 | while (true) { |
| 102 | xfer += iprot->readFieldBegin(fname, ftype, fid); |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 103 | if (ftype == apache::thrift::protocol::T_STOP) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | switch (fid) { |
| 107 | case 1: |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 108 | if (ftype == apache::thrift::protocol::T_STRING) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 109 | xfer += iprot->readString(message_); |
| 110 | } else { |
| 111 | xfer += iprot->skip(ftype); |
| 112 | } |
| 113 | break; |
| 114 | case 2: |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 115 | if (ftype == apache::thrift::protocol::T_I32) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 116 | int32_t type; |
| 117 | xfer += iprot->readI32(type); |
| 118 | type_ = (TApplicationExceptionType)type; |
| 119 | } else { |
| 120 | xfer += iprot->skip(ftype); |
| 121 | } |
| 122 | break; |
| 123 | default: |
| 124 | xfer += iprot->skip(ftype); |
| 125 | break; |
| 126 | } |
| 127 | xfer += iprot->readFieldEnd(); |
| 128 | } |
| 129 | |
| 130 | xfer += iprot->readStructEnd(); |
| 131 | return xfer; |
| 132 | } |
| 133 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 134 | uint32_t TApplicationException::write(apache::thrift::protocol::TProtocol* oprot) const { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 135 | uint32_t xfer = 0; |
| 136 | xfer += oprot->writeStructBegin("TApplicationException"); |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 137 | xfer += oprot->writeFieldBegin("message", apache::thrift::protocol::T_STRING, 1); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 138 | xfer += oprot->writeString(message_); |
| 139 | xfer += oprot->writeFieldEnd(); |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 140 | xfer += oprot->writeFieldBegin("type", apache::thrift::protocol::T_I32, 2); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 141 | xfer += oprot->writeI32(type_); |
| 142 | xfer += oprot->writeFieldEnd(); |
| 143 | xfer += oprot->writeFieldStop(); |
| 144 | xfer += oprot->writeStructEnd(); |
| 145 | return xfer; |
| 146 | } |
| 147 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 148 | }} // apache::thrift |