Apache Thrift C++ GlobalOutput 输出到哪里?

Where does the Apache Thrift C++ GlobalOutput output to?

我在我的 C++ 项目中使用 Apache Thrift TSimpleServer。一旦启动,服务器将监听客户端连接并处理请求。一切都很好,但偶尔服务器会毫无征兆地停止。

我遵循 thrift 库的源代码,可以看到 GlobalOutput(error_message)TTransportExceptionTException 被捕获时被创建。我需要了解这一点,以便在服务器死机时创建恢复机制。

这里是我说的源代码:

void TSimpleServer::serve() {

  shared_ptr<TTransport> client;
  shared_ptr<TTransport> inputTransport;
  shared_ptr<TTransport> outputTransport;
  shared_ptr<TProtocol> inputProtocol;
  shared_ptr<TProtocol> outputProtocol;

  // Start the server listening
  serverTransport_->listen();

  // Run the preServe event
  if (eventHandler_) {
    eventHandler_->preServe();
  }

  // Fetch client from server
  while (!stop_) {
    try {
      client = serverTransport_->accept();
      inputTransport = inputTransportFactory_->getTransport(client);
      outputTransport = outputTransportFactory_->getTransport(client);
      inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
      outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
    } catch (TTransportException& ttx) {
      if (inputTransport) { inputTransport->close(); }
      if (outputTransport) { outputTransport->close(); }
      if (client) { client->close(); }
      if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
          string errStr = string("TServerTransport died on accept: ") + ttx.what();
          GlobalOutput(errStr.c_str());
      }
      continue;
    } catch (TException& tx) {
      if (inputTransport) { inputTransport->close(); }
      if (outputTransport) { outputTransport->close(); }
      if (client) { client->close(); }
      string errStr = string("Some kind of accept exception: ") + tx.what();
      GlobalOutput(errStr.c_str());
      continue;
    } catch (string s) {
      if (inputTransport) { inputTransport->close(); }
      if (outputTransport) { outputTransport->close(); }
      if (client) { client->close(); }
      string errStr = string("Some kind of accept exception: ") + s;
      GlobalOutput(errStr.c_str());
      break;
    }

    // Get the processor
    shared_ptr<TProcessor> processor = getProcessor(inputProtocol,
                                                    outputProtocol, client);

    void* connectionContext = NULL;
    if (eventHandler_) {
      connectionContext = eventHandler_->createContext(inputProtocol, outputProtocol);
    }
    try {
      for (;;) {
        if (eventHandler_) {
          eventHandler_->processContext(connectionContext, client);
        }
        if (!processor->process(inputProtocol, outputProtocol,
                                connectionContext) ||
          // Peek ahead, is the remote side closed?
            !inputProtocol->getTransport()->peek()) {
          break;
        }
      }
    } catch (const TTransportException& ttx) {
      string errStr = string("TSimpleServer client died: ") + ttx.what();
      GlobalOutput(errStr.c_str());
    } catch (const std::exception& x) {
      GlobalOutput.printf("TSimpleServer exception: %s: %s",
                          typeid(x).name(), x.what());
    } catch (...) {
      GlobalOutput("TSimpleServer uncaught exception.");
    }
    if (eventHandler_) {
      eventHandler_->deleteContext(connectionContext, inputProtocol, outputProtocol);
    }

    try {
      inputTransport->close();
    } catch (const TTransportException& ttx) {
      string errStr = string("TSimpleServer input close failed: ")
        + ttx.what();
      GlobalOutput(errStr.c_str());
    }
    try {
      outputTransport->close();
    } catch (const TTransportException& ttx) {
      string errStr = string("TSimpleServer output close failed: ")
        + ttx.what();
      GlobalOutput(errStr.c_str());
    }
    try {
      client->close();
    } catch (const TTransportException& ttx) {
      string errStr = string("TSimpleServer client close failed: ")
        + ttx.what();
      GlobalOutput(errStr.c_str());
    }
  }

  if (stop_) {
    try {
      serverTransport_->close();
    } catch (TTransportException &ttx) {
      string errStr = string("TServerTransport failed on close: ") + ttx.what();
      GlobalOutput(errStr.c_str());
    }
    stop_ = false;
  }
}

在 TOutput.cpp 的深处有行 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg); (source here) 并且这是默认情况下所有 Thrift GlobalOutput 消息结束的地方(在标准错误中)。

但是您可以通过以函数指针的形式向 GlobalOutput 提供自己的处理程序来更改它(如果出于任何原因您不能使用 stderr):

void myOutputFunction(const char* x)
{
  fprintf(myLogFile, "Thrift internal message: %s\n", x);
}

// Inside some init function or main
GlobalOutput.setOutputFunction(myOutputFunction);