diff --git a/SConstruct b/SConstruct index 691c9ba..d1b0f2f 100644 --- a/SConstruct +++ b/SConstruct @@ -61,6 +61,7 @@ Headers = Split(""" Examples = Split(""" basic.c + basic_logging.c active.c roster.c """) diff --git a/examples/basic_logging.c b/examples/basic_logging.c new file mode 100644 index 0000000..acb7442 --- /dev/null +++ b/examples/basic_logging.c @@ -0,0 +1,82 @@ +/* basic.c +** libstrophe XMPP client library -- basic usage example +** +** Copyright (C) 2005 OGG, LCC. All rights reserved. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This software is distributed under license and may not be copied, +** modified or distributed except as expressly authorized under the +** terms of the license contained in the file LICENSE.txt in this +** distribution. +*/ + +#include + +#include + +void log_handler(void * const userdata, + const xmpp_log_level_t level, + const char * const area, + const char * const msg) +{ + fprintf(stderr, "%s %s %s\n", area, xmpp_log_level_name[level], msg); +} + +void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, + const int error, xmpp_stream_error_t * const stream_error, + void * const userdata) +{ + xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + + if (status == XMPP_CONN_CONNECT) { + fprintf(stderr, "DEBUG: connected\n"); + xmpp_disconnect(conn); + } + else { + fprintf(stderr, "DEBUG: disconnected\n"); + xmpp_stop(ctx); + } +} + +int main(int argc, char **argv) +{ + xmpp_ctx_t *ctx; + xmpp_conn_t *conn; + xmpp_log_t log; + + if (argc != 4) { + fprintf(stderr, "Usage: basic \n\n"); + return 1; + } + + /* init library */ + xmpp_initialize(); + + /* create a context */ + log.handler = log_handler; + log.userdata = NULL; + ctx = xmpp_ctx_new(NULL, &log); + + /* create a connection */ + conn = xmpp_conn_new(ctx); + + /* setup authentication information */ + xmpp_conn_set_jid(conn, argv[1]); + xmpp_conn_set_pass(conn, argv[2]); + + /* initiate connection */ + xmpp_connect_client(conn, argv[3], conn_handler, ctx); + + /* start the event loop */ + xmpp_run(ctx); + + /* release our connection and context */ + xmpp_conn_release(conn); + xmpp_ctx_free(ctx); + + xmpp_shutdown(); + + return 0; +} diff --git a/src/ctx.c b/src/ctx.c index 4c05cb4..e98da55 100644 --- a/src/ctx.c +++ b/src/ctx.c @@ -63,8 +63,6 @@ static xmpp_mem_t xmpp_default_mem = { realloc }; -static const char * const xmpp_log_level_name[4] = {"DEBUG", "INFO", "WARN", "ERROR"}; - void xmpp_default_logger(void * const userdata, const xmpp_log_level_t level, const char * const area, diff --git a/strophe.h b/strophe.h index 750308a..4f9944d 100644 --- a/strophe.h +++ b/strophe.h @@ -63,6 +63,8 @@ typedef enum { XMPP_LEVEL_ERROR } xmpp_log_level_t; +static const char * const xmpp_log_level_name[4] = {"DEBUG", "INFO", "WARN", "ERROR"}; + typedef enum { XMPP_UNKNOWN, XMPP_CLIENT,