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  */
19 
20 #include <thrift/Thrift.h>
21 #include <thrift/TToString.h>
22 #include <cstring>
23 #include <cstdlib>
24 #include <stdarg.h>
25 #include <stdio.h>
26 
27 #ifdef _WIN32
28 #include <windows.h>
29 #endif
30 
31 namespace apache {
32 namespace thrift {
33 
34 THRIFT_EXPORT TOutput GlobalOutput;
35 
TOutput()36 TOutput::TOutput() : f_(&errorTimeWrapper) {}
37 
printf(const char * message,...)38 void TOutput::printf(const char* message, ...) {
39 #ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT
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 
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
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   }
66 #endif
67 
68   char* heap_buf = (char*)malloc((need + 1) * sizeof(char));
69   if (heap_buf == nullptr) {
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
75     // Malloc failed.  We might as well print the stack buffer.
76     f_(stack_buf);
77     return;
78   }
79 
80   va_start(ap, message);
81   int rval = vsnprintf(heap_buf, need + 1, message, ap);
82   va_end(ap);
83   // TODO(shigin): inform user
84   if (rval != -1) {
85     f_(heap_buf);
86   }
87   free(heap_buf);
88 #endif
89 }
90 
errorTimeWrapper(const char * msg)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
100 }
101 
perror(const char * message,int errno_copy)102 void TOutput::perror(const char* message, int errno_copy) {
103   std::string out = message + std::string(": ") + strerror_s(errno_copy);
104   f_(out.c_str());
105 }
106 
strerror_s(int errno_copy)107 std::string TOutput::strerror_s(int errno_copy) {
108 #ifdef __ZEPHYR__
109   return std::string(strerror(errno_copy));
110 #else
111   char b_errbuf[1024] = {'\0'};
112 
113 #ifdef HAVE_STRERROR_R
114 #ifdef STRERROR_R_CHAR_P
115   char* b_error = ::strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
116 #else // STRERROR_R_CHAR_P
117   char* b_error = b_errbuf;
118   int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
119   if (rv == -1) {
120     // strerror_r failed.  omgwtfbbq.
121     return "XSI-compliant strerror_r() failed with errno = "
122            + to_string(errno_copy);
123   }
124 #endif // STRERROR_R_CHAR_P
125 #else // HAVE_STRERROR_R
126 #ifdef _WIN32
127   const size_t size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
128                                      nullptr, errno_copy, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
129                                      b_errbuf, sizeof(b_errbuf), nullptr);
130 
131   if (size > 2 && b_errbuf[size-2] == '\r' && b_errbuf[size-1] == '\n') {
132     b_errbuf[size-2] = '\0';
133     b_errbuf[size-1] = '\0';
134   }
135 #else // _WIN32
136   ::strerror_s(b_errbuf, sizeof(b_errbuf), errno_copy);
137 #endif // _WIN32
138   char* b_error = b_errbuf;
139 #endif // HAVE_STRERROR_R
140 
141   // Can anyone prove that explicit cast is probably not necessary
142   // to ensure that the string object is constructed before
143   // b_error becomes invalid?
144   return std::string(b_error);
145 #endif // __ZEPHYR__
146 }
147 }
148 } // apache::thrift
149