[sheepdog] [PATCH v2] sheep: new assert() for better error messages

Hitoshi Mitake h.mitake at gmail.com
Fri Oct 5 13:51:12 CEST 2012


This patch removes direct usage of assert() in sheep. assert() in
daemon process causes problem because assert() writes its error
message to stderr. In addition, typical daemon processes including
sheep close stderr and dup() other fd. Actually, logger process of
sheep dup() /dev/null for stderr.

>From my experience, using assert() directly in daemon processes makes
debugging hard. So this patch adds assert() as a wrapper of
panic(). With this patch, assert() can find suitable output fd for
error messages with vprintf() and the error messages can appear in a
log file of sheep.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 include/strbuf.h          |    1 -
 include/util.h            |   18 ++++++++++++++++++
 sheep/cluster/accord.c    |    2 +-
 sheep/cluster/local.c     |    2 +-
 sheep/cluster/zookeeper.c |    2 +-
 sheep/group.c             |    1 -
 sheep/request.c           |    2 +-
 sheep/sheep.c             |    1 +
 sheep/trace/graph.c       |    1 -
 9 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/include/strbuf.h b/include/strbuf.h
index 42794b9..3cca6b5 100644
--- a/include/strbuf.h
+++ b/include/strbuf.h
@@ -1,7 +1,6 @@
 #ifndef STRBUF_H
 #define STRBUF_H
 
-#include <assert.h>
 #include <ctype.h>
 #include <stdarg.h>
 #include <unistd.h>
diff --git a/include/util.h b/include/util.h
index c482c12..90ed414 100644
--- a/include/util.h
+++ b/include/util.h
@@ -77,4 +77,22 @@ extern ssize_t xpread(int fd, void *buf, size_t count, off_t offset);
 extern ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset);
 extern int rmdir_r(char *dir_path);
 
+#ifdef assert
+#undef assert
+#endif
+
+#ifndef NDEBUG
+
+#define assert(expr) ((expr) ?						\
+			(void)0 :					\
+			panic("assert: %s:%d: %s: "			\
+				"Asserting `%s' failed.\n",		\
+				__FILE__, __LINE__, __func__, #expr))
+
+#else
+
+#define assert(expr) ((void)0)
+
+#endif	/* NDEBUG */
+
 #endif
diff --git a/sheep/cluster/accord.c b/sheep/cluster/accord.c
index a0f1d00..a05fc34 100644
--- a/sheep/cluster/accord.c
+++ b/sheep/cluster/accord.c
@@ -12,7 +12,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <search.h>
-#include <assert.h>
 #include <pthread.h>
 #include <sys/epoll.h>
 #include <sys/eventfd.h>
@@ -21,6 +20,7 @@
 #include "cluster.h"
 #include "event.h"
 #include "work.h"
+#include "util.h"
 
 #define BASE_FILE "/sheepdog"
 #define LOCK_FILE BASE_FILE "/lock"
diff --git a/sheep/cluster/local.c b/sheep/cluster/local.c
index c7ef38e..ec8289b 100644
--- a/sheep/cluster/local.c
+++ b/sheep/cluster/local.c
@@ -17,11 +17,11 @@
 #include <sys/file.h>
 #include <signal.h>
 #include <fcntl.h>
-#include <assert.h>
 
 #include "cluster.h"
 #include "event.h"
 #include "work.h"
+#include "util.h"
 
 #define MAX_EVENTS 500
 #define PROCESS_CHECK_INTERVAL 200 /* ms */
diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index 4cd5be8..e16e740 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -12,7 +12,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <search.h>
-#include <assert.h>
 #include <sys/epoll.h>
 #include <sys/eventfd.h>
 #include <zookeeper/zookeeper.h>
@@ -21,6 +20,7 @@
 #include "cluster.h"
 #include "event.h"
 #include "work.h"
+#include "util.h"
 
 #define SESSION_TIMEOUT 30000		/* millisecond */
 #define MEMBER_CREATE_TIMEOUT SESSION_TIMEOUT
diff --git a/sheep/group.c b/sheep/group.c
index 7ec3523..c9906b3 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -8,7 +8,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
diff --git a/sheep/request.c b/sheep/request.c
index 40a01d5..c53a487 100644
--- a/sheep/request.c
+++ b/sheep/request.c
@@ -8,7 +8,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -21,6 +20,7 @@
 #include <fcntl.h>
 
 #include "sheep_priv.h"
+#include "util.h"
 
 static void requeue_request(struct request *req);
 
diff --git a/sheep/sheep.c b/sheep/sheep.c
index cf42205..4076e52 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -30,6 +30,7 @@
 
 #include "sheep_priv.h"
 #include "trace/trace.h"
+#include "util.h"
 
 #define EPOLL_SIZE 4096
 #define DEFAULT_OBJECT_DIR "/tmp"
diff --git a/sheep/trace/graph.c b/sheep/trace/graph.c
index ab3a6c3..c6b6833 100644
--- a/sheep/trace/graph.c
+++ b/sheep/trace/graph.c
@@ -12,7 +12,6 @@
  */
 
 #include <time.h>
-#include <assert.h>
 #include <sched.h>
 
 #include "trace.h"
-- 
1.7.5.1




More information about the sheepdog mailing list