Skip to content

Commit 6854d14

Browse files
committed
THRIFT-5898: Replace global variable with a singleton getter
Client: cpp
1 parent e720e6f commit 6854d14

24 files changed

+195
-191
lines changed

contrib/transport-sample/ThriftCommon.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace thriftcommon
77
{
88
//----------------------------------------------------------------------------
99
//Launch child process and pass R/W anonymous pipe handles on cmd line.
10-
//This is a simple example and does not include elevation or other
10+
//This is a simple example and does not include elevation or other
1111
//advanced features.
1212
//
1313
bool LaunchAnonPipeChild(std::string app, boost::shared_ptr<TServerTransport> transport)
@@ -25,7 +25,7 @@ namespace thriftcommon
2525
//spawn the child process
2626
if (!CreateProcessA(nullptr, handles, nullptr,nullptr,TRUE,0,nullptr,nullptr,&si,&pi))
2727
{
28-
GlobalOutput.perror("TPipeServer CreateProcess failed, GLE=", GetLastError());
28+
TOutput::instance().perror("TPipeServer CreateProcess failed, GLE=", GetLastError());
2929
return false;
3030
}
3131

lib/cpp/src/thrift/TDispatchProcessor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class TDispatchProcessorT : public TProcessor {
6161
// (The old generated processor code used to try to skip a T_STRUCT and
6262
// continue. However, that seems unsafe.)
6363
if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
64-
GlobalOutput.printf("received invalid message type %d from client", mtype);
64+
TOutput::instance().printf("received invalid message type %d from client", mtype);
6565
return false;
6666
}
6767

@@ -76,7 +76,7 @@ class TDispatchProcessorT : public TProcessor {
7676
in->readMessageBegin(fname, mtype, seqid);
7777

7878
if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
79-
GlobalOutput.printf("received invalid message type %d from client", mtype);
79+
TOutput::instance().printf("received invalid message type %d from client", mtype);
8080
return false;
8181
}
8282

@@ -114,7 +114,7 @@ class TDispatchProcessor : public TProcessor {
114114
in->readMessageBegin(fname, mtype, seqid);
115115

116116
if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
117-
GlobalOutput.printf("received invalid message type %d from client", mtype);
117+
TOutput::instance().printf("received invalid message type %d from client", mtype);
118118
return false;
119119
}
120120

lib/cpp/src/thrift/TOutput.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
namespace apache {
3232
namespace thrift {
3333

34-
/*THRIFT_EXPORT*/ TOutput GlobalOutput; // if you need this exported, build your own wrapper lib around and export it yourself
35-
3634
TOutput::TOutput() : f_(&errorTimeWrapper) {}
3735

3836
void TOutput::printf(const char* message, ...) {
@@ -144,5 +142,11 @@ std::string TOutput::strerror_s(int errno_copy) {
144142
return std::string(b_error);
145143
#endif // __ZEPHYR__
146144
}
145+
146+
TOutput& TOutput::instance() {
147+
static TOutput instance;
148+
return instance;
149+
}
150+
147151
}
148152
} // apache::thrift

lib/cpp/src/thrift/TOutput.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#ifndef _THRIFT_OUTPUT_H_
2121
#define _THRIFT_OUTPUT_H_ 1
2222

23-
//#include <thrift/thrift_export.h>
24-
2523
namespace apache {
2624
namespace thrift {
2725

@@ -49,11 +47,13 @@ class TOutput {
4947
/** Just like strerror_r but returns a C++ string object. */
5048
static std::string strerror_s(int errno_copy);
5149

50+
/** Get a singleton instance of the global TOutput object used by
51+
* the library internally. */
52+
static TOutput& instance();
53+
5254
private:
5355
void (*f_)(const char*);
5456
};
55-
56-
/*THRIFT_EXPORT*/ extern TOutput GlobalOutput; // if you need this exported, build your own wrapper lib around and export it yourself
5757
}
5858
} // namespace apache::thrift
5959

lib/cpp/src/thrift/async/TAsyncDispatchProcessor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TAsyncDispatchProcessorT : public TAsyncProcessor {
6262
// (The old generated processor code used to try to skip a T_STRUCT and
6363
// continue. However, that seems unsafe.)
6464
if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
65-
GlobalOutput.printf("received invalid message type %d from client", mtype);
65+
TOutput::instance().printf("received invalid message type %d from client", mtype);
6666
_return(false);
6767
return;
6868
}
@@ -79,7 +79,7 @@ class TAsyncDispatchProcessorT : public TAsyncProcessor {
7979
in->readMessageBegin(fname, mtype, seqid);
8080

8181
if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
82-
GlobalOutput.printf("received invalid message type %d from client", mtype);
82+
TOutput::instance().printf("received invalid message type %d from client", mtype);
8383
_return(false);
8484
return;
8585
}
@@ -123,7 +123,7 @@ class TAsyncDispatchProcessor : public TAsyncProcessor {
123123
// (The old generated processor code used to try to skip a T_STRUCT and
124124
// continue. However, that seems unsafe.)
125125
if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
126-
GlobalOutput.printf("received invalid message type %d from client", mtype);
126+
TOutput::instance().printf("received invalid message type %d from client", mtype);
127127
_return(false);
128128
return;
129129
}

lib/cpp/src/thrift/concurrency/ThreadManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ class ThreadManager::Worker : public Runnable {
309309
try {
310310
task->run();
311311
} catch (const std::exception& e) {
312-
GlobalOutput.printf("[ERROR] task->run() raised an exception: %s", e.what());
312+
TOutput::instance().printf("[ERROR] task->run() raised an exception: %s", e.what());
313313
} catch (...) {
314-
GlobalOutput.printf("[ERROR] task->run() raised an unknown exception");
314+
TOutput::instance().printf("[ERROR] task->run() raised an unknown exception");
315315
}
316316

317317
// Re-acquire the lock to proceed in the thread manager

lib/cpp/src/thrift/server/TConnectedClient.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ void TConnectedClient::run() {
7575
// All other transport exceptions are logged.
7676
// State of connection is unknown. Done.
7777
string errStr = string("TConnectedClient died: ") + ttx.what();
78-
GlobalOutput(errStr.c_str());
78+
TOutput::instance()(errStr.c_str());
7979
done = true;
8080
break;
8181
}
8282
}
8383
} catch (const TException& tex) {
8484
string errStr = string("TConnectedClient processing exception: ") + tex.what();
85-
GlobalOutput(errStr.c_str());
85+
TOutput::instance()(errStr.c_str());
8686
// Disconnect from client, because we could not process the message.
8787
done = true;
8888
}
@@ -100,21 +100,21 @@ void TConnectedClient::cleanup() {
100100
inputProtocol_->getTransport()->close();
101101
} catch (const TTransportException& ttx) {
102102
string errStr = string("TConnectedClient input close failed: ") + ttx.what();
103-
GlobalOutput(errStr.c_str());
103+
TOutput::instance()(errStr.c_str());
104104
}
105105

106106
try {
107107
outputProtocol_->getTransport()->close();
108108
} catch (const TTransportException& ttx) {
109109
string errStr = string("TConnectedClient output close failed: ") + ttx.what();
110-
GlobalOutput(errStr.c_str());
110+
TOutput::instance()(errStr.c_str());
111111
}
112112

113113
try {
114114
client_->close();
115115
} catch (const TTransportException& ttx) {
116116
string errStr = string("TConnectedClient client close failed: ") + ttx.what();
117-
GlobalOutput(errStr.c_str());
117+
TOutput::instance()(errStr.c_str());
118118
}
119119
}
120120
}

0 commit comments

Comments
 (0)