Other: added a few log entries related to gRPC server and client config files.

This commit is contained in:
Xavier Michelon
2022-10-13 08:49:00 +02:00
parent ee515394c0
commit d1a7ca7822
5 changed files with 53 additions and 23 deletions

View File

@ -268,10 +268,12 @@ int main(int argc, char *argv[])
launchBridge(args); launchBridge(args);
} }
app().backend().init(GRPCClient::waitAndRetrieveServiceConfig(attach ? 0 : grpcServiceConfigWaitDelayMs));
log.debug(QString("Server configuration file will be loaded from '%1'").arg(QDir::toNativeSeparators(grpcServerConfigPath())));
app().backend().init(GRPCClient::waitAndRetrieveServiceConfig(attach ? 0 : grpcServiceConfigWaitDelayMs));
if (!attach) if (!attach)
GRPCClient::removeServiceConfigFile(); GRPCClient::removeServiceConfigFile();
log.debug("Backend was successfully initialized.");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
std::unique_ptr<QQmlComponent> rootComponent(createRootQmlComponent(engine)); std::unique_ptr<QQmlComponent> rootComponent(createRootQmlComponent(engine));

View File

@ -123,8 +123,7 @@ bool GRPCClient::connectToServer(GRPCConfig const &config, QString &outError)
int i = 0; int i = 0;
while (true) while (true)
{ {
if (log_) this->logDebug(QString("Connection to gRPC server at %1. attempt #%2").arg(address).arg(++i));
log_->debug(QString("Connection to gRPC server at %1. attempt #%2").arg(address).arg(++i));
if (channel_->WaitForConnected(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(grpcConnectionRetryDelayMs, GPR_TIMESPAN)))) if (channel_->WaitForConnected(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(grpcConnectionRetryDelayMs, GPR_TIMESPAN))))
break; // connection established. break; // connection established.
@ -136,13 +135,13 @@ bool GRPCClient::connectToServer(GRPCConfig const &config, QString &outError)
if (channel_->GetState(true) != GRPC_CHANNEL_READY) if (channel_->GetState(true) != GRPC_CHANNEL_READY)
throw Exception("connection check failed."); throw Exception("connection check failed.");
if (log_) this->logDebug("Successfully connected to gRPC server.");
log_->debug("Successfully connected to gRPC server.");
QString const clientToken = QUuid::createUuid().toString(); QString const clientToken = QUuid::createUuid().toString();
QString clientConfigPath = createClientConfigFile(clientToken); QString clientConfigPath = createClientConfigFile(clientToken);
if (clientConfigPath.isEmpty()) if (clientConfigPath.isEmpty())
throw Exception("gRPC client config could not be saved."); throw Exception("gRPC client config could not be saved.");
this->logDebug(QString("Client config file was saved to '%1'").arg(QDir::toNativeSeparators(clientConfigPath)));
QString returnedClientToken; QString returnedClientToken;
grpc::Status status = this->checkTokens(QDir::toNativeSeparators(clientConfigPath), returnedClientToken); grpc::Status status = this->checkTokens(QDir::toNativeSeparators(clientConfigPath), returnedClientToken);
@ -874,8 +873,7 @@ grpc::Status GRPCClient::runEventStreamReader()
this->processUserEvent(event.user()); this->processUserEvent(event.user());
break; break;
default: default:
if (log_) this->logDebug(QString("Unknown stream event type: %1").arg(event.event_case()));
log_->debug(QString("Unknown stream event type: %1").arg(event.event_case()));
} }
} }
@ -897,13 +895,32 @@ grpc::Status GRPCClient::stopEventStreamReader()
} }
//****************************************************************************************************************************************************
/// \param[in] level The level of the event.
/// \param[in] message The event message.
//****************************************************************************************************************************************************
void GRPCClient::log(Log::Level level, QString const &message)
{
if (log_)
log_->addEntry(level, message);
}
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
/// \param[in] message The event message. /// \param[in] message The event message.
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
void GRPCClient::logTrace(QString const &message) void GRPCClient::logTrace(QString const &message)
{ {
if (log_) this->log(Log::Level::Trace, message);
log_->trace(message); }
//****************************************************************************************************************************************************
/// \param[in] message The event message.
//****************************************************************************************************************************************************
void GRPCClient::logDebug(QString const &message)
{
this->log(Log::Level::Debug, message);
} }
@ -912,8 +929,7 @@ void GRPCClient::logTrace(QString const &message)
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
void GRPCClient::logError(QString const &message) void GRPCClient::logError(QString const &message)
{ {
if (log_) this->log(Log::Level::Error, message);
log_->error(message);
} }

View File

@ -207,8 +207,10 @@ public:
grpc::Status stopEventStreamReader(); ///< Stop the event stream. grpc::Status stopEventStreamReader(); ///< Stop the event stream.
private: private:
void logTrace(QString const &message); ///< Log an event. void log(Log::Level level, QString const & message); ///< Log an event.
void logError(QString const &message); ///< Log an event. void logTrace(QString const &message); ///< Log a trace event.
void logDebug(QString const &message); ///< Log a debug event.
void logError(QString const &message); ///< Log an error event.
grpc::Status logGRPCCallStatus(grpc::Status const &status, QString const &callName, QList<grpc::StatusCode> allowedErrors = {}); ///< Log the status of a gRPC code. grpc::Status logGRPCCallStatus(grpc::Status const &status, QString const &callName, QList<grpc::StatusCode> allowedErrors = {}); ///< Log the status of a gRPC code.
grpc::Status simpleMethod(SimpleMethod method); ///< perform a gRPC call to a bool setter. grpc::Status simpleMethod(SimpleMethod method); ///< perform a gRPC call to a bool setter.
grpc::Status setBool(BoolSetter setter, bool value); ///< perform a gRPC call to a bool setter. grpc::Status setBool(BoolSetter setter, bool value); ///< perform a gRPC call to a bool setter.

View File

@ -124,7 +124,7 @@ func NewService(
func (s *Service) startGRPCServer() { func (s *Service) startGRPCServer() {
tlsConfig, pemCert, err := s.generateTLSConfig() tlsConfig, pemCert, err := s.generateTLSConfig()
if err != nil { if err != nil {
s.log.WithError(err).Panic("could not generate gRPC TLS config") s.log.WithError(err).Panic("Could not generate gRPC TLS config")
} }
s.pemCert = string(pemCert) s.pemCert = string(pemCert)
@ -135,11 +135,13 @@ func (s *Service) startGRPCServer() {
s.listener, err = net.Listen("tcp", "127.0.0.1:0") // Port 0 means that the port is randomly picked by the system. s.listener, err = net.Listen("tcp", "127.0.0.1:0") // Port 0 means that the port is randomly picked by the system.
if err != nil { if err != nil {
s.log.WithError(err).Panic("could not create gRPC listener") s.log.WithError(err).Panic("Could not create gRPC listener")
} }
if err := s.saveGRPCServerConfigFile(); err != nil { if path, err := s.saveGRPCServerConfigFile(); err != nil {
s.log.WithError(err).Panic("could not write gRPC service configuration file") s.log.WithError(err).WithField("path", path).Panic("Could not write gRPC service config file")
} else {
s.log.WithField("path", path).Debug("Successfully saved gRPC service config file")
} }
s.log.Info("gRPC server listening at ", s.listener.Addr()) s.log.Info("gRPC server listening at ", s.listener.Addr())
@ -435,10 +437,10 @@ func (s *Service) generateTLSConfig() (tlsConfig *cryptotls.Config, pemCert []by
return tlsConfig, pemCert, nil return tlsConfig, pemCert, nil
} }
func (s *Service) saveGRPCServerConfigFile() error { func (s *Service) saveGRPCServerConfigFile() (string, error) {
address, ok := s.listener.Addr().(*net.TCPAddr) address, ok := s.listener.Addr().(*net.TCPAddr)
if !ok { if !ok {
return fmt.Errorf("could not retrieve gRPC service listener address") return "", fmt.Errorf("could not retrieve gRPC service listener address")
} }
sc := config{ sc := config{
@ -449,8 +451,10 @@ func (s *Service) saveGRPCServerConfigFile() error {
settingsPath, err := s.locations.ProvideSettingsPath() settingsPath, err := s.locations.ProvideSettingsPath()
if err != nil { if err != nil {
return err return "", err
} }
return sc.save(filepath.Join(settingsPath, serverConfigFileName)) configPath := filepath.Join(settingsPath, serverConfigFileName)
return configPath, sc.save(configPath)
} }

View File

@ -47,12 +47,18 @@ func (s *Service) CheckTokens(ctx context.Context, clientConfigPath *wrapperspb.
return nil, err return nil, err
} }
path := clientConfigPath.Value
logEntry := s.log.WithField("path", path)
var clientConfig config var clientConfig config
if err := clientConfig.load(clientConfigPath.Value); err != nil { if err := clientConfig.load(path); err != nil {
s.log.WithError(err).Error("could not read gRPC client config file") logEntry.WithError(err).Error("Could not read gRPC client config file")
return nil, err return nil, err
} }
logEntry.Info("gRPC client config file was successfully loaded")
return &wrapperspb.StringValue{Value: clientConfig.Token}, nil return &wrapperspb.StringValue{Value: clientConfig.Token}, nil
} }