From 5eaf687d98de9683ea4e479845868c56c14f015d Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 5 Feb 2012 15:10:10 +0000 Subject: [PATCH] Split log into module --- Makefile | 13 +++++---- curses_example.c | 76 ------------------------------------------------ log.c | 23 +++++++++++++++ log.h | 9 ++++++ profanity.c | 37 +++++++---------------- 5 files changed, 50 insertions(+), 108 deletions(-) delete mode 100644 curses_example.c create mode 100644 log.c create mode 100644 log.h diff --git a/Makefile b/Makefile index 4ffd8c11..be7b41d3 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,15 @@ CC = gcc -CFLAGS = -O3 -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-variable +WARNS = -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-variable LIBS = -lxml2 -lssl -lresolv -lncurses -lstrophe +CFLAGS = -O3 $(WARNS) $(LIBS) +OBJS = log.o profanity.o -profanity: clean - $(CC) profanity.c $(LIBS) -o profanity - $(CC) curses_example.c -lncurses -o curses_example +profanity: $(OBJS) + $(CC) -o profanity $(OBJS) $(LIBS) + +log.o: log.h +profanity.o: log.h .PHONY: clean clean: rm -f profanity - rm -f curses_example diff --git a/curses_example.c b/curses_example.c deleted file mode 100644 index fa4149ca..00000000 --- a/curses_example.c +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include - -void init() -{ - initscr(); - raw(); - keypad(stdscr, TRUE); - start_color(); - - init_color(COLOR_WHITE, 1000, 1000, 1000); - init_pair(1, COLOR_WHITE, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - - init_color(COLOR_BLUE, 0, 0, 250); - init_pair(3, COLOR_WHITE, COLOR_BLUE); - - attron(A_BOLD); - attron(COLOR_PAIR(1)); -} - -void print_title() -{ - int rows, cols; - char *title = "PROFANITY"; - - getmaxyx(stdscr, rows, cols); - - attron(COLOR_PAIR(3)); - mvprintw(1, (cols - strlen(title))/2, title); - attroff(COLOR_PAIR(3)); -} - -void close() -{ - int rows, cols; - char *exit_msg = "< HIT ANY KEY TO EXIT >"; - - getmaxyx(stdscr, rows, cols); - - attron(A_BLINK); - curs_set(0); - mvprintw(rows-10, (cols - strlen(exit_msg))/2, exit_msg); - - refresh(); - getch(); - endwin(); -} - -int main() -{ - int ypos = 2; - int xpos = 2; - int ch; - char name[20]; - - init(); - - print_title(); - ypos += 2; - mvprintw(ypos, xpos, "Enter your name: "); - echo(); - getstr(name); - noecho(); - - ypos += 2; - mvprintw(ypos, xpos, "Shit, "); - attron(COLOR_PAIR(2)); - printw("%s", name); - attroff(COLOR_PAIR(2)); - - printw("\n"); - close(); - - return 0; -} diff --git a/log.c b/log.c new file mode 100644 index 00000000..4022a20f --- /dev/null +++ b/log.c @@ -0,0 +1,23 @@ +#include +#include + +#include "log.h" + +extern FILE *logp; + +void xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, + const char * const area, const char * const msg); + +static const xmpp_log_t file_log = { &xmpp_file_logger, XMPP_LEVEL_DEBUG }; + +xmpp_log_t *xmpp_get_file_logger() +{ + return (xmpp_log_t*) &file_log; +} + +void xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, + const char * const area, const char * const msg) +{ + fprintf(logp, "%s DEBUG %s\n", area, msg); +} + diff --git a/log.h b/log.h new file mode 100644 index 00000000..1608167b --- /dev/null +++ b/log.h @@ -0,0 +1,9 @@ +#ifndef LOG_H +#define LOG_H + +// log +FILE *logp; + +xmpp_log_t *xmpp_get_file_logger(); + +#endif diff --git a/profanity.c b/profanity.c index eec51ca4..cbc6aefc 100644 --- a/profanity.c +++ b/profanity.c @@ -4,7 +4,12 @@ #include #include -static FILE *logp; +#include "log.h" + +// refernce to log +extern FILE *logp; + +// chat windows static WINDOW *incoming_border; static WINDOW *outgoing_border; static WINDOW *incoming; @@ -12,27 +17,21 @@ static WINDOW *outgoing; static int incoming_ypos = 0; static int inc_scroll_max = 0; -xmpp_log_t *xmpp_get_file_logger(); - -void xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, - const char * const area, const char * const msg); - -static const xmpp_log_t file_log = { &xmpp_file_logger, XMPP_LEVEL_DEBUG }; - +// message handlers int in_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); int out_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +// connection handler 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); +// curses functions void init(void); - void print_title(void); - void close(void); WINDOW *create_win(int, int, int, int, int); @@ -42,7 +41,6 @@ int main(void) { int ypos = 2; int xpos = 2; - int ch; char user[50]; char passwd[50]; @@ -103,11 +101,7 @@ int main(void) int in_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { - char *append_reply = ", recieved"; - - xmpp_stanza_t *reply, *body, *text; - char *intext, *replytext; - xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata; + char *intext; if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1; @@ -166,17 +160,6 @@ int out_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, return 1; } -xmpp_log_t *xmpp_get_file_logger() -{ - return (xmpp_log_t*) &file_log; -} - -void xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, - const char * const area, const char * const msg) -{ - fprintf(logp, "%s %s %s\n", area, XMPP_LEVEL_DEBUG, 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)