feat(GODT-2705): added log entries for focus service on client and server sides.

This commit is contained in:
Xavier Michelon
2023-06-22 11:19:20 +02:00
committed by Xavier Michelon
parent ab70e85f1c
commit ae65385c38
4 changed files with 24 additions and 7 deletions

View File

@ -45,6 +45,7 @@ type Service struct {
raiseCh chan struct{} raiseCh chan struct{}
version *semver.Version version *semver.Version
log *logrus.Entry
panicHandler async.PanicHandler panicHandler async.PanicHandler
} }
@ -55,13 +56,14 @@ func NewService(locator service.Locator, version *semver.Version, panicHandler a
server: grpc.NewServer(), server: grpc.NewServer(),
raiseCh: make(chan struct{}, 1), raiseCh: make(chan struct{}, 1),
version: version, version: version,
log: logrus.WithField("pkg", "focus/service"),
panicHandler: panicHandler, panicHandler: panicHandler,
} }
proto.RegisterFocusServer(serv.server, serv) proto.RegisterFocusServer(serv.server, serv)
if listener, err := net.Listen("tcp", net.JoinHostPort(Host, fmt.Sprint(0))); err != nil { if listener, err := net.Listen("tcp", net.JoinHostPort(Host, fmt.Sprint(0))); err != nil {
logrus.WithError(err).Warn("Failed to start focus serv") serv.log.WithError(err).Warn("Failed to start focus service")
} else { } else {
config := service.Config{} config := service.Config{}
// retrieve the port assigned by the system, so that we can put it in the config file. // retrieve the port assigned by the system, so that we can put it in the config file.
@ -71,9 +73,9 @@ func NewService(locator service.Locator, version *semver.Version, panicHandler a
} }
config.Port = address.Port config.Port = address.Port
if path, err := service.SaveGRPCServerConfigFile(locator, &config, serverConfigFileName); err != nil { if path, err := service.SaveGRPCServerConfigFile(locator, &config, serverConfigFileName); err != nil {
logrus.WithError(err).WithField("path", path).Warn("Could not write focus gRPC service config file") serv.log.WithError(err).WithField("path", path).Warn("Could not write focus gRPC service config file")
} else { } else {
logrus.WithField("path", path).Info("Successfully saved gRPC Focus service config file") serv.log.WithField("path", path).Info("Successfully saved gRPC Focus service config file")
} }
go func() { go func() {
@ -90,12 +92,14 @@ func NewService(locator service.Locator, version *semver.Version, panicHandler a
// Raise implements the gRPC FocusService interface; it raises the application. // Raise implements the gRPC FocusService interface; it raises the application.
func (service *Service) Raise(context.Context, *emptypb.Empty) (*emptypb.Empty, error) { func (service *Service) Raise(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
service.log.Debug("Raise")
service.raiseCh <- struct{}{} service.raiseCh <- struct{}{}
return &emptypb.Empty{}, nil return &emptypb.Empty{}, nil
} }
// Version implements the gRPC FocusService interface; it returns the version of the service. // Version implements the gRPC FocusService interface; it returns the version of the service.
func (service *Service) Version(context.Context, *emptypb.Empty) (*proto.VersionResponse, error) { func (service *Service) Version(context.Context, *emptypb.Empty) (*proto.VersionResponse, error) {
service.log.Debug("Version")
return &proto.VersionResponse{ return &proto.VersionResponse{
Version: service.version.Original(), Version: service.version.Original(),
}, nil }, nil

View File

@ -196,7 +196,7 @@ bool isBridgeRunning() {
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
void focusOtherInstance() { void focusOtherInstance() {
try { try {
FocusGRPCClient client; FocusGRPCClient client(app().log());
GRPCConfig sc; GRPCConfig sc;
QString const path = FocusGRPCClient::grpcFocusServerConfigPath(bridgepp::userConfigDir()); QString const path = FocusGRPCClient::grpcFocusServerConfigPath(bridgepp::userConfigDir());
QFile file(path); QFile file(path);
@ -209,7 +209,6 @@ void focusOtherInstance() {
throw Exception("Server did not provide gRPC Focus service configuration."); throw Exception("Server did not provide gRPC Focus service configuration.");
} }
QString error; QString error;
if (!client.connectToServer(5000, sc.port, &error)) { if (!client.connectToServer(5000, sc.port, &error)) {
throw Exception("Could not connect to bridge focus service for a raise call.", error); throw Exception("Could not connect to bridge focus service for a raise call.", error);
@ -288,6 +287,7 @@ int main(int argc, char *argv[]) {
CommandLineOptions const cliOptions = parseCommandLine(argc, argv); CommandLineOptions const cliOptions = parseCommandLine(argc, argv);
Log &log = initLog(); Log &log = initLog();
log.setLevel(cliOptions.logLevel);
QLockFile lock(bridgepp::userCacheDir() + "/" + bridgeGUILock); QLockFile lock(bridgepp::userCacheDir() + "/" + bridgeGUILock);
if (!checkSingleInstance(lock)) { if (!checkSingleInstance(lock)) {
@ -303,7 +303,6 @@ int main(int argc, char *argv[]) {
// In attached mode, we do not intercept stderr and stdout of bridge, as we did not launch it ourselves, so we output the log to the console. // In attached mode, we do not intercept stderr and stdout of bridge, as we did not launch it ourselves, so we output the log to the console.
// When not in attached mode, log entries are forwarded to bridge, which output it on stdout/stderr. bridge-gui's process monitor intercept // When not in attached mode, log entries are forwarded to bridge, which output it on stdout/stderr. bridge-gui's process monitor intercept
// these outputs and output them on the command-line. // these outputs and output them on the command-line.
log.setLevel(cliOptions.logLevel);
log.info(QString("New Sentry reporter - id: %1.").arg(getProtectedHostname())); log.info(QString("New Sentry reporter - id: %1.").arg(getProtectedHostname()));
QString const &sessionID = app().sessionID(); QString const &sessionID = app().sessionID();

View File

@ -46,6 +46,15 @@ QString grpcFocusServerConfigFilename() {
} }
//****************************************************************************************************************************************************
/// \param[in] log The log.
//****************************************************************************************************************************************************
FocusGRPCClient::FocusGRPCClient(Log& log)
:log_(log) {
}
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
/// \return The absolute path of the focus service config path. /// \return The absolute path of the focus service config path.
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
@ -91,6 +100,7 @@ bool FocusGRPCClient::connectToServer(qint64 timeoutMs, quint16 port, QString *o
throw Exception("Connexion check with focus service failed."); throw Exception("Connexion check with focus service failed.");
} }
log_.debug(QString("Successfully connected to focus gRPC service."));
return true; return true;
} }
catch (Exception const &e) { catch (Exception const &e) {
@ -106,6 +116,7 @@ bool FocusGRPCClient::connectToServer(qint64 timeoutMs, quint16 port, QString *o
/// \return The status for the call. /// \return The status for the call.
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
grpc::Status FocusGRPCClient::raise() { grpc::Status FocusGRPCClient::raise() {
log_.debug("FocusGRPCService::raise()");
ClientContext ctx; ClientContext ctx;
return stub_->Raise(&ctx, empty, &empty); return stub_->Raise(&ctx, empty, &empty);
} }
@ -116,6 +127,7 @@ grpc::Status FocusGRPCClient::raise() {
/// \return The status for the call. /// \return The status for the call.
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
grpc::Status FocusGRPCClient::version(QString &outVersion) { grpc::Status FocusGRPCClient::version(QString &outVersion) {
log_.debug("FocusGRPCService::version()");
ClientContext ctx; ClientContext ctx;
VersionResponse response; VersionResponse response;
Status status = stub_->Version(&ctx, empty, &response); Status status = stub_->Version(&ctx, empty, &response);

View File

@ -22,6 +22,7 @@
#include "grpc++/grpc++.h" #include "grpc++/grpc++.h"
#include "focus.grpc.pb.h" #include "focus.grpc.pb.h"
#include "../Log/Log.h"
namespace bridgepp { namespace bridgepp {
@ -36,7 +37,7 @@ public: // static member functions
static QString grpcFocusServerConfigPath(QString const &configDir); ///< Return the path of the gRPC Focus server config file. static QString grpcFocusServerConfigPath(QString const &configDir); ///< Return the path of the gRPC Focus server config file.
public: // member functions. public: // member functions.
FocusGRPCClient() = default; ///< Default constructor. FocusGRPCClient(Log& log); ///< Default constructor.
FocusGRPCClient(FocusGRPCClient const &) = delete; ///< Disabled copy-constructor. FocusGRPCClient(FocusGRPCClient const &) = delete; ///< Disabled copy-constructor.
FocusGRPCClient(FocusGRPCClient &&) = delete; ///< Disabled assignment copy-constructor. FocusGRPCClient(FocusGRPCClient &&) = delete; ///< Disabled assignment copy-constructor.
~FocusGRPCClient() = default; ///< Destructor. ~FocusGRPCClient() = default; ///< Destructor.
@ -48,6 +49,7 @@ public: // member functions.
grpc::Status version(QString &outVersion); ///< Performs the 'version' call. grpc::Status version(QString &outVersion); ///< Performs the 'version' call.
private: private:
Log &log_; ///< The log to use for logging calls
std::shared_ptr<grpc::Channel> channel_ { nullptr }; ///< The gRPC channel. std::shared_ptr<grpc::Channel> channel_ { nullptr }; ///< The gRPC channel.
std::shared_ptr<focus::Focus::Stub> stub_ { nullptr }; ///< The gRPC stub (a.k.a. client). std::shared_ptr<focus::Focus::Stub> stub_ { nullptr }; ///< The gRPC stub (a.k.a. client).
}; };