Permalink
Browse files

Use glib logging instead of stdio logging.

  • Loading branch information...
1 parent 497ceb4 commit 7ff6f0470b4bd419d29ffd43a59dafd36e12cb5c @rosslagerwall committed Jun 24, 2012
Showing with 19 additions and 22 deletions.
  1. +2 −2 src/cxmap.c
  2. +10 −6 src/main.c
  3. +2 −5 src/pclient.c
  4. +5 −9 src/pserv.c
View
@@ -40,9 +40,9 @@ cxmap_register(struct ProxyServer *serv)
struct event_base *base = bufferevent_get_base(serv->bev);
client = pclient_new(base, serv->location, serv->path);
g_hash_table_insert(tbl, client->location, client);
- printf("Creating a new ProxyClient\n");
+ g_debug("Creating a new ProxyClient object for %s", serv->location);
} else {
- printf("Reusing old one\n");
+ g_debug("Reusing an existing ProxyClient for %s", serv->location);
}
pclient_register(client, serv);
}
View
@@ -19,13 +19,13 @@
#include "pserv.h"
#include "dns.h"
+#include <glib.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/listener.h>
#include <string.h>
#include <stdlib.h>
-#include <stdio.h>
#include <unistd.h>
@@ -42,19 +42,21 @@ listener_accept_cb(struct evconnlistener *listener, evutil_socket_t sock,
bufferevent_setcb(bev, pserv_read_cb, pserv_write_cb, pserv_event_cb,
serv);
bufferevent_enable(bev, EV_READ|EV_WRITE);
+
+ g_debug("Received a connection");
}
/* Start the server listening on 8080 and start the first client. */
-static void
+static int
start_loop(void)
{
struct event_base *base;
struct evconnlistener *listener;
base = event_base_new();
if (base == NULL) {
- puts("Could not open event base!");
- exit(1);
+ g_critical("Could not open event base!");
+ return 1;
}
dns_init(base);
@@ -73,11 +75,13 @@ start_loop(void)
exit(1);
}
+ g_message("Starting up main loop");
event_base_dispatch(base);
+
+ return 0;
}
int main(int argc, char **argv) {
cxmap_init();
- start_loop();
- return 0;
+ return start_loop();
}
View
@@ -16,7 +16,6 @@
*/
#include <stdlib.h>
-#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <sys/stat.h>
@@ -42,10 +41,8 @@ void mkdir_p(char *path)
int i;
for (i = 0; i < n_components; i++) {
- printf("lol=%s\n", components[i]);
strcat(pathbuf, components[i]);
strcat(pathbuf, "/");
- printf("otto=%s\n", pathbuf);
mkdir(pathbuf, 0777);
}
@@ -102,6 +99,8 @@ pclient_free(struct ProxyClient *client)
bufferevent_free(client->bev);
fclose(client->sink);
free(client);
+
+ g_debug("ProxyClient disconnect");
}
void
@@ -159,13 +158,11 @@ pclient_event_cb(struct bufferevent *bev, short events, void *ctx)
} else if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
struct ProxyClient *client = ctx;
pclient_free(client);
- printf("freeing proxy client\n");
}
}
void
pclient_register(struct ProxyClient *client, struct ProxyServer *serv)
{
client->observers = g_list_prepend(client->observers, serv);
- printf("New server registered\n");
}
View
@@ -64,6 +64,8 @@ pserv_free(struct ProxyServer *serv)
close(serv->proxy_fd);
bufferevent_free(serv->bev);
free(serv);
+
+ g_debug("ProxyServer disconnect");
}
/* Adds the location to the base path and makes sure that the path is safe
@@ -117,18 +119,13 @@ void
pserv_write_cb(struct bufferevent *bev, void *ctx)
{
struct ProxyServer *serv = ctx;
- //printf("write ready\n");
if (serv->state == PS_SEND) {
struct evbuffer *output = bufferevent_get_output(serv->bev);
- //printf("lenrem=%lu\n", evbuffer_get_length(output));
if ((serv->n_avail - serv->n_written) > 0) {
int n_towrite = MIN(32767, serv->n_avail - serv->n_written);
- //printf("tow %d rem %d\n", n_towrite, serv->n_avail);
int ret = evbuffer_read(output, serv->proxy_fd, n_towrite);
- //printf("actually read %d\n", ret);
serv->n_written += ret;
} else if (serv->n_written == serv->n_tot) {
- printf("close!\n");
pserv_free(serv);
}
}
@@ -141,7 +138,6 @@ pserv_event_cb(struct bufferevent *bev, short events, void *ctx)
perror("Error from bufferevent");
exit(1);
} else if (events & BEV_EVENT_EOF) {
- printf("serv eof");
struct ProxyServer *serv = ctx;
pserv_free(serv);
}
@@ -171,8 +167,10 @@ pserv_start_transfer(struct ProxyServer *serv)
int ret = evbuffer_read(output, serv->proxy_fd, n_towrite);
serv->n_avail = serv->n_tot;
serv->n_written = ret;
- printf("starting transfer\n");
+
+ g_message("Sending cached content for %s", serv->location);
} else {
+ g_message("Proxying content for %s", serv->location);
serv->state = PS_SENDH;
cxmap_register(serv);
}
@@ -185,7 +183,6 @@ void
pserv_data_updated_cb(struct ProxyServer *serv, int n, int length)
{
if (serv->state == PS_SENDH) {
- printf("length cb\n");
struct evbuffer *output = bufferevent_get_output(serv->bev);
serv->state = PS_SEND;
char *path = g_strconcat(serv->path, ".tmp", NULL);
@@ -200,7 +197,6 @@ pserv_data_updated_cb(struct ProxyServer *serv, int n, int length)
}
if (serv->state == PS_SEND) {
- //printf("got %d bytes\n", n);
struct evbuffer *output = bufferevent_get_output(serv->bev);
serv->n_avail = n;
if (evbuffer_get_length(output) == 0) {

0 comments on commit 7ff6f04

Please sign in to comment.