aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Ghigonis <laurent@p1sec.com>2012-11-30 10:44:20 +0100
committerLaurent Ghigonis <laurent@p1sec.com>2012-11-30 10:44:20 +0100
commit79b70a291f339a66f6d3c9f9a8977b6ce2011a7a (patch)
treee1e1bfda8d1a624075141ba77685325cf2201a46
parentTODO-- (diff)
downloadglouglou-79b70a291f339a66f6d3c9f9a8977b6ce2011a7a.tar.xz
glouglou-79b70a291f339a66f6d3c9f9a8977b6ce2011a7a.zip
add error() verbose() and debug() to log stuff from libglouglou,
and gg_verbosity_get() and gg_verbosity_set() to change libglouglou verbosity level
-rw-r--r--libglouglou/libglouglou.c67
-rw-r--r--libglouglou/libglouglou.h4
2 files changed, 50 insertions, 21 deletions
diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c
index da18ae6..a692045 100644
--- a/libglouglou/libglouglou.c
+++ b/libglouglou/libglouglou.c
@@ -14,6 +14,16 @@
#include "libglouglou.h"
+#define error(fmt, ...) \
+ if (_verbosity >= 0) \
+ printf("libgg: %s: ERROR: " fmt "\n", __func__, ##__VA_ARGS__)
+#define verbose(fmt, ...) \
+ if (_verbosity >= 1) \
+ printf("libgg: %s: " fmt "\n", __func__, ##__VA_ARGS__)
+#define debug(fmt, ...) \
+ if (_verbosity >= 2) \
+ printf("libgg: %s: XXX: " fmt "\n", __func__, ##__VA_ARGS__)
+
int server_send(struct gg_server *, struct gg_user *,
void *, int);
void cb_srv_receive(evutil_socket_t, short, void *);
@@ -27,6 +37,8 @@ void cb_cli_timer(evutil_socket_t, short, void *);
struct gg_packet *pkt_decode(char **, int *);
int pkt_getsize(struct gg_packet *);
+int _verbosity = 0;
+
/*
* Server
*/
@@ -81,8 +93,7 @@ gg_server_start(struct event_base *ev_base, char *ip, int port,
return srv;
err:
- printf("libglouglou: gg_server_start: Error ! %s",
- strerror(errno));
+ error("%s", strerror(errno));
gg_server_stop(srv);
return NULL;
}
@@ -142,7 +153,7 @@ void cb_srv_receive(evutil_socket_t fd, short what, void *arg)
len = recvfrom(fd, buf, sizeof(buf), 0,
(struct sockaddr *)&remote, &remote_len);
if (len < 0) {
- printf("libglouglou: cb_srv_receive: recvfrom failed\n");
+ error("recvfrom failed");
return;
}
@@ -153,9 +164,8 @@ void cb_srv_receive(evutil_socket_t fd, short what, void *arg)
srv->handle_conn(srv, usr);
user_send(usr, "", 0);
} else {
- printf("srv: Incoming data from existing user !\n");
+ debug("Incoming data from existing user !");
if (len == 0) {
- printf("libglouglou: cb_srv_receive: recvfrom = 0\n");
user_del(srv, usr);
return;
}
@@ -166,7 +176,7 @@ void cb_srv_receive(evutil_socket_t fd, short what, void *arg)
srv->handle_packet(srv, usr, pkt);
if (buf_len > 0) {
/* XXX store incomplete packet for next recv */
- printf("srv: incomplete packet, dropped %d bytes !\n", buf_len);
+ error("incomplete packet, dropped %d bytes !", buf_len);
}
}
}
@@ -183,7 +193,7 @@ user_add(struct gg_server *srv, struct sockaddr_in *remote)
usr->sock = srv->sock;
addrcpy(&usr->addr, remote);
LIST_INSERT_HEAD(&srv->user_list, usr, entry);
- printf("srv: Add user %d !\n", usr->id);
+ verbose("Add user %d !", usr->id);
return usr;
}
@@ -191,7 +201,7 @@ user_add(struct gg_server *srv, struct sockaddr_in *remote)
void
user_del(struct gg_server *srv, struct gg_user *usr)
{
- printf("srv: Del user %d !\n", usr->id);
+ verbose("Del user %d !", usr->id);
LIST_REMOVE(usr, entry);
free(usr);
}
@@ -216,7 +226,7 @@ user_send(struct gg_user *usr, void *data, int size)
{
if (sendto(usr->sock, data, size, 0,
(struct sockaddr *)&usr->addr, sizeof(struct sockaddr_in)) == -1) {
- printf("user_send failed: %s", strerror(errno));
+ error("failed: %s", strerror(errno));
return -1;
}
return 0;
@@ -280,8 +290,7 @@ gg_client_connect(struct event_base *ev_base, char *ip, int port,
return cli;
err:
- printf("libglouglou: gg_client_connect: Error ! %s",
- strerror(errno));
+ error("Error ! %s", strerror(errno));
gg_client_disconnect(cli);
return NULL;
}
@@ -318,7 +327,7 @@ client_send(struct gg_client *cli, void *data, int size)
{
if (sendto(cli->sock, data, size, 0,
(struct sockaddr *)&cli->addr, sizeof(struct sockaddr_in)) == -1) {
- printf("gg_client_send failed: %s", strerror(errno));
+ error("failed: %s", strerror(errno));
return -1;
}
return 0;
@@ -340,28 +349,28 @@ void cb_cli_receive(evutil_socket_t fd, short what, void *arg)
len = recvfrom(fd, buf, sizeof(buf), 0,
(struct sockaddr *)&remote, &remote_len);
if (addrcmp(&cli->addr, &remote)) {
- printf("libglouglou; cb_cli_receive: receiving from stranger !\n");
+ error("receiving from stranger !");
return;
}
if (len < 0) {
- printf("libglouglou: cb_cli_receive: recvfrom failed\n");
+ error("recvfrom failed");
return;
}
switch (cli->status) {
case GG_CLIENT_STATUS_CONNECTING:
- printf("cli: Connected !\n");
+ verbose("Connected !");
cli->status = GG_CLIENT_STATUS_CONNECTED;
if (cli->handle_conn)
cli->handle_conn(cli);
break;
case GG_CLIENT_STATUS_CONNECTED:
if (len == 0) {
- printf("libglouglou: cb_cli_receive: recvfrom = 0\n");
+ verbose("libglouglou: cb_cli_receive: recvfrom = 0");
cli->status = GG_CLIENT_STATUS_CONNECTING;
return;
}
- printf("cli: Incoming data !\n");
+ debug("Incoming data !");
if (cli->handle_packet) {
buf_p = buf;
buf_len = len;
@@ -369,7 +378,7 @@ void cb_cli_receive(evutil_socket_t fd, short what, void *arg)
cli->handle_packet(cli, pkt);
if (buf_len > 0) {
/* XXX store incomplete packet for next recv */
- printf("cli: incomplete packet, dropped %d bytes !\n", buf_len);
+ error("incomplete packet, dropped %d bytes !", buf_len);
}
}
break;
@@ -491,12 +500,12 @@ pkt_decode(char **buf, int *buf_len)
return &newpkt;
incomplete:
- printf("pkt_decode: incomplete packet\n");
+ error("incomplete packet");
*buf_len = len;
return NULL;
invalid:
- printf("pkt_decode: invalid packet\n");
+ error("invalid packet");
*buf = NULL;
*buf_len = 0;
return NULL;
@@ -538,6 +547,22 @@ pkt_getsize(struct gg_packet *pkt)
}
/*
+ * Verbosity
+ */
+
+int
+gg_verbosity_get(void)
+{
+ return _verbosity;
+}
+
+void
+gg_verbosity_set(int verb)
+{
+ _verbosity = verb;
+}
+
+/*
* Utils
*/
@@ -569,7 +594,7 @@ fd_nonblock(int fd)
int flags = fcntl(fd, F_GETFL, 0);
int rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if (rc == -1)
- printf("failed to set fd %i non-blocking", fd);
+ error("failed to set fd %i non-blocking", fd);
}
void
diff --git a/libglouglou/libglouglou.h b/libglouglou/libglouglou.h
index 1da4bd8..f0285a8 100644
--- a/libglouglou/libglouglou.h
+++ b/libglouglou/libglouglou.h
@@ -143,8 +143,12 @@ struct gg_client *gg_client_connect(struct event_base *, char *, int,
int gg_client_send(struct gg_client *, struct gg_packet *);
void gg_client_disconnect(struct gg_client *);
+int gg_verbosity_get(void);
+void gg_verbosity_set(int);
+
void *xmalloc(size_t);
void *xcalloc(size_t, size_t);
void fd_nonblock(int);
void addrcpy(struct sockaddr_in *, struct sockaddr_in *);
int addrcmp(struct sockaddr_in *, struct sockaddr_in *);
+