[sheepdog] [PATCH] use sd_err instead of fprintf(stderr)

MORITA Kazutaka morita.kazutaka at gmail.com
Fri Aug 9 17:56:16 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

If logger is not initialized, sd_err works similarly to
fprintf(stderr).

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/cluster.c        |   36 ++++-----
 collie/collie.c         |   28 +++----
 collie/common.c         |   22 +++--
 collie/farm/farm.c      |   15 ++--
 collie/farm/sha1_file.c |   12 ++-
 collie/farm/snap.c      |    8 +-
 collie/node.c           |   24 +++---
 collie/trace.c          |   25 +++---
 collie/treeview.c       |    2 +-
 collie/vdi.c            |  204 +++++++++++++++++++++++------------------------
 lib/logger.c            |    6 +-
 sheep/sheep.c           |   55 ++++++-------
 sheep/store.c           |    2 +-
 shepherd/shepherd.c     |    6 +-
 14 files changed, 209 insertions(+), 236 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index b9d3cf8..8826d15 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -48,8 +48,7 @@ static int list_store(void)
 		return EXIT_SYSFAIL;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Restore failed: %s\n",
-				sd_strerror(rsp->result));
+		sd_err("Restore failed: %s", sd_strerror(rsp->result));
 		return EXIT_FAILURE;
 	}
 
@@ -109,8 +108,7 @@ static int cluster_format(int argc, char **argv)
 		return EXIT_SYSFAIL;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Format failed: %s\n",
-				sd_strerror(rsp->result));
+		sd_err("Format failed: %s", sd_strerror(rsp->result));
 		if (rsp->result == SD_RES_NO_STORE)
 			return list_store();
 		else
@@ -197,7 +195,7 @@ static int cluster_shutdown(int argc, char **argv)
 
 	ret = send_light_req(&hdr, sdhost, sdport);
 	if (ret) {
-		fprintf(stderr, "failed to execute request\n");
+		sd_err("failed to execute request");
 		return EXIT_FAILURE;
 	}
 
@@ -236,7 +234,7 @@ static int list_snapshot(int argc, char **argv)
 	ret = EXIT_SUCCESS;
 out:
 	if (ret)
-		fprintf(stderr, "Fail to list snapshot.\n");
+		sd_err("Fail to list snapshot.");
 	free(buf);
 	return ret;
 }
@@ -280,12 +278,12 @@ static int save_snapshot(int argc, char **argv)
 
 	unused = strtol(tag, &p, 10);
 	if (tag != p) {
-		fprintf(stderr, "Tag should not start with number.\n");
+		sd_err("Tag should not start with number.");
 		return EXIT_USAGE;
 	}
 
 	if (!argv[optind]) {
-		fprintf(stderr, "Please specify the path to save snapshot.\n");
+		sd_err("Please specify the path to save snapshot.");
 		return EXIT_USAGE;
 	}
 	path = argv[optind];
@@ -294,8 +292,8 @@ static int save_snapshot(int argc, char **argv)
 		goto out;
 
 	if (farm_contain_snapshot(0, tag)) {
-		fprintf(stderr, "Snapshot tag has already been used for another"
-			" snapshot, please, use another one.\n");
+		sd_err("Snapshot tag has already been used for another"
+		       " snapshot, please, use another one.");
 		goto out;
 	}
 
@@ -308,7 +306,7 @@ static int save_snapshot(int argc, char **argv)
 	ret = EXIT_SUCCESS;
 out:
 	if (ret)
-		fprintf(stderr, "Fail to save snapshot to path: %s.\n", path);
+		sd_err("Fail to save snapshot to path: %s.", path);
 	object_tree_free();
 	return ret;
 }
@@ -325,7 +323,7 @@ static int load_snapshot(int argc, char **argv)
 		idx = 0;
 
 	if (!argv[optind]) {
-		fprintf(stderr, "Please specify the path to save snapshot.\n");
+		sd_err("Please specify the path to save snapshot.");
 		return EXIT_USAGE;
 	}
 	path = argv[optind];
@@ -334,7 +332,7 @@ static int load_snapshot(int argc, char **argv)
 		goto out;
 
 	if (!farm_contain_snapshot(idx, tag)) {
-		fprintf(stderr, "Snapshot index or tag does not exist.\n");
+		sd_err("Snapshot index or tag does not exist.");
 		goto out;
 	}
 
@@ -347,7 +345,7 @@ static int load_snapshot(int argc, char **argv)
 	ret = EXIT_SUCCESS;
 out:
 	if (ret)
-		fprintf(stderr, "Fail to load snapshot\n");
+		sd_err("Fail to load snapshot");
 	return ret;
 }
 
@@ -388,8 +386,8 @@ static int cluster_force_recover(int argc, char **argv)
 		return EXIT_SYSFAIL;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "failed to execute request, %s\n",
-			sd_strerror(rsp->result));
+		sd_err("failed to execute request, %s",
+		       sd_strerror(rsp->result));
 		return EXIT_FAILURE;
 	}
 
@@ -504,11 +502,11 @@ static int cluster_parser(int ch, char *opt)
 	case 'c':
 		copies = strtol(opt, &p, 10);
 		if (opt == p || copies < 1) {
-			fprintf(stderr, "There must be at least one copy of data\n");
+			sd_err("There must be at least one copy of data");
 			exit(EXIT_FAILURE);
 		} else if (copies > SD_MAX_COPIES) {
-			fprintf(stderr, "Redundancy may not exceed %d copies\n",
-				SD_MAX_COPIES);
+			sd_err("Redundancy may not exceed %d copies",
+			       SD_MAX_COPIES);
 			exit(EXIT_FAILURE);
 		}
 		cluster_cmd_data.copies = copies;
diff --git a/collie/collie.c b/collie/collie.c
index 55ed087..7ea32ec 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -70,8 +70,8 @@ int update_node_list(int max_nodes)
 		goto out;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to update node list: %s\n",
-				sd_strerror(rsp->result));
+		sd_err("Failed to update node list: %s",
+		       sd_strerror(rsp->result));
 		ret = -1;
 		goto out;
 	}
@@ -79,7 +79,7 @@ int update_node_list(int max_nodes)
 	size = rsp->data_length;
 	sd_nodes_nr = size / sizeof(*ent);
 	if (sd_nodes_nr == 0) {
-		fprintf(stderr, "There are no active sheep daemons\n");
+		sd_err("There are no active sheep daemons");
 		exit(EXIT_FAILURE);
 	}
 
@@ -125,7 +125,7 @@ static const struct sd_option *find_opt(int ch)
 		}
 	}
 
-	fprintf(stderr, "Internal error\n");
+	sd_err("Internal error");
 	exit(EXIT_SYSFAIL);
 }
 
@@ -190,7 +190,7 @@ static unsigned long setup_commands(const struct command *commands,
 	if (!found) {
 		if (cmd && strcmp(cmd, "help") && strcmp(cmd, "--help") &&
 		    strcmp(cmd, "-h")) {
-			fprintf(stderr, "Invalid command '%s'\n", cmd);
+			sd_err("Invalid command '%s'", cmd);
 			usage(commands, EXIT_USAGE);
 		}
 		usage(commands, 0);
@@ -211,10 +211,10 @@ static unsigned long setup_commands(const struct command *commands,
 	if (!command_fn) {
 		if (subcmd && strcmp(subcmd, "help") &&
 		    strcmp(subcmd, "--help") && strcmp(subcmd, "-h"))
-			fprintf(stderr, "Invalid command '%s %s'\n", cmd, subcmd);
-		fprintf(stderr, "Available %s commands:\n", cmd);
+			sd_err("Invalid command '%s %s'", cmd, subcmd);
+		sd_err("Available %s commands:", cmd);
 		for (s = commands[i].sub; s->name; s++)
-			fprintf(stderr, "  %s %s\n", cmd, s->name);
+			sd_err("  %s %s", cmd, s->name);
 		exit(EXIT_USAGE);
 	}
 
@@ -228,7 +228,7 @@ static void usage(const struct command *commands, int status)
 	char name[64];
 
 	if (status)
-		fprintf(stderr, "Try '%s --help' for more information.\n", program_name);
+		sd_err("Try '%s --help' for more information.", program_name);
 	else {
 		printf("Sheepdog administrator utility\n");
 		printf("Usage: %s <command> <subcommand> [options]\n", program_name);
@@ -317,7 +317,7 @@ static const struct sd_option *build_sd_options(const char *opts)
 
 static void crash_handler(int signo)
 {
-	fprintf(stderr, "collie exits unexpectedly (%s).\n", strsignal(signo));
+	sd_err("collie exits unexpectedly (%s).", strsignal(signo));
 
 	sd_backtrace();
 
@@ -372,7 +372,7 @@ int main(int argc, char **argv)
 		case 'p':
 			sdport = strtol(optarg, &p, 10);
 			if (optarg == p || sdport < 1 || sdport > UINT16_MAX) {
-				fprintf(stderr, "Invalid port number '%s'\n", optarg);
+				sd_err("Invalid port number '%s'", optarg);
 				exit(EXIT_USAGE);
 			}
 			break;
@@ -403,7 +403,7 @@ int main(int argc, char **argv)
 	if (flags & SUBCMD_FLAG_NEED_NODELIST) {
 		ret = update_node_list(SD_MAX_NODES);
 		if (ret < 0) {
-			fprintf(stderr, "Failed to get node list\n");
+			sd_err("Failed to get node list");
 			exit(EXIT_SYSFAIL);
 		}
 	}
@@ -415,12 +415,12 @@ int main(int argc, char **argv)
 		exit(EXIT_SYSFAIL);
 
 	if (init_work_queue(get_nr_nodes) != 0) {
-		fprintf(stderr, "Failed to init work queue\n");
+		sd_err("Failed to init work queue");
 		exit(EXIT_SYSFAIL);
 	}
 
 	if (sockfd_init()) {
-		fprintf(stderr, "sockfd_init() failed\n");
+		sd_err("sockfd_init() failed");
 		exit(EXIT_SYSFAIL);
 	}
 
diff --git a/collie/common.c b/collie/common.c
index fb5812c..9d0147c 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -56,13 +56,13 @@ int sd_read_object(uint64_t oid, void *data, unsigned int datalen,
 
 	ret = collie_exec_req(sdhost, sdport, &hdr, data);
 	if (ret < 0) {
-		fprintf(stderr, "Failed to read object %" PRIx64 "\n", oid);
+		sd_err("Failed to read object %" PRIx64, oid);
 		return SD_RES_EIO;
 	}
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to read object %" PRIx64 " %s\n", oid,
-			sd_strerror(rsp->result));
+		sd_err("Failed to read object %" PRIx64 " %s", oid,
+		       sd_strerror(rsp->result));
 		return rsp->result;
 	}
 
@@ -98,12 +98,12 @@ int sd_write_object(uint64_t oid, uint64_t cow_oid, void *data,
 
 	ret = collie_exec_req(sdhost, sdport, &hdr, data);
 	if (ret < 0) {
-		fprintf(stderr, "Failed to write object %" PRIx64 "\n", oid);
+		sd_err("Failed to write object %" PRIx64, oid);
 		return SD_RES_EIO;
 	}
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to write object %" PRIx64 ": %s\n", oid,
-				sd_strerror(rsp->result));
+		sd_err("Failed to write object %" PRIx64 ": %s", oid,
+		       sd_strerror(rsp->result));
 		return rsp->result;
 	}
 
@@ -137,7 +137,7 @@ int parse_vdi(vdi_parser_func_t func, size_t size, void *data)
 		memset(&i, 0, sizeof(i));
 		ret = sd_read_object(oid, &i, SD_INODE_HEADER_SIZE, 0, true);
 		if (ret != SD_RES_SUCCESS) {
-			fprintf(stderr, "Failed to read inode header\n");
+			sd_err("Failed to read inode header");
 			continue;
 		}
 
@@ -154,7 +154,7 @@ int parse_vdi(vdi_parser_func_t func, size_t size, void *data)
 					     rlen, SD_INODE_HEADER_SIZE, true);
 
 			if (ret != SD_RES_SUCCESS) {
-				fprintf(stderr, "Failed to read inode\n");
+				sd_err("Failed to read inode");
 				continue;
 			}
 		}
@@ -202,8 +202,7 @@ int send_light_req(struct sd_req *hdr, const char *host, int port)
 		return -1;
 
 	if (ret != SD_RES_SUCCESS) {
-		fprintf(stderr, "Response's result: %s\n",
-			sd_strerror(ret));
+		sd_err("Response's result: %s", sd_strerror(ret));
 		return -1;
 	}
 
@@ -221,8 +220,7 @@ int do_generic_subcommand(struct subcommand *sub, int argc, char **argv)
 			if (flags & SUBCMD_FLAG_NEED_NODELIST) {
 				ret = update_node_list(SD_MAX_NODES);
 				if (ret < 0) {
-					fprintf(stderr,
-						"Failed to get node list\n");
+					sd_err("Failed to get node list");
 					exit(EXIT_SYSFAIL);
 				}
 			}
diff --git a/collie/farm/farm.c b/collie/farm/farm.c
index cdf8e9b..90cb675 100644
--- a/collie/farm/farm.c
+++ b/collie/farm/farm.c
@@ -119,7 +119,7 @@ static int create_directory(const char *p)
 	strbuf_addstr(&buf, p);
 	if (xmkdir(buf.buf, 0755) < 0) {
 		if (errno == EEXIST)
-			fprintf(stderr, "Path is not a directory: %s\n", p);
+			sd_err("Path is not a directory: %s", p);
 		goto out;
 	}
 
@@ -144,7 +144,7 @@ static int create_directory(const char *p)
 	ret = 0;
 out:
 	if (ret)
-		fprintf(stderr, "Fail to create directory: %m\n");
+		sd_err("Fail to create directory: %m");
 	strbuf_release(&buf);
 	return ret;
 }
@@ -189,8 +189,8 @@ static int notify_vdi_add(uint32_t vdi_id, uint32_t nr_copies)
 	ret = collie_exec_req(sdhost, sdport, &hdr, buf);
 
 	if (ret)
-		fprintf(stderr, "Fail to notify vdi add event(%"PRIx32", %d)\n",
-			vdi_id, nr_copies);
+		sd_err("Fail to notify vdi add event(%"PRIx32", %d)", vdi_id,
+		       nr_copies);
 
 	free(buf);
 	return ret;
@@ -207,7 +207,7 @@ int farm_init(const char *path)
 	return 0;
 out:
 	if (ret)
-		fprintf(stderr, "Fail to init farm.\n");
+		sd_err("Fail to init farm.");
 	return ret;
 }
 
@@ -241,8 +241,7 @@ static void do_save_object(struct work *work)
 	return;
 error:
 	free(buf);
-	fprintf(stderr, "Fail to save object, oid %"PRIx64"\n",
-		sw->entry.oid);
+	sd_err("Fail to save object, oid %"PRIx64, sw->entry.oid);
 	uatomic_set_true(&work_error);
 }
 
@@ -360,7 +359,7 @@ static void do_load_object(struct work *work)
 	return;
 error:
 	free(buffer);
-	fprintf(stderr, "Fail to load object, oid %"PRIx64"\n", sw->entry.oid);
+	sd_err("Fail to load object, oid %"PRIx64, sw->entry.oid);
 	uatomic_set_true(&work_error);
 }
 
diff --git a/collie/farm/sha1_file.c b/collie/farm/sha1_file.c
index 7bc00ee..3ba2519 100644
--- a/collie/farm/sha1_file.c
+++ b/collie/farm/sha1_file.c
@@ -66,16 +66,15 @@ static int sha1_buffer_write(const unsigned char *sha1,
 	fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 	if (fd < 0) {
 		if (errno != EEXIST) {
-			fprintf(stderr,
-				"failed to open file %s with error: %m\n",
-				filename);
+			sd_err("failed to open file %s with error: %m",
+			       filename);
 			ret = -1;
 		}
 		goto err_open;
 	}
 	len = xwrite(fd, buf, size);
 	if (len != size) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		close(fd);
 		return -1;
 	}
@@ -104,8 +103,7 @@ static int verify_sha1_file(const unsigned char *sha1,
 
 	sha1_from_buffer(buf, len, tmp);
 	if (memcmp((char *)tmp, (char *)sha1, SHA1_DIGEST_SIZE) != 0) {
-		fprintf(stderr, "failed, %s != %s\n", sha1_to_hex(sha1),
-			sha1_to_hex(tmp));
+		sd_err("failed, %s != %s", sha1_to_hex(sha1), sha1_to_hex(tmp));
 		return -1;
 	}
 	return 0;
@@ -123,7 +121,7 @@ void *sha1_file_read(const unsigned char *sha1, size_t *size)
 		return NULL;
 	}
 	if (fstat(fd, &st) < 0) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		goto out;
 	}
 
diff --git a/collie/farm/snap.c b/collie/farm/snap.c
index 071c12d..8c46484 100644
--- a/collie/farm/snap.c
+++ b/collie/farm/snap.c
@@ -37,7 +37,7 @@ int snap_init(const char *farm_dir)
 	fd = open(snap_log_path, O_CREAT | O_EXCL, 0666);
 	if (fd < 0) {
 		if (errno != EEXIST) {
-			fprintf(stderr, "%m\n");
+			sd_err("%m");
 			goto out;
 		}
 	}
@@ -60,7 +60,7 @@ int snap_log_write(uint32_t idx, const char *tag, unsigned char *sha1)
 
 	fd = open(snap_log_path, O_WRONLY | O_APPEND);
 	if (fd < 0) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		goto out;
 	}
 
@@ -86,11 +86,11 @@ void *snap_log_read(int *out_nr)
 
 	fd = open(snap_log_path, O_RDONLY);
 	if (fd < 0) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		goto out;
 	}
 	if (fstat(fd, &st) < 0) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		goto out_close;
 	}
 
diff --git a/collie/node.c b/collie/node.c
index 1eb5c97..9307b7f 100644
--- a/collie/node.c
+++ b/collie/node.c
@@ -99,7 +99,7 @@ static int node_info(int argc, char **argv)
 	}
 
 	if (success == 0) {
-		fprintf(stderr, "Cannot get information from any nodes\n");
+		sd_err("Cannot get information from any nodes");
 		return EXIT_SYSFAIL;
 	}
 
@@ -131,7 +131,7 @@ static int get_recovery_state(struct recovery_state *state)
 
 	ret = collie_exec_req(sdhost, sdport, &req, state);
 	if (ret < 0) {
-		fprintf(stderr, "Failed to execute request\n");
+		sd_err("Failed to execute request");
 		return -1;
 	}
 
@@ -252,14 +252,14 @@ static int node_kill(int argc, char **argv)
 	const char *p = argv[optind++];
 
 	if (!is_numeric(p)) {
-		fprintf(stderr, "Invalid node id '%s', "
-			"please specify a numeric value\n", p);
+		sd_err("Invalid node id '%s', please specify a numeric value",
+		       p);
 		exit(EXIT_USAGE);
 	}
 
 	node_id = strtol(p, NULL, 10);
 	if (node_id < 0 || node_id >= sd_nodes_nr) {
-		fprintf(stderr, "Invalid node id '%d'\n", node_id);
+		sd_err("Invalid node id '%d'", node_id);
 		exit(EXIT_USAGE);
 	}
 
@@ -269,7 +269,7 @@ static int node_kill(int argc, char **argv)
 
 	ret = send_light_req(&req, host, sd_nodes[node_id].nid.port);
 	if (ret) {
-		fprintf(stderr, "Failed to execute request\n");
+		sd_err("Failed to execute request");
 		exit(EXIT_FAILURE);
 	}
 
@@ -295,8 +295,8 @@ static int node_md_info(struct node_id *nid)
 		return EXIT_SYSFAIL;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "failed to get multi-disk infomation: %s\n",
-			sd_strerror(rsp->result));
+		sd_err("failed to get multi-disk infomation: %s",
+		       sd_strerror(rsp->result));
 		return EXIT_FAILURE;
 	}
 
@@ -324,7 +324,7 @@ static int md_info(int argc, char **argv)
 		struct node_id nid = {.port = sdport};
 
 		if (!str_to_addr(sdhost, nid.addr)) {
-			fprintf(stderr, "Invalid address %s\n", sdhost);
+			sd_err("Invalid address %s", sdhost);
 			return EXIT_FAILURE;
 		}
 
@@ -347,7 +347,7 @@ static int do_plug_unplug(char *disks, bool plug)
 	int ret;
 
 	if (!strlen(disks)) {
-		fprintf(stderr, "Empty path isn't allowed\n");
+		sd_err("Empty path isn't allowed");
 		return EXIT_FAILURE;
 	}
 
@@ -363,8 +363,8 @@ static int do_plug_unplug(char *disks, bool plug)
 		return EXIT_SYSFAIL;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to execute request, look for sheep.log"
-			" for more information\n");
+		sd_err("Failed to execute request, look for sheep.log"
+		       " for more information");
 		return EXIT_FAILURE;
 	}
 
diff --git a/collie/trace.c b/collie/trace.c
index 87ccecb..30764ce 100644
--- a/collie/trace.c
+++ b/collie/trace.c
@@ -82,7 +82,7 @@ static int trace_read_buffer(void)
 
 	tfd = open(tracefile, O_CREAT | O_RDWR | O_APPEND | O_TRUNC, 0644);
 	if (tfd < 0) {
-		fprintf(stderr, "can't create tracefile\n");
+		sd_err("can't create tracefile");
 		return EXIT_SYSFAIL;
 	}
 
@@ -98,8 +98,7 @@ read_buffer:
 		goto read_buffer;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Trace failed: %s\n",
-				sd_strerror(rsp->result));
+		sd_err("Trace failed: %s", sd_strerror(rsp->result));
 		return EXIT_FAILURE;
 	}
 
@@ -132,14 +131,13 @@ static int trace_enable(int argc, char **argv)
 	case SD_RES_SUCCESS:
 		break;
 	case SD_RES_NO_SUPPORT:
-		fprintf(stderr, "no such tracer %s\n", tracer);
+		sd_err("no such tracer %s", tracer);
 		return EXIT_FAILURE;
 	case SD_RES_INVALID_PARMS:
-		fprintf(stderr, "tracer %s is already enabled\n", tracer);
+		sd_err("tracer %s is already enabled", tracer);
 		return EXIT_FAILURE;
 	default:
-		fprintf(stderr, "unknown error (%s)\n",
-			sd_strerror(rsp->result));
+		sd_err("unknown error (%s)", sd_strerror(rsp->result));
 		return EXIT_SYSFAIL;
 	}
 
@@ -165,14 +163,13 @@ static int trace_disable(int argc, char **argv)
 	case SD_RES_SUCCESS:
 		break;
 	case SD_RES_NO_SUPPORT:
-		fprintf(stderr, "no such tracer %s\n", tracer);
+		sd_err("no such tracer %s", tracer);
 		return EXIT_FAILURE;
 	case SD_RES_INVALID_PARMS:
-		fprintf(stderr, "tracer %s is not enabled\n", tracer);
+		sd_err("tracer %s is not enabled", tracer);
 		return EXIT_FAILURE;
 	default:
-		fprintf(stderr, "unknown error (%s)\n",
-			sd_strerror(rsp->result));
+		sd_err("unknown error (%s)", sd_strerror(rsp->result));
 		return EXIT_SYSFAIL;
 	}
 
@@ -204,12 +201,12 @@ static int trace_cat(int argc, char **argv)
 	void *map;
 
 	if (fd < 0) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		return EXIT_SYSFAIL;
 	}
 
 	if (fstat(fd, &st) < 0) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		close(fd);
 		return EXIT_SYSFAIL;
 	}
@@ -220,7 +217,7 @@ static int trace_cat(int argc, char **argv)
 	map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
 	close(fd);
 	if (map == MAP_FAILED) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		return EXIT_SYSFAIL;
 	}
 	parse_trace_buffer(map, st.st_size);
diff --git a/collie/treeview.c b/collie/treeview.c
index 2910170..baf0f00 100644
--- a/collie/treeview.c
+++ b/collie/treeview.c
@@ -175,7 +175,7 @@ void dump_tree(void)
 	width = malloc(sizeof(int) * depth);
 	more = malloc(sizeof(int) * depth);
 	if (!width || !more) {
-		fprintf(stderr, "Failed to allocate memory\n");
+		sd_err("Failed to allocate memory");
 		return;
 	}
 
diff --git a/collie/vdi.c b/collie/vdi.c
index 3d5382e..b09734c 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -76,9 +76,9 @@ static int parse_option_size(const char *value, uint64_t *ret)
 		*ret = (uint64_t) sizef;
 		break;
 	default:
-		fprintf(stderr, "Invalid size '%s'\n", value);
-		fprintf(stderr, "You may use k, M, G or T suffixes for "
-			"kilobytes, megabytes, gigabytes and terabytes.\n");
+		sd_err("Invalid size '%s'", value);
+		sd_err("You may use k, M, G or T suffixes for "
+		       "kilobytes, megabytes, gigabytes and terabytes.");
 		return -1;
 	}
 
@@ -250,11 +250,11 @@ static int do_print_obj(char *sheep, uint64_t oid, struct sd_rsp *rsp,
 		break;
 	case SD_RES_OLD_NODE_VER:
 	case SD_RES_NEW_NODE_VER:
-		fprintf(stderr, "The node list has changed: please try again\n");
+		sd_err("The node list has changed: please try again");
 		break;
 	default:
-		fprintf(stderr, "%s: hit an unexpected error (%s)\n",
-			sheep, sd_strerror(rsp->result));
+		sd_err("%s: hit an unexpected error (%s)", sheep,
+		       sd_strerror(rsp->result));
 		break;
 	}
 
@@ -287,11 +287,11 @@ static int get_data_oid(char *sheep, uint64_t oid, struct sd_rsp *rsp,
 		break;
 	case SD_RES_OLD_NODE_VER:
 	case SD_RES_NEW_NODE_VER:
-		fprintf(stderr, "The node list has changed: please try again\n");
+		sd_err("The node list has changed: please try again");
 		break;
 	default:
-		fprintf(stderr, "%s: hit an unexpected error (%s)\n",
-			sheep, sd_strerror(rsp->result));
+		sd_err("%s: hit an unexpected error (%s)", sheep,
+		       sd_strerror(rsp->result));
 		break;
 	}
 
@@ -408,8 +408,8 @@ static int find_vdi_name(const char *vdiname, uint32_t snapid, const char *tag,
 		return -1;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Cannot get VDI info for %s %d %s: %s\n",
-			vdiname, snapid, tag, sd_strerror(rsp->result));
+		sd_err("Cannot get VDI info for %s %d %s: %s", vdiname, snapid,
+		       tag, sd_strerror(rsp->result));
 		return -1;
 	}
 	*vid = rsp->vdi.vdi_id;
@@ -426,20 +426,19 @@ static int read_vdi_obj(const char *vdiname, int snapid, const char *tag,
 
 	ret = find_vdi_name(vdiname, snapid, tag, &vid, 0);
 	if (ret < 0) {
-		fprintf(stderr, "Failed to open VDI %s\n", vdiname);
+		sd_err("Failed to open VDI %s", vdiname);
 		return EXIT_FAILURE;
 	}
 
 	ret = sd_read_object(vid_to_vdi_oid(vid), inode, size, 0, true);
 	if (ret != SD_RES_SUCCESS) {
 		if (snapid) {
-			fprintf(stderr, "Failed to read a snapshot %s:%d\n",
-				vdiname, snapid);
+			sd_err("Failed to read a snapshot %s:%d", vdiname,
+			       snapid);
 		} else if (tag && tag[0]) {
-			fprintf(stderr, "Failed to read a snapshot %s:%s\n",
-				vdiname, tag);
+			sd_err("Failed to read a snapshot %s:%s", vdiname, tag);
 		} else {
-			fprintf(stderr, "Failed to read a vdi %s\n", vdiname);
+			sd_err("Failed to read a vdi %s", vdiname);
 		}
 		return EXIT_FAILURE;
 	}
@@ -476,8 +475,8 @@ int do_vdi_create(const char *vdiname, int64_t vdi_size,
 		return EXIT_SYSFAIL;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to create VDI %s: %s\n", vdiname,
-				sd_strerror(rsp->result));
+		sd_err("Failed to create VDI %s: %s", vdiname,
+		       sd_strerror(rsp->result));
 		return EXIT_FAILURE;
 	}
 
@@ -497,20 +496,20 @@ static int vdi_create(int argc, char **argv)
 	struct sd_inode *inode = NULL;
 
 	if (!argv[optind]) {
-		fprintf(stderr, "Please specify the VDI size\n");
+		sd_err("Please specify the VDI size");
 		return EXIT_USAGE;
 	}
 	ret = parse_option_size(argv[optind], &size);
 	if (ret < 0)
 		return EXIT_USAGE;
 	if (size > SD_MAX_VDI_SIZE) {
-		fprintf(stderr, "VDI size is too large\n");
+		sd_err("VDI size is too large");
 		return EXIT_USAGE;
 	}
 
 	if (nr_copies > sd_nodes_nr) {
-		fprintf(stderr, "There are not enough nodes(%d) to hold "
-			"the copies(%d)\n", sd_nodes_nr, nr_copies);
+		sd_err("There are not enough nodes(%d) to hold the copies(%d)",
+		       sd_nodes_nr, nr_copies);
 		return EXIT_USAGE;
 	}
 
@@ -523,7 +522,7 @@ static int vdi_create(int argc, char **argv)
 
 	ret = sd_read_object(vid_to_vdi_oid(vid), inode, sizeof(*inode), 0, true);
 	if (ret != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to read a newly created VDI object\n");
+		sd_err("Failed to read a newly created VDI object");
 		ret = EXIT_FAILURE;
 		goto out;
 	}
@@ -573,8 +572,8 @@ static int vdi_snapshot(int argc, char **argv)
 	struct sd_inode *inode = (struct sd_inode *)buf;
 
 	if (vdi_cmd_data.snapshot_id != 0) {
-		fprintf(stderr, "Please specify a non-integer value for "
-			"a snapshot tag name\n");
+		sd_err("Please specify a non-integer value for "
+		       "a snapshot tag name");
 		return EXIT_USAGE;
 	}
 
@@ -613,14 +612,14 @@ static int vdi_clone(int argc, char **argv)
 
 	dst_vdi = argv[optind];
 	if (!dst_vdi) {
-		fprintf(stderr, "Destination VDI name must be specified\n");
+		sd_err("Destination VDI name must be specified");
 		ret = EXIT_USAGE;
 		goto out;
 	}
 
 	if (!vdi_cmd_data.snapshot_id && !vdi_cmd_data.snapshot_tag[0]) {
-		fprintf(stderr, "Only snapshot VDIs can be cloned\n");
-		fprintf(stderr, "Please specify the '-s' option\n");
+		sd_err("Only snapshot VDIs can be cloned");
+		sd_err("Please specify the '-s' option");
 		ret = EXIT_USAGE;
 		goto out;
 	}
@@ -694,14 +693,14 @@ static int vdi_resize(int argc, char **argv)
 	struct sd_inode *inode = (struct sd_inode *)buf;
 
 	if (!argv[optind]) {
-		fprintf(stderr, "Please specify the new size for the VDI\n");
+		sd_err("Please specify the new size for the VDI");
 		return EXIT_USAGE;
 	}
 	ret = parse_option_size(argv[optind], &new_size);
 	if (ret < 0)
 		return EXIT_USAGE;
 	if (new_size > SD_MAX_VDI_SIZE) {
-		fprintf(stderr, "New VDI size is too large\n");
+		sd_err("New VDI size is too large");
 		return EXIT_USAGE;
 	}
 
@@ -710,7 +709,7 @@ static int vdi_resize(int argc, char **argv)
 		return ret;
 
 	if (new_size < inode->vdi_size) {
-		fprintf(stderr, "Shrinking VDIs is not implemented\n");
+		sd_err("Shrinking VDIs is not implemented");
 		return EXIT_USAGE;
 	}
 	inode->vdi_size = new_size;
@@ -718,7 +717,7 @@ static int vdi_resize(int argc, char **argv)
 	ret = sd_write_object(vid_to_vdi_oid(vid), 0, inode, SD_INODE_HEADER_SIZE, 0,
 			      0, inode->nr_copies, false, true);
 	if (ret != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to update an inode header\n");
+		sd_err("Failed to update an inode header");
 		return EXIT_FAILURE;
 	}
 
@@ -735,7 +734,7 @@ static int do_vdi_delete(const char *vdiname, int snap_id, const char *snap_tag)
 
 	ret = find_vdi_name(vdiname, snap_id, snap_tag, &vid, 0);
 	if (ret < 0) {
-		fprintf(stderr, "Failed to open VDI %s\n", vdiname);
+		sd_err("Failed to open VDI %s", vdiname);
 		return EXIT_FAILURE;
 	}
 
@@ -744,7 +743,7 @@ static int do_vdi_delete(const char *vdiname, int snap_id, const char *snap_tag)
 
 	ret = send_light_req(&hdr, sdhost, sdport);
 	if (ret) {
-		fprintf(stderr, "failed to execute request\n");
+		sd_err("failed to execute request");
 		return EXIT_FAILURE;
 	}
 
@@ -762,8 +761,8 @@ static int do_vdi_delete(const char *vdiname, int snap_id, const char *snap_tag)
 		return EXIT_SYSFAIL;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to delete %s: %s\n", vdiname,
-				sd_strerror(rsp->result));
+		sd_err("Failed to delete %s: %s", vdiname,
+		       sd_strerror(rsp->result));
 		if (rsp->result == SD_RES_NO_VDI)
 			return EXIT_MISSING;
 		else
@@ -790,7 +789,7 @@ static int vdi_rollback(int argc, char **argv)
 	struct sd_inode *inode = (struct sd_inode *)buf;
 
 	if (!vdi_cmd_data.snapshot_id && !vdi_cmd_data.snapshot_tag[0]) {
-		fprintf(stderr, "Please specify the '-s' option\n");
+		sd_err("Please specify the '-s' option");
 		return EXIT_USAGE;
 	}
 
@@ -806,7 +805,7 @@ static int vdi_rollback(int argc, char **argv)
 
 	ret = do_vdi_delete(vdiname, 0, NULL);
 	if (ret != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to delete the current state\n");
+		sd_err("Failed to delete the current state");
 		return EXIT_FAILURE;
 	}
 
@@ -841,7 +840,7 @@ static int vdi_object(int argc, char **argv)
 
 	vid = info.vid;
 	if (vid == 0) {
-		fprintf(stderr, "VDI not found\n");
+		sd_err("VDI not found");
 		return EXIT_MISSING;
 	}
 
@@ -873,7 +872,8 @@ static int vdi_object(int argc, char **argv)
 				printf("The inode object 0x%" PRIx32 " idx %u is not allocated\n",
 				       vid, idx);
 		} else
-			fprintf(stderr, "Failed to read the inode object 0x%" PRIx32 "\n", vid);
+			sd_err("Failed to read the inode object 0x%" PRIx32,
+			       vid);
 	}
 
 	return EXIT_SUCCESS;
@@ -965,7 +965,7 @@ static int vdi_track(int argc, char **argv)
 	vid = info.vid;
 	nr_copies = info.nr_copies;
 	if (vid == 0) {
-		fprintf(stderr, "VDI not found\n");
+		sd_err("VDI not found");
 		return EXIT_MISSING;
 	}
 
@@ -987,8 +987,7 @@ static int vdi_track(int argc, char **argv)
 		   &oid_info, SD_DATA_OBJ_SIZE);
 
 	if (!oid_info.success) {
-		fprintf(stderr, "Failed to read the inode object 0x%"PRIx32"\n",
-			vid);
+		sd_err("Failed to read the inode object 0x%" PRIx32, vid);
 		goto err;
 	}
 	if (!oid_info.data_oid) {
@@ -1062,7 +1061,7 @@ static int vdi_setattr(int argc, char **argv)
 
 	key = argv[optind++];
 	if (!key) {
-		fprintf(stderr, "Please specify the attribute key\n");
+		sd_err("Please specify the attribute key");
 		return EXIT_USAGE;
 	}
 
@@ -1075,7 +1074,7 @@ reread:
 		ret = read(STDIN_FILENO, value + offset,
 			   SD_MAX_VDI_ATTR_VALUE_LEN - offset);
 		if (ret < 0) {
-			fprintf(stderr, "Failed to read attribute value from stdin: %m\n");
+			sd_err("Failed to read attribute value from stdin: %m");
 			return EXIT_SYSFAIL;
 		}
 		if (ret > 0) {
@@ -1094,17 +1093,16 @@ reread:
 				vdi_cmd_data.exclusive, vdi_cmd_data.delete);
 	if (ret) {
 		if (ret == SD_RES_VDI_EXIST) {
-			fprintf(stderr, "The attribute '%s' already exists\n", key);
+			sd_err("The attribute '%s' already exists", key);
 			return EXIT_EXISTS;
 		} else if (ret == SD_RES_NO_OBJ) {
-			fprintf(stderr, "Attribute '%s' not found\n", key);
+			sd_err("Attribute '%s' not found", key);
 			return EXIT_MISSING;
 		} else if (ret == SD_RES_NO_VDI) {
-			fprintf(stderr, "VDI not found\n");
+			sd_err("VDI not found");
 			return EXIT_MISSING;
 		} else
-			fprintf(stderr, "Failed to set attribute: %s\n",
-				sd_strerror(ret));
+			sd_err("Failed to set attribute: %s", sd_strerror(ret));
 		return EXIT_FAILURE;
 	}
 
@@ -1121,7 +1119,7 @@ static int vdi_getattr(int argc, char **argv)
 
 	key = argv[optind++];
 	if (!key) {
-		fprintf(stderr, "Please specify the attribute key\n");
+		sd_err("Please specify the attribute key");
 		return EXIT_USAGE;
 	}
 
@@ -1129,14 +1127,13 @@ static int vdi_getattr(int argc, char **argv)
 				vdi_cmd_data.snapshot_id, key, NULL, 0, &vid,
 				&attr_oid, &nr_copies, false, false, false);
 	if (ret == SD_RES_NO_OBJ) {
-		fprintf(stderr, "Attribute '%s' not found\n", key);
+		sd_err("Attribute '%s' not found", key);
 		return EXIT_MISSING;
 	} else if (ret == SD_RES_NO_VDI) {
-		fprintf(stderr, "VDI not found\n");
+		sd_err("VDI not found");
 		return EXIT_MISSING;
 	} else if (ret) {
-		fprintf(stderr, "Failed to find attribute oid: %s\n",
-			sd_strerror(ret));
+		sd_err("Failed to find attribute oid: %s", sd_strerror(ret));
 		return EXIT_MISSING;
 	}
 
@@ -1144,8 +1141,7 @@ static int vdi_getattr(int argc, char **argv)
 
 	ret = sd_read_object(oid, &vattr, SD_ATTR_OBJ_SIZE, 0, true);
 	if (ret != SD_RES_SUCCESS) {
-		fprintf(stderr, "Failed to read attribute oid: %s\n",
-			sd_strerror(ret));
+		sd_err("Failed to read attribute oid: %s", sd_strerror(ret));
 		return EXIT_SYSFAIL;
 	}
 
@@ -1183,7 +1179,7 @@ static int vdi_read(int argc, char **argv)
 		goto out;
 
 	if (inode->vdi_size < offset) {
-		fprintf(stderr, "Read offset is beyond the end of the VDI\n");
+		sd_err("Read offset is beyond the end of the VDI");
 		ret = EXIT_FAILURE;
 		goto out;
 	}
@@ -1198,7 +1194,7 @@ static int vdi_read(int argc, char **argv)
 			oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
 			ret = sd_read_object(oid, buf, len, offset, false);
 			if (ret != SD_RES_SUCCESS) {
-				fprintf(stderr, "Failed to read VDI\n");
+				sd_err("Failed to read VDI");
 				ret = EXIT_FAILURE;
 				goto out;
 			}
@@ -1207,7 +1203,7 @@ static int vdi_read(int argc, char **argv)
 
 		ret = xwrite(STDOUT_FILENO, buf, len);
 		if (ret < 0) {
-			fprintf(stderr, "Failed to write to stdout: %m\n");
+			sd_err("Failed to write to stdout: %m");
 			abort();
 			ret = EXIT_SYSFAIL;
 			goto out;
@@ -1256,7 +1252,7 @@ static int vdi_write(int argc, char **argv)
 		goto out;
 
 	if (inode->vdi_size < offset) {
-		fprintf(stderr, "Write offset is beyond the end of the VDI\n");
+		sd_err("Write offset is beyond the end of the VDI");
 		ret = EXIT_FAILURE;
 		goto out;
 	}
@@ -1282,7 +1278,7 @@ static int vdi_write(int argc, char **argv)
 
 		ret = xread(STDIN_FILENO, buf, len);
 		if (ret < 0) {
-			fprintf(stderr, "Failed to read from stdin: %m\n");
+			sd_err("Failed to read from stdin: %m");
 			ret = EXIT_SYSFAIL;
 			goto out;
 		} else if (ret < len) {
@@ -1296,7 +1292,7 @@ static int vdi_write(int argc, char **argv)
 		ret = sd_write_object(oid, old_oid, buf, len, offset, flags,
 				      inode->nr_copies, create, false);
 		if (ret != SD_RES_SUCCESS) {
-			fprintf(stderr, "Failed to write VDI\n");
+			sd_err("Failed to write VDI");
 			ret = EXIT_FAILURE;
 			goto out;
 		}
@@ -1359,8 +1355,8 @@ static void *read_object_from(const struct sd_vnode *vnode, uint64_t oid)
 		free(buf);
 		return NULL;
 	default:
-		fprintf(stderr, "FATAL: failed to read %"PRIx64", %s\n",
-			oid, sd_strerror(rsp->result));
+		sd_err("FATAL: failed to read %"PRIx64", %s", oid,
+		       sd_strerror(rsp->result));
 		exit(EXIT_FAILURE);
 	}
 	return buf;
@@ -1390,8 +1386,8 @@ static void write_object_to(const struct sd_vnode *vnode, uint64_t oid,
 		exit(EXIT_SYSFAIL);
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "FATAL: failed to write %"PRIx64", %s\n",
-			oid, sd_strerror(rsp->result));
+		sd_err("FATAL: failed to write %"PRIx64", %s", oid,
+		       sd_strerror(rsp->result));
 		exit(EXIT_FAILURE);
 	}
 }
@@ -1481,9 +1477,8 @@ static void vdi_hash_check_work(struct work *work)
 		vcw->object_found = false;
 		break;
 	default:
-		fprintf(stderr, "failed to read %"PRIx64" from %s:%d, %s\n",
-			info->oid, host, vcw->vnode->nid.port,
-			sd_strerror(ret));
+		sd_err("failed to read %" PRIx64 " from %s:%d, %s", info->oid,
+		       host, vcw->vnode->nid.port, sd_strerror(ret));
 		exit(EXIT_FAILURE);
 	}
 }
@@ -1499,7 +1494,7 @@ static void vdi_hash_check_main(struct work *work)
 		return;
 
 	if (info->base  == NULL) {
-		fprintf(stderr, "no node has %"PRIx64"\n", info->oid);
+		sd_err("no node has %" PRIx64, info->oid);
 		exit(EXIT_FAILURE);
 	}
 
@@ -1559,12 +1554,11 @@ static int vdi_check(int argc, char **argv)
 			   vdi_cmd_data.snapshot_tag, &vid, inode,
 			   SD_INODE_SIZE);
 	if (ret != EXIT_SUCCESS) {
-		fprintf(stderr, "FATAL: no inode objects\n");
+		sd_err("FATAL: no inode objects");
 		goto out;
 	}
 	if (sd_nodes_nr < inode->nr_copies) {
-		fprintf(stderr, "ABORT: Not enough active nodes for "
-			"consistency-check\n");
+		sd_err("ABORT: Not enough active nodes for consistency-check");
 		return EXIT_FAILURE;
 	}
 
@@ -1648,8 +1642,8 @@ static int get_obj_backup(int idx, uint32_t from_vid, uint32_t to_vid,
 		ret = sd_read_object(vid_to_data_oid(to_vid, idx), backup->data,
 				     SD_DATA_OBJ_SIZE, 0, true);
 		if (ret != SD_RES_SUCCESS) {
-			fprintf(stderr, "Failed to read object %"PRIx32", %d\n",
-				to_vid, idx);
+			sd_err("Failed to read object %" PRIx32 ", %d", to_vid,
+			       idx);
 			return EXIT_FAILURE;
 		}
 	} else
@@ -1659,8 +1653,8 @@ static int get_obj_backup(int idx, uint32_t from_vid, uint32_t to_vid,
 		ret = sd_read_object(vid_to_data_oid(from_vid, idx), from_data,
 				     SD_DATA_OBJ_SIZE, 0, true);
 		if (ret != SD_RES_SUCCESS) {
-			fprintf(stderr, "Failed to read object %"PRIx32", %d\n",
-				from_vid, idx);
+			sd_err("Failed to read object %" PRIx32 ", %d",
+			       from_vid, idx);
 			return EXIT_FAILURE;
 		}
 	}
@@ -1687,8 +1681,7 @@ static int vdi_backup(int argc, char **argv)
 	if ((!vdi_cmd_data.snapshot_id && !vdi_cmd_data.snapshot_tag[0]) ||
 	    (!vdi_cmd_data.from_snapshot_id &&
 	     !vdi_cmd_data.from_snapshot_tag[0])) {
-		fprintf(stderr, "Please specify snapshots with '-F' and '-s'"
-			"options\n");
+		sd_err("Please specify snapshots with '-F' and '-s' options");
 		ret = EXIT_USAGE;
 		goto out;
 	}
@@ -1709,7 +1702,7 @@ static int vdi_backup(int argc, char **argv)
 
 	ret = xwrite(STDOUT_FILENO, &hdr, sizeof(hdr));
 	if (ret < 0) {
-		fprintf(stderr, "failed to write backup header, %m\n");
+		sd_err("failed to write backup header, %m");
 		ret = EXIT_SYSFAIL;
 		goto out;
 	}
@@ -1731,14 +1724,14 @@ static int vdi_backup(int argc, char **argv)
 		ret = xwrite(STDOUT_FILENO, backup,
 			     sizeof(*backup) - sizeof(backup->data));
 		if (ret < 0) {
-			fprintf(stderr, "failed to write backup data, %m\n");
+			sd_err("failed to write backup data, %m");
 			ret = EXIT_SYSFAIL;
 			goto out;
 		}
 		ret = xwrite(STDOUT_FILENO, backup->data + backup->offset,
 			     backup->length);
 		if (ret < 0) {
-			fprintf(stderr, "failed to write backup data, %m\n");
+			sd_err("failed to write backup data, %m");
 			ret = EXIT_SYSFAIL;
 			goto out;
 		}
@@ -1750,7 +1743,7 @@ static int vdi_backup(int argc, char **argv)
 	ret = xwrite(STDOUT_FILENO, backup,
 		     sizeof(*backup) - sizeof(backup->data));
 	if (ret < 0) {
-		fprintf(stderr, "failed to write end marker, %m\n");
+		sd_err("failed to write end marker, %m");
 		ret = EXIT_SYSFAIL;
 		goto out;
 	}
@@ -1796,11 +1789,11 @@ static uint32_t do_restore(const char *vdiname, int snapid, const char *tag)
 
 	ret = xread(STDIN_FILENO, &hdr, sizeof(hdr));
 	if (ret != sizeof(hdr))
-		fprintf(stderr, "failed to read backup header, %m\n");
+		sd_err("failed to read backup header, %m");
 
 	if (hdr.version != VDI_BACKUP_FORMAT_VERSION ||
 	    hdr.magic != VDI_BACKUP_MAGIC) {
-		fprintf(stderr, "The backup file is corrupted\n");
+		sd_err("The backup file is corrupted");
 		ret = EXIT_SYSFAIL;
 		goto out;
 	}
@@ -1812,7 +1805,7 @@ static uint32_t do_restore(const char *vdiname, int snapid, const char *tag)
 	ret = do_vdi_create(vdiname, inode->vdi_size, inode->vdi_id, &vid,
 			    false, inode->nr_copies);
 	if (ret != EXIT_SUCCESS) {
-		fprintf(stderr, "Failed to read VDI\n");
+		sd_err("Failed to read VDI");
 		goto out;
 	}
 
@@ -1820,7 +1813,7 @@ static uint32_t do_restore(const char *vdiname, int snapid, const char *tag)
 		ret = xread(STDIN_FILENO, backup,
 			    sizeof(*backup) - sizeof(backup->data));
 		if (ret != sizeof(*backup) - sizeof(backup->data)) {
-			fprintf(stderr, "failed to read backup data\n");
+			sd_err("failed to read backup data");
 			ret = EXIT_SYSFAIL;
 			break;
 		}
@@ -1832,14 +1825,14 @@ static uint32_t do_restore(const char *vdiname, int snapid, const char *tag)
 
 		ret = xread(STDIN_FILENO, backup->data, backup->length);
 		if (ret != backup->length) {
-			fprintf(stderr, "failed to read backup data\n");
+			sd_err("failed to read backup data");
 			ret = EXIT_SYSFAIL;
 			break;
 		}
 
 		ret = restore_obj(backup, vid, inode);
 		if (ret != SD_RES_SUCCESS) {
-			fprintf(stderr, "failed to restore backup\n");
+			sd_err("failed to restore backup");
 			do_vdi_delete(vdiname, 0, NULL);
 			ret = EXIT_FAILURE;
 			break;
@@ -1862,9 +1855,8 @@ static int vdi_restore(int argc, char **argv)
 	bool need_current_recovery = false;
 
 	if (!vdi_cmd_data.snapshot_id && !vdi_cmd_data.snapshot_tag[0]) {
-		fprintf(stderr, "We can restore a backup file only to"
-			"snapshots\n");
-		fprintf(stderr, "Please specify the '-s' option\n");
+		sd_err("We can restore a backup file only to snapshots");
+		sd_err("Please specify the '-s' option");
 		ret = EXIT_USAGE;
 		goto out;
 	}
@@ -1886,14 +1878,14 @@ static int vdi_restore(int argc, char **argv)
 	}
 
 	if (is_stdin_console()) {
-		fprintf(stderr, "stdin must be pipe\n");
+		sd_err("stdin must be pipe");
 		ret = EXIT_USAGE;
 		goto out;
 	}
 
 	ret = do_vdi_delete(vdiname, 0, NULL);
 	if (ret != EXIT_SUCCESS) {
-		fprintf(stderr, "Failed to delete the current state\n");
+		sd_err("Failed to delete the current state");
 		goto out;
 	}
 	need_current_recovery = true;
@@ -1909,7 +1901,7 @@ out:
 					     current_inode->parent_vdi_id, NULL,
 					     true, current_inode->nr_copies);
 		if (recovery_ret != EXIT_SUCCESS) {
-			fprintf(stderr, "failed to resume the current vdi\n");
+			sd_err("failed to resume the current vdi");
 			ret = recovery_ret;
 		}
 	}
@@ -1927,7 +1919,7 @@ static int vdi_cache_flush(int argc, char **argv)
 	ret = find_vdi_name(vdiname, vdi_cmd_data.snapshot_id,
 			    vdi_cmd_data.snapshot_tag, &vid, 0);
 	if (ret < 0) {
-		fprintf(stderr, "Failed to open VDI %s\n", vdiname);
+		sd_err("Failed to open VDI %s", vdiname);
 		ret = EXIT_FAILURE;
 		goto out;
 	}
@@ -1937,7 +1929,7 @@ static int vdi_cache_flush(int argc, char **argv)
 
 	ret = send_light_req(&hdr, sdhost, sdport);
 	if (ret) {
-		fprintf(stderr, "failed to execute request\n");
+		sd_err("failed to execute request");
 		return EXIT_FAILURE;
 	}
 out:
@@ -1954,7 +1946,7 @@ static int vdi_cache_delete(int argc, char **argv)
 	ret = find_vdi_name(vdiname, vdi_cmd_data.snapshot_id,
 			    vdi_cmd_data.snapshot_tag, &vid, 0);
 	if (ret < 0) {
-		fprintf(stderr, "Failed to open VDI %s\n", vdiname);
+		sd_err("Failed to open VDI %s", vdiname);
 		ret = EXIT_FAILURE;
 		goto out;
 	}
@@ -1964,7 +1956,7 @@ static int vdi_cache_delete(int argc, char **argv)
 
 	ret = send_light_req(&hdr, sdhost, sdport);
 	if (ret) {
-		fprintf(stderr, "failed to execute request\n");
+		sd_err("failed to execute request");
 		return EXIT_FAILURE;
 	}
 out:
@@ -2002,8 +1994,8 @@ static int vdi_cache_info(int argc, char **argv)
 		return EXIT_SYSFAIL;
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		fprintf(stderr, "failed to get cache infomation: %s\n",
-			sd_strerror(rsp->result));
+		sd_err("failed to get cache infomation: %s",
+		       sd_strerror(rsp->result));
 		return EXIT_FAILURE;
 	}
 
@@ -2119,7 +2111,7 @@ static int vdi_parser(int ch, char *opt)
 	case 'i':
 		vdi_cmd_data.index = strtol(opt, &p, 10);
 		if (opt == p) {
-			fprintf(stderr, "The index must be an integer\n");
+			sd_err("The index must be an integer");
 			exit(EXIT_FAILURE);
 		}
 		break;
@@ -2147,8 +2139,8 @@ static int vdi_parser(int ch, char *opt)
 	case 'c':
 		nr_copies = strtol(opt, &p, 10);
 		if (opt == p || nr_copies < 0 || nr_copies > SD_MAX_COPIES) {
-			fprintf(stderr, "Invalid copies number, must be "
-				"an integer between 0 and %d\n", SD_MAX_COPIES);
+			sd_err("Invalid copies number, must be "
+			       "an integer between 0 and %d", SD_MAX_COPIES);
 			exit(EXIT_FAILURE);
 		}
 		vdi_cmd_data.nr_copies = nr_copies;
diff --git a/lib/logger.c b/lib/logger.c
index 3a600d4..97a5648 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -595,10 +595,10 @@ void early_log_init(const char *format_name, struct logger_user_info *user_info)
 		}
 	}
 
-	fprintf(stderr, "invalid log format: %s\n", format_name);
-	fprintf(stderr, "valid options are:\n");
+	sd_err("invalid log format: %s", format_name);
+	sd_err("valid options are:");
 	list_for_each_entry(f, &log_formats, list) {
-		fprintf(stderr, "\t%s\n", f->name);
+		sd_err("\t%s", f->name);
 	}
 
 	exit(1);
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 79551dd..a3f572e 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -140,8 +140,7 @@ static void usage(int status)
 			goto out;
 		}
 
-		fprintf(stderr, "Try '%s --help' for more information.\n",
-			program_name);
+		sd_err("Try '%s --help' for more information.", program_name);
 	} else {
 		struct sd_option *opt;
 
@@ -288,9 +287,8 @@ static void object_cache_size_set(char *s)
 	return;
 
 err:
-	fprintf(stderr, "Invalid object cache option '%s': "
-		"size must be an integer between 1 and %"PRIu32" inclusive\n",
-		s, max_cache_size);
+	sd_err("Invalid object cache option '%s': size must be an integer "
+	       "between 1 and %" PRIu32 " inclusive", s, max_cache_size);
 	exit(1);
 }
 
@@ -334,7 +332,7 @@ static void _object_cache_set(char *s)
 		}
 	}
 
-	fprintf(stderr, "invalid object cache arg: %s\n", s);
+	sd_err("invalid object cache arg: %s", s);
 	exit(1);
 }
 
@@ -346,7 +344,7 @@ static void object_cache_set(char *arg)
 	parse_arg(arg, ",", _object_cache_set);
 
 	if (sys->object_cache_size == 0) {
-		fprintf(stderr, "object cache size is not set\n");
+		sd_err("object cache size is not set");
 		exit(1);
 	}
 }
@@ -368,15 +366,15 @@ static void init_journal_arg(char *arg)
 		arg += szl;
 		jsize = strtoll(arg, NULL, 10);
 		if (jsize < MIN_JOURNAL_SIZE || jsize == LLONG_MAX) {
-			fprintf(stderr, "invalid size %s, "
-				"must be bigger than %u(M)\n", arg,
+			sd_err("invalid size %s, must be bigger than %u(M)",
+			       arg,
 				MIN_JOURNAL_SIZE);
 			exit(1);
 		}
 	} else if (!strncmp(sp, arg, spl)) {
 		jskip = true;
 	} else {
-		fprintf(stderr, "invalid paramters %s\n", arg);
+		sd_err("invalid paramters %s", arg);
 		exit(1);
 	}
 }
@@ -394,9 +392,8 @@ static void init_io_arg(char *arg)
 		arg += hl;
 		io_pt = arg;
 	} else {
-		fprintf(stderr, "invalid paramters %s. "
-			"Use '-i host=a.b.c.d,port=xxx'\n",
-			arg);
+		sd_err("invalid paramters %s. Use '-i host=a.b.c.d,port=xxx'",
+		       arg);
 		exit(1);
 	}
 }
@@ -599,8 +596,7 @@ int main(int argc, char **argv)
 			port = strtol(optarg, &p, 10);
 			if (optarg == p || port < 1 || UINT16_MAX < port
 				|| *p != '\0') {
-				fprintf(stderr, "Invalid port number '%s'\n",
-					optarg);
+				sd_err("Invalid port number '%s'", optarg);
 				exit(1);
 			}
 			break;
@@ -617,8 +613,7 @@ int main(int argc, char **argv)
 			log_level = strtol(optarg, &p, 10);
 			if (optarg == p || log_level < SDOG_EMERG ||
 			    SDOG_DEBUG < log_level || *p != '\0') {
-				fprintf(stderr, "Invalid log level '%s'\n",
-					optarg);
+				sd_err("Invalid log level '%s'", optarg);
 				sdlog_help();
 				exit(1);
 			}
@@ -628,8 +623,7 @@ int main(int argc, char **argv)
 			break;
 		case 'y':
 			if (!str_to_addr(optarg, sys->this_node.nid.addr)) {
-				fprintf(stderr, "Invalid address: '%s'\n",
-					optarg);
+				sd_err("Invalid address: '%s'", optarg);
 				exit(1);
 			}
 			explicit_addr = true;
@@ -652,9 +646,9 @@ int main(int argc, char **argv)
 			zone = strtol(optarg, &p, 10);
 			if (optarg == p || zone < 0 || UINT32_MAX < zone
 				|| *p != '\0') {
-				fprintf(stderr, "Invalid zone id '%s': "
-					"must be an integer between 0 and %u\n",
-					optarg, UINT32_MAX);
+				sd_err("Invalid zone id '%s': must be "
+				       "an integer between 0 and %u", optarg,
+				       UINT32_MAX);
 				exit(1);
 			}
 			sys->this_node.zone = zone;
@@ -665,7 +659,7 @@ int main(int argc, char **argv)
 		case 'c':
 			sys->cdrv = find_cdrv(optarg);
 			if (!sys->cdrv) {
-				fprintf(stderr, "Invalid cluster driver '%s'\n", optarg);
+				sd_err("Invalid cluster driver '%s'", optarg);
 				fprintf(stderr, "Supported drivers:");
 				FOR_EACH_CLUSTER_DRIVER(cdrv) {
 					fprintf(stderr, " %s", cdrv->name);
@@ -682,15 +676,13 @@ int main(int argc, char **argv)
 		case 'i':
 			parse_arg(optarg, ",", init_io_arg);
 			if (!str_to_addr(io_addr, sys->this_node.nid.io_addr)) {
-				fprintf(stderr, "Bad addr: '%s'\n",
-					io_addr);
+				sd_err("Bad addr: '%s'", io_addr);
 				exit(1);
 			}
 
 			if (io_pt)
 				if (sscanf(io_pt, "%u", &io_port) != 1) {
-					fprintf(stderr, "Bad port '%s'\n",
-						io_pt);
+					sd_err("Bad port '%s'", io_pt);
 					exit(1);
 				}
 			sys->this_node.nid.io_port = io_port;
@@ -699,8 +691,7 @@ int main(int argc, char **argv)
 			uatomic_set_true(&sys->use_journal);
 			parse_arg(optarg, ",", init_journal_arg);
 			if (!jsize) {
-				fprintf(stderr,
-					"you must specify size for journal\n");
+				sd_err("you must specify size for journal");
 				exit(1);
 			}
 			break;
@@ -749,7 +740,7 @@ int main(int argc, char **argv)
 
 	dir = realpath(dirp, NULL);
 	if (!dir) {
-		fprintf(stderr, "%m\n");
+		sd_err("%m");
 		exit(1);
 	}
 
@@ -852,12 +843,12 @@ int main(int argc, char **argv)
 		exit(1);
 
 	if (pid_file && (create_pidfile(pid_file) != 0)) {
-		fprintf(stderr, "failed to pid file '%s' - %m\n", pid_file);
+		sd_err("failed to pid file '%s' - %m", pid_file);
 		exit(1);
 	}
 
 	if (chdir(dir) < 0) {
-		fprintf(stderr, "failed to chdir to %s: %m\n", dir);
+		sd_err("failed to chdir to %s: %m", dir);
 		exit(1);
 	}
 
diff --git a/sheep/store.c b/sheep/store.c
index 8b5eca0..f4343b3 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -169,7 +169,7 @@ out:
 int init_base_path(const char *d)
 {
 	if (xmkdir(d, sd_def_dmode) < 0) {
-		fprintf(stderr, "cannot create the directory %s (%m)\n", d);
+		sd_err("cannot create the directory %s (%m)", d);
 		return -1;
 	}
 
diff --git a/shepherd/shepherd.c b/shepherd/shepherd.c
index 2acbffd..27ab4a8 100644
--- a/shepherd/shepherd.c
+++ b/shepherd/shepherd.c
@@ -704,12 +704,12 @@ int main(int argc, char **argv)
 		case 'p':
 			port = strtol(optarg, &p, 10);
 			if (p == optarg) {
-				fprintf(stderr, "invalid port: %s\n", optarg);
+				sd_err("invalid port: %s", optarg);
 				exit(1);
 			}
 			break;
 		default:
-			fprintf(stderr, "unknown option: %c\n", ch);
+			sd_err("unknown option: %c", ch);
 			usage();
 			exit(1);
 			break;
@@ -720,7 +720,7 @@ int main(int argc, char **argv)
 		ret = daemon(0, 0);
 
 		if (-1 == ret) {
-			fprintf(stderr, "daemon() failed: %m\n");
+			sd_err("daemon() failed: %m");
 			exit(1);
 		}
 	}
-- 
1.7.9.5




More information about the sheepdog mailing list