add missing irc_cmd.h, and modify line_proto/irc_conn to use log
authorTero Marttila <terom@fixme.fi>
Sun, 01 Mar 2009 02:02:48 +0200
changeset 22 c339c020fd33
parent 21 0911d0b828d4
child 23 542c73d07d3c
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
src/irc_cmd.h
src/irc_conn.c
src/line_proto.c
src/log.c
src/log.h
src/nexus.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/irc_cmd.h	Sun Mar 01 02:02:48 2009 +0200
@@ -0,0 +1,10 @@
+#ifndef IRC_CMD_H
+#define IRC_CMD_H
+
+/*
+ * IRC command numerics
+ */
+#define IRC_RPL_WELCOME         "001"
+
+
+#endif /* IRC_CMD_H */
--- a/src/irc_conn.c	Sun Mar 01 01:48:14 2009 +0200
+++ b/src/irc_conn.c	Sun Mar 01 02:02:48 2009 +0200
@@ -1,6 +1,7 @@
 
 #include "irc_conn.h"
 #include "irc_cmd.h"
+#include "log.h"
 
 #include <stdlib.h>
 #include <string.h>
@@ -12,6 +13,8 @@
 {
     // update state
     conn->registered = true;
+
+    log_info("registered");
 }
 
 /*
@@ -49,7 +52,7 @@
     int err;
     
     // log
-    printf("<<< %s\n", line_buf);
+    log_debug("%s", line_buf);
 
     // parse
     if ((err = irc_line_parse(&line, line_buf))) {
@@ -104,7 +107,7 @@
         return err;
     
     // log
-    printf(">>> %s\n", line_buf);
+    log_debug("%s", line_buf);
 
     // add CRLF
     strcat(line_buf, "\r\n");
--- a/src/line_proto.c	Sun Mar 01 01:48:14 2009 +0200
+++ b/src/line_proto.c	Sun Mar 01 02:02:48 2009 +0200
@@ -1,12 +1,11 @@
 
 #include "line_proto.h"
+#include "log.h"
 
 #include <string.h>
 #include <stdlib.h>
 #include <assert.h>
 
-#include <err.h>
-
 /*
  * Our state
  */
@@ -54,8 +53,8 @@
     do {
         // attempt to read a line
         if (line_proto_recv(lp, &line))
-            // XXX: fail
-            errx(1, "line_proto_recv: %s", error_msg(&lp->err));
+            // XXX: un-fatalize
+            FATAL_ERROR(&lp->err, "line_proto_recv");
         
         // got a line?
         if (line)
@@ -65,7 +64,7 @@
 
     // reschedule
     if (line_proto_schedule_events(lp, EV_READ))
-        errx(1, "line_proto_schedule_events: %s", error_msg(&lp->err));
+        FATAL_ERROR(&lp->err, "line_proto_schedule_events");
 }
 
 /*
@@ -78,7 +77,7 @@
 
     // just flush
     if ((err = line_proto_flush(lp)) < 0)
-        errx(1, "line_proto_flush: %s", error_name(err));
+        FATAL_ERR(err, "line_proto_flush");
 }
 
 /*
--- a/src/log.c	Sun Mar 01 01:48:14 2009 +0200
+++ b/src/log.c	Sun Mar 01 02:02:48 2009 +0200
@@ -22,7 +22,7 @@
  */
 void _log_header (enum log_level level, const char *func)
 {
-    printf("[%5s] %s: ", log_level_name(level), func);
+    printf("[%5s] %20s : ", log_level_name(level), func);
 }
 
 void log_msg (enum log_level level, const char *func, const char *format, ...)
@@ -40,12 +40,12 @@
     printf("\n");
 }
 
-void _log_err (err_t err, const char *func, const char *format, ...)
+void _log_err (enum log_level level, err_t err, const char *func, const char *format, ...)
 {
     va_list vargs;
 
     // header
-    _log_header(LOG_ERROR, func);
+    _log_header(level, func);
     
     // formatted output
     va_start(vargs, format);
@@ -56,12 +56,12 @@
     printf(": %s\n", error_name(err));
 }
 
-void _log_err_info (struct error_info *err, const char *func, const char *format, ...)
+void _log_err_info (enum log_level level, struct error_info *err, const char *func, const char *format, ...)
 {
     va_list vargs;
 
     // header
-    _log_header(LOG_ERROR, func);
+    _log_header(level, func);
     
     // formatted output
     va_start(vargs, format);
--- a/src/log.h	Sun Mar 01 01:48:14 2009 +0200
+++ b/src/log.h	Sun Mar 01 02:02:48 2009 +0200
@@ -38,14 +38,14 @@
 /**
  * Log a message with LOG_ERROR, appending the formatted error code
  */
-void _log_err (err_t err, const char *func, const char *format, ...)
-    __attribute__ ((format (printf, 3, 4)));
+void _log_err (enum log_level level, err_t err, const char *func, const char *format, ...)
+    __attribute__ ((format (printf, 4, 5)));
 
-void _log_err_info (struct error_info *err, const char *func, const char *format, ...)
-    __attribute__ ((format (printf, 3, 4)));
+void _log_err_info (enum log_level level, struct error_info *err, const char *func, const char *format, ...)
+    __attribute__ ((format (printf, 4, 5)));
 
-#define log_err(err, ...) _log_err(err, __func__, __VA_ARGS__)
-#define log_err_info(err_info, ...) _log_err_info(err_info, __func__, __VA_ARGS__)
+#define log_err(err, ...) _log_err(LOG_ERROR, err, __func__, __VA_ARGS__)
+#define log_err_info(err_info, ...) _log_err_info(LOG_ERROR, err_info, __func__, __VA_ARGS__)
 
 /*
  * log_fatal + exit failure
@@ -53,8 +53,13 @@
 #define FATAL(...) do { log_fatal(__VA_ARGS__); exit(EXIT_FAILURE); } while (0)
 
 /*
+ * log_err + exit failure
+ */
+#define FATAL_ERR(err_code, ...) do { _log_err(LOG_FATAL, err_code, __func__, __VA_ARGS__); exit(EXIT_FAILURE); } while (0)
+
+/*
  * log_err_info + exit failure
  */
-#define FATAL_ERROR(err, ...) do { log_err_info(err, __VA_ARGS__); exit(EXIT_FAILURE); } while (0)
+#define FATAL_ERROR(err_info, ...) do { _log_err_info(LOG_FATAL, err_info, __func__, __VA_ARGS__); exit(EXIT_FAILURE); } while (0)
 
 #endif /* LOG_H */
--- a/src/nexus.c	Sun Mar 01 01:48:14 2009 +0200
+++ b/src/nexus.c	Sun Mar 01 02:02:48 2009 +0200
@@ -91,15 +91,21 @@
 
     // over-simplified connect
     if (ssl) {
+        log_info("SSL connecting to [%s]:%s", hostname, portname);
+
         if (sock_ssl_connect(&sock, hostname, portname, &err))
             FATAL_ERROR(&err, "sock_ssl_connect(%s, %s)", hostname, portname);
 
     } else {
+        log_info("connecting to [%s]:%s", hostname, portname);
+
         if (sock_tcp_connect(&sock, hostname, portname, &err))
             FATAL_ERROR(&err, "sock_tcp_connect(%s, %s)", hostname, portname);
 
     }
 
+    log_info("connected, registering");
+
     // create the irc connection state
     if (irc_conn_create(&conn, sock, &conn_config, &err))
         FATAL_ERROR(&err, "irc_conn_create");