Max has uploaded this change for review. ( https://gerrit.osmocom.org/12318


Change subject: ctrl: log host/port on errors
......................................................................

ctrl: log host/port on errors

In case of multiple ctrl-client entries in .cfg file it's impossible to
see which one is causing particular ctrl error. Fix this by introducing
macro wrapper for stderr logging which always show host:port relevant to
the error.

Change-Id: I788d51359965a66c54075a3971aa7824c3bfb0bf
Related: SYS#2655
---
M src/simple_ctrl.c
1 file changed, 18 insertions(+), 16 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-sysmon refs/changes/18/12318/1

diff --git a/src/simple_ctrl.c b/src/simple_ctrl.c
index 24d92b9..433f3bf 100644
--- a/src/simple_ctrl.c
+++ b/src/simple_ctrl.c
@@ -37,6 +37,8 @@

 #include "simple_ctrl.h"

+#define CTRL_ERR(c, fmt, args...) fprintf(stderr, "CTRL %s:%u error: " fmt 
"\n", c->remote_host, c->remote_port, ##args)
+
 /***********************************************************************
  * blocking I/O with timeout helpers
  ***********************************************************************/
@@ -110,7 +112,7 @@
        fd = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, 
cfg->remote_host, cfg->remote_port,
                            OSMO_SOCK_F_CONNECT | OSMO_SOCK_F_NONBLOCK);
        if (fd < 0) {
-               fprintf(stderr, "CTRL: error connecting socket: %s\n", 
strerror(errno));
+               CTRL_ERR(cfg, "connecting socket: %s", strerror(errno));
                return NULL;
        }

@@ -119,17 +121,17 @@
        FD_SET(fd, &writeset);
        rc = select(fd+1, NULL, &writeset, NULL, timeval_from_msec(tout_msec));
        if (rc == 0) {
-               fprintf(stderr, "CTRL: timeout during connect\n");
+               CTRL_ERR(cfg, "timeout during connect");
                goto out_close;
        }
        if (rc < 0) {
-               fprintf(stderr, "CTRL: error connecting socket: %s\n", 
strerror(errno));
+               CTRL_ERR(cfg, "error connecting socket: %s", strerror(errno));
                goto out_close;
        }

        /* set FD blocking again */
        if (ioctl(fd, FIONBIO, (unsigned char *)&off) < 0) {
-               fprintf(stderr, "CTRL: cannot set socket blocking: %s\n", 
strerror(errno));
+               CTRL_ERR(cfg, "cannot set socket blocking: %s", 
strerror(errno));
                goto out_close;
        }

@@ -156,7 +158,7 @@
        talloc_free(sch);
 }

-static struct msgb *simple_ipa_receive(struct simple_ctrl_handle *sch)
+static struct msgb *simple_ipa_receive(const struct ctrl_cfg *cfg, struct 
simple_ctrl_handle *sch)
 {
        struct ipaccess_head hh;
        struct msgb *resp;
@@ -164,10 +166,10 @@

        rc = read_timeout(sch->fd, (uint8_t *) &hh, sizeof(hh), sch->tout_msec);
        if (rc < 0) {
-               fprintf(stderr, "CTRL: Error during read: %d\n", rc);
+               CTRL_ERR(cfg, "read(): %d", rc);
                return NULL;
        } else if (rc < sizeof(hh)) {
-               fprintf(stderr, "CTRL: ERROR: short read (header)\n");
+               CTRL_ERR(cfg, "short read (header)");
                return NULL;
        }
        len = ntohs(hh.len);
@@ -181,7 +183,7 @@
        resp->l2h = resp->tail;
        rc = read(sch->fd, resp->l2h, len);
        if (rc < len) {
-               fprintf(stderr, "CTRL: ERROR: short read (payload)\n");
+               CTRL_ERR(cfg, "short read (payload)");
                msgb_free(resp);
                return NULL;
        }
@@ -199,7 +201,7 @@

        /* loop until we've received a CTRL message */
        while (true) {
-               resp = simple_ipa_receive(sch);
+               resp = simple_ipa_receive(cfg, sch);
                if (!resp)
                        return NULL;

@@ -213,13 +215,13 @@
                        *tmp = '\0';
                        return resp;
                } else {
-                       fprintf(stderr, "unknown IPA message %s\n", 
msgb_hexdump(resp));
+                       CTRL_ERR(cfg, "unknown IPA message %s", 
msgb_hexdump(resp));
                        msgb_free(resp);
                }
        }
 }

-static int simple_ctrl_send(struct simple_ctrl_handle *sch, struct msgb *msg)
+static int simple_ctrl_send(const struct ctrl_cfg *cfg, struct 
simple_ctrl_handle *sch, struct msgb *msg)
 {
        int rc;

@@ -228,10 +230,10 @@

        rc = write_timeout(sch->fd, msg->data, msg->len, sch->tout_msec);
        if (rc < 0) {
-               fprintf(stderr, "CTRL: Error during write: %d\n", rc);
+               CTRL_ERR(cfg, "write(): %d", rc);
                return rc;
        } else if (rc < msg->len) {
-               fprintf(stderr, "CTRL: ERROR: short write\n");
+               CTRL_ERR(cfg, "short write");
                msgb_free(msg);
                return -1;
        } else {
@@ -244,7 +246,7 @@
 {
        int rc;

-       rc = simple_ctrl_send(sch, msg);
+       rc = simple_ctrl_send(cfg, sch, msg);
        if (rc < 0)
                return NULL;

@@ -282,7 +284,7 @@
                free(rx_var);
                free(rx_val);
        } else {
-               fprintf(stderr, "CTRL: ERROR: GET(%s) results in '%s'\n", var, 
(char *)msgb_l2(resp));
+               CTRL_ERR(cfg, "GET(%s) results in '%s'", var, (char 
*)msgb_l2(resp));
        }

        msgb_free(resp);
@@ -320,7 +322,7 @@
                        free(rx_var);
                }
        } else {
-               fprintf(stderr, "CTRL: ERROR: SET(%s=%s) results in '%s'\n", 
var, val, (char *) msgb_l2(resp));
+               CTRL_ERR(cfg, "SET(%s=%s) results in '%s'", var, val, (char *) 
msgb_l2(resp));
        }

        msgb_free(resp);

--
To view, visit https://gerrit.osmocom.org/12318
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-sysmon
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I788d51359965a66c54075a3971aa7824c3bfb0bf
Gerrit-Change-Number: 12318
Gerrit-PatchSet: 1
Gerrit-Owner: Max <[email protected]>

Reply via email to