Tidy logging
This commit is contained in:
@@ -113,38 +113,6 @@ log_println(const char * const msg, ...)
|
||||
va_end(arg);
|
||||
}
|
||||
|
||||
void
|
||||
log_print(const char * const msg, ...)
|
||||
{
|
||||
va_list arg;
|
||||
va_start(arg, msg);
|
||||
GString *fmt_msg = g_string_new(NULL);
|
||||
g_string_vprintf(fmt_msg, msg, arg);
|
||||
GTimeZone *tz = g_time_zone_new_local();
|
||||
GDateTime *dt = g_date_time_new_now(tz);
|
||||
gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S");
|
||||
fprintf(logp, "%s: %s", date_fmt, fmt_msg->str);
|
||||
g_date_time_unref(dt);
|
||||
g_time_zone_unref(tz);
|
||||
fflush(logp);
|
||||
g_free(date_fmt);
|
||||
g_string_free(fmt_msg, TRUE);
|
||||
va_end(arg);
|
||||
}
|
||||
|
||||
void
|
||||
log_print_chars(const char * const msg, ...)
|
||||
{
|
||||
va_list arg;
|
||||
va_start(arg, msg);
|
||||
GString *fmt_msg = g_string_new(NULL);
|
||||
g_string_vprintf(fmt_msg, msg, arg);
|
||||
fprintf(logp, "%s", fmt_msg->str);
|
||||
fflush(logp);
|
||||
g_string_free(fmt_msg, TRUE);
|
||||
va_end(arg);
|
||||
}
|
||||
|
||||
void
|
||||
log_close(void)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,5 @@
|
||||
void log_init(void);
|
||||
void log_close(void);
|
||||
void log_println(const char * const msg, ...);
|
||||
void log_print(const char * const msg, ...);
|
||||
void log_print_chars(const char * const msg, ...);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,12 +5,14 @@
|
||||
|
||||
#include "server/parser.h"
|
||||
#include "server/stanza.h"
|
||||
#include "server/log.h"
|
||||
|
||||
static int depth = 0;
|
||||
static int do_reset = 0;
|
||||
|
||||
static XML_Parser parser;
|
||||
static XMPPStanza *curr_stanza;
|
||||
static GString *curr_string = NULL;
|
||||
|
||||
static stream_start_func stream_start_cb = NULL;
|
||||
static auth_func auth_cb = NULL;
|
||||
@@ -20,6 +22,7 @@ static void
|
||||
start_element(void *data, const char *element, const char **attributes)
|
||||
{
|
||||
if (g_strcmp0(element, "stream:stream") == 0) {
|
||||
log_println("RECV: %s", curr_string->str);
|
||||
stream_start_cb();
|
||||
do_reset = 1;
|
||||
return;
|
||||
@@ -47,6 +50,7 @@ end_element(void *data, const char *element)
|
||||
stanza_add_child(curr_stanza->parent, curr_stanza);
|
||||
curr_stanza = curr_stanza->parent;
|
||||
} else {
|
||||
log_println("RECV: %s", curr_string->str);
|
||||
stanza_add(curr_stanza);
|
||||
if (stanza_get_child_by_ns(curr_stanza, "jabber:iq:auth")) {
|
||||
auth_cb(curr_stanza);
|
||||
@@ -74,6 +78,11 @@ handle_data(void *data, const char *content, int length)
|
||||
void
|
||||
parser_init(stream_start_func startcb, auth_func authcb, id_func idcb)
|
||||
{
|
||||
if (curr_string) {
|
||||
g_string_free(curr_string, TRUE);
|
||||
}
|
||||
curr_string = g_string_new("");
|
||||
|
||||
stream_start_cb = startcb;
|
||||
auth_cb = authcb;
|
||||
id_cb = idcb;
|
||||
@@ -86,6 +95,7 @@ parser_init(stream_start_func startcb, auth_func authcb, id_func idcb)
|
||||
int
|
||||
parser_feed(char *chunk, int len)
|
||||
{
|
||||
g_string_append_len(curr_string, chunk, len);
|
||||
int res = XML_Parse(parser, chunk, len, 0);
|
||||
parser_reset();
|
||||
return res;
|
||||
|
||||
@@ -31,10 +31,10 @@ static int listen_socket;
|
||||
static pthread_t server_thread;
|
||||
|
||||
void
|
||||
write_stream(const char * const stanza)
|
||||
write_stream(const char * const stream)
|
||||
{
|
||||
int to_send = strlen(stanza);
|
||||
char *marker = (char*)stanza;
|
||||
int to_send = strlen(stream);
|
||||
char *marker = (char*)stream;
|
||||
|
||||
while (to_send > 0) {
|
||||
int sent = write(client->sock, marker, to_send);
|
||||
@@ -48,7 +48,6 @@ write_stream(const char * const stanza)
|
||||
|
||||
// real error
|
||||
} else {
|
||||
log_println("");
|
||||
log_println("Error sending on connection: %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
@@ -58,7 +57,7 @@ write_stream(const char * const stanza)
|
||||
marker += sent;
|
||||
}
|
||||
|
||||
log_println("SENT: %s", stanza);
|
||||
log_println("SENT: %s", stream);
|
||||
}
|
||||
|
||||
int
|
||||
@@ -74,7 +73,6 @@ read_stream(void)
|
||||
|
||||
// client disconnect
|
||||
if (read_size == 0) {
|
||||
log_println("");
|
||||
log_println("%s:%d - Client disconnected.", client->ip, client->port);
|
||||
g_string_free(stream, TRUE);
|
||||
return -1;
|
||||
@@ -89,7 +87,6 @@ read_stream(void)
|
||||
|
||||
// real error
|
||||
} else {
|
||||
log_println("");
|
||||
log_println("Error receiving on connection: %s", strerror(errno));
|
||||
g_string_free(stream, TRUE);
|
||||
return -1;
|
||||
@@ -97,11 +94,10 @@ read_stream(void)
|
||||
}
|
||||
|
||||
// success, feed parser with byte
|
||||
log_print_chars("%c", buf[0]);
|
||||
parser_feed(buf, 1);
|
||||
g_string_append_len(stream, buf, read_size);
|
||||
if (g_str_has_suffix(stream->str, STREAM_END)) {
|
||||
log_print_chars("\n");
|
||||
log_println("RECV: </stream:stream>");
|
||||
log_println("--> Stream end callback fired");
|
||||
write_stream(STREAM_END);
|
||||
_shutdown();
|
||||
@@ -116,20 +112,16 @@ read_stream(void)
|
||||
void
|
||||
stream_start_callback(void)
|
||||
{
|
||||
log_print_chars("\n");
|
||||
log_println("--> Stream start callback fired");
|
||||
|
||||
write_stream(XML_START);
|
||||
write_stream(STREAM_RESP);
|
||||
write_stream(FEATURES);
|
||||
|
||||
log_print("RECV: ");
|
||||
}
|
||||
|
||||
void
|
||||
auth_callback(XMPPStanza *stanza)
|
||||
{
|
||||
log_print_chars("\n");
|
||||
log_println("--> Auth callback fired");
|
||||
|
||||
XMPPStanza *query = stanza_get_child_by_ns(stanza, "jabber:iq:auth");
|
||||
@@ -149,7 +141,6 @@ auth_callback(XMPPStanza *stanza)
|
||||
}
|
||||
|
||||
write_stream(AUTH_RESP);
|
||||
log_print("RECV: ");
|
||||
}
|
||||
|
||||
void
|
||||
@@ -157,10 +148,8 @@ id_callback(const char *id)
|
||||
{
|
||||
char *stream = prime_get_for(id);
|
||||
if (stream) {
|
||||
log_print_chars("\n");
|
||||
log_println("--> ID callback fired for '%s'", id);
|
||||
write_stream(stream);
|
||||
log_print("RECV: ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,7 +206,6 @@ _start_server_cb(void* userdata)
|
||||
client = xmppclient_new(client_addr, client_socket);
|
||||
parser_init(stream_start_callback, auth_callback, id_callback);
|
||||
|
||||
log_print("RECV: ");
|
||||
read_stream();
|
||||
|
||||
return NULL;
|
||||
|
||||
Reference in New Issue
Block a user