From: Liu Yuan <tailai.ly at taobao.com> Now with -f option, we can redirect printf output to stderr and also save us from manual adding function name and function line to printf. Signed-off-by: Liu Yuan <tailai.ly at taobao.com> --- sheepfs/cluster.c | 1 - sheepfs/config.c | 1 - sheepfs/core.c | 38 ++++++++++++++++++++++++++++++++------ sheepfs/node.c | 1 - sheepfs/shadow_file.c | 19 +++++++++---------- sheepfs/sheepfs.h | 10 ++++++++++ sheepfs/vdi.c | 1 - sheepfs/volume.c | 33 ++++++++++++++------------------- 8 files changed, 65 insertions(+), 39 deletions(-) diff --git a/sheepfs/cluster.c b/sheepfs/cluster.c index 39415f8..eeb8bed 100644 --- a/sheepfs/cluster.c +++ b/sheepfs/cluster.c @@ -18,7 +18,6 @@ #include <stdlib.h> #include <stdio.h> #include <time.h> -#include <syslog.h> #include "strbuf.h" #include "sheepfs.h" diff --git a/sheepfs/config.c b/sheepfs/config.c index 09dae34..27d8b4e 100644 --- a/sheepfs/config.c +++ b/sheepfs/config.c @@ -18,7 +18,6 @@ #include <stdlib.h> #include <stdio.h> #include <time.h> -#include <syslog.h> #include "strbuf.h" #include "sheepfs.h" diff --git a/sheepfs/core.c b/sheepfs/core.c index 873c318..3b0de45 100644 --- a/sheepfs/core.c +++ b/sheepfs/core.c @@ -20,6 +20,7 @@ #include <getopt.h> #include <syslog.h> #include <stdlib.h> +#include <stdarg.h> #include "strbuf.h" #include "util.h" @@ -77,6 +78,30 @@ static struct sheepfs_file_operation { volume_sync, volume_open }, }; +__attribute__ ((format (__printf__, 3, 4))) +static void fg_printf(const char *func, int line, const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "%s(%d): ", func, line); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + +__attribute__ ((format (__printf__, 3, 4))) +static void bg_printf(const char *func, int line, const char *fmt, ...) +{ + va_list ap; + + syslog(LOG_ERR, "%s(%d)", func, line); + va_start(ap, fmt); + vsyslog(LOG_ERR, fmt, ap); + va_end(ap); +} + +printf_fn fs_printf = bg_printf; + int sheepfs_set_op(const char *path, unsigned opcode) { if (shadow_file_setxattr(path, SH_OP_NAME, &opcode, SH_OP_SIZE) < 0) { @@ -136,13 +161,13 @@ static int sheepfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, dir = opendir(p.buf); if (!dir) { ret = -errno; - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); goto out; } while ((dentry = readdir(dir))) { if (filler(buf, dentry->d_name, NULL, 0) != 0) { - syslog(LOG_ERR, "[%s] out of memory\n", __func__); + sheepfs_pr("out of memory\n"); ret = -ENOMEM; goto out; } @@ -241,10 +266,10 @@ static int sheepfs_main_loop(char *mountpoint) if (sheepfs_fg) fuse_opt_add_arg(&args, "-f"); - syslog(LOG_INFO, "sheepfs daemon started\n"); + sheepfs_pr("sheepfs daemon started\n"); ret = fuse_main(args.argc, args.argv, &sheepfs_ops, NULL); rmdir_r(sheepfs_shadow); - syslog(LOG_INFO, "sheepfs daemon exited %d\n", ret); + sheepfs_pr("sheepfs daemon exited %d\n", ret); return ret; } @@ -304,6 +329,7 @@ int main(int argc, char **argv) break; case 'f': sheepfs_fg = 1; + fs_printf = fg_printf; break; case 'k': sheepfs_page_cache = 1; @@ -315,7 +341,7 @@ int main(int argc, char **argv) sdport = strtol(optarg, NULL, 10); if (sdport < 1 || sdport > UINT16_MAX) { fprintf(stderr, - "Invalid port number '%s'\n", optarg); + "Invalid port number '%s'\n", optarg); exit(1); } break; @@ -359,7 +385,7 @@ struct strbuf *sheepfs_run_cmd(const char *command) FILE *f = popen(command, "re"); if (!f) { - syslog(LOG_ERR, "[%s] popen failed\n", __func__); + sheepfs_pr("popen failed\n"); goto err; } diff --git a/sheepfs/node.c b/sheepfs/node.c index 38472e7..d3ef21b 100644 --- a/sheepfs/node.c +++ b/sheepfs/node.c @@ -18,7 +18,6 @@ #include <stdlib.h> #include <stdio.h> #include <time.h> -#include <syslog.h> #include "strbuf.h" #include "sheepfs.h" diff --git a/sheepfs/shadow_file.c b/sheepfs/shadow_file.c index e7b4878..4037e65 100644 --- a/sheepfs/shadow_file.c +++ b/sheepfs/shadow_file.c @@ -22,7 +22,6 @@ #include <errno.h> #include <stdio.h> #include <sys/xattr.h> -#include <syslog.h> #include <stdlib.h> #include "util.h" @@ -36,7 +35,7 @@ int shadow_file_read(const char *path, char *buf, size_t size, off_t offset) sprintf(p, "%s%s", sheepfs_shadow, path); fd = open(p, O_RDONLY); if (fd < 0) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); return -errno; } len = xpread(fd, buf, size, offset); @@ -53,12 +52,12 @@ size_t shadow_file_write(const char *path, char *buf, size_t size) sprintf(p, "%s%s", sheepfs_shadow, path); fd = open(p, O_WRONLY | O_TRUNC); if (fd < 0) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); return 0; } len = xwrite(fd, buf, size); if (len != size) { - syslog(LOG_ERR, "[%s] failed to write\n", __func__); + sheepfs_pr("failed to write\n"); len = 0; } close(fd); @@ -73,7 +72,7 @@ int shadow_file_create(const char *path) fd = creat(p, 0644); if (fd < 0) { if (errno != EEXIST) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); return -1; } } @@ -88,7 +87,7 @@ int shadow_dir_create(const char *path) sprintf(p, "%s%s", sheepfs_shadow, path); if (mkdir(p, 0755) < 0) { if (errno != EEXIST) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); return -1; } } @@ -102,7 +101,7 @@ int shadow_file_setxattr(const char *path, const char *name, sprintf(p, "%s%s", sheepfs_shadow, path); if (setxattr(p, name, value, size, 0) < 0) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); return -1; } return 0; @@ -115,7 +114,7 @@ int shadow_file_getxattr(const char *path, const char *name, sprintf(p, "%s%s", sheepfs_shadow, path); if (getxattr(p, name, value, size) < 0) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); return -1; } return 0; @@ -128,7 +127,7 @@ int shadow_file_delete(const char *path) sprintf(p, "%s%s", sheepfs_shadow, path); if (unlink(p) < 0) { if (errno != ENOENT) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); return -1; } } @@ -142,7 +141,7 @@ int shadow_file_exsit(const char *path) sprintf(p, "%s%s", sheepfs_shadow, path); if (access(p, R_OK | W_OK) < 0) { if (errno != ENOENT) - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); return 0; } diff --git a/sheepfs/sheepfs.h b/sheepfs/sheepfs.h index af64f56..da59a92 100644 --- a/sheepfs/sheepfs.h +++ b/sheepfs/sheepfs.h @@ -28,6 +28,16 @@ extern int sdport; extern struct strbuf *sheepfs_run_cmd(const char *command); extern int sheepfs_set_op(const char *path, unsigned opcode); +typedef void (*printf_fn)(const char *func, int line, const char *, ...) +__attribute__ ((format (__printf__, 3, 4))); + +printf_fn fs_printf; + +#define sheepfs_pr(fmt, args...) \ +({ \ + fs_printf(__func__, __LINE__, fmt, ##args); \ +}) + /* shadow_file.c */ extern size_t shadow_file_write(const char *path, char *buf, size_t size); extern int shadow_file_read(const char *, char *buf, size_t size, off_t); diff --git a/sheepfs/vdi.c b/sheepfs/vdi.c index 980dfaa..b15bce4 100644 --- a/sheepfs/vdi.c +++ b/sheepfs/vdi.c @@ -18,7 +18,6 @@ #include <stdlib.h> #include <stdio.h> #include <time.h> -#include <syslog.h> #include "strbuf.h" #include "sheepfs.h" diff --git a/sheepfs/volume.c b/sheepfs/volume.c index 8daea98..43db09d 100644 --- a/sheepfs/volume.c +++ b/sheepfs/volume.c @@ -19,7 +19,6 @@ #include <stdio.h> #include <time.h> #include <assert.h> -#include <syslog.h> #include <urcu/uatomic.h> #include <pthread.h> @@ -153,7 +152,7 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t size, if (is_data_obj(oid)) { if (off % SECTOR_SIZE || size % SECTOR_SIZE) { - syslog(LOG_ERR, "offset or size not aligned\n"); + sheepfs_pr("offset or size not aligned\n"); return -1; } @@ -204,7 +203,7 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t size, put_socket_fd(vdi, sock_idx); if (ret || rsp->result != SD_RES_SUCCESS) { - syslog(LOG_ERR, + sheepfs_pr( "[%s] failed to %s object %" PRIx64 " ret %d, res %u\n", __func__, rw == VOLUME_READ ? "read" : "write", oid, ret, rsp->result); @@ -247,10 +246,10 @@ static int volume_do_rw(const char *path, char *buf, size_t size, do { #ifdef DEBUG - syslog(LOG_INFO, "%s oid %"PRIx64", off %ju, len %zu," - " size %zu\n", - rw == VOLUME_READ ? "read" : "write", - oid, start, len, size); + sheepfs_pr("%s oid %"PRIx64", off %ju, len %zu," + " size %zu\n", + rw == VOLUME_READ ? "read" : "write", + oid, start, len, size); #endif ret = volume_rw_object(buf, oid, len, start, rw); @@ -312,8 +311,7 @@ static int volume_do_sync(uint32_t vid) put_socket_fd(vdi, idx); if (ret || rsp->result != SD_RES_SUCCESS) { - syslog(LOG_ERR, "[%s] failed to flush vdi %"PRIx32"\n", - __func__, vid); + sheepfs_pr("failed to flush vdi %"PRIx32"\n", vid); return -1; } @@ -354,14 +352,14 @@ static int setup_socket_pool(int array[], int len) for (i = 0; i < len; i++) { fd = connect_to(sdhost, sdport); if (fd < 0) { - syslog(LOG_ERR, "[%s] connect_to %m\n", __func__); + sheepfs_pr("connect_to %m\n"); destroy_socket_pool(array, --i); return -1; } ret = set_nodelay(fd); if (ret) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); destroy_socket_pool(array, i); return -1; } @@ -407,21 +405,20 @@ static int init_vdi_info(const char *entry, uint32_t *vid, size_t *size) return -1; if (sscanf(buf->buf, "%*s %*s %*d %zu %*s %*s %*s %"PRIx32, size, vid) < 2) { - syslog(LOG_ERR, "[%s] failed to sscanf %s\n", __func__, entry); + sheepfs_pr("failed to sscanf %s\n", entry); goto err; } inode_buf = malloc(SD_INODE_SIZE); if (!inode_buf) { - syslog(LOG_ERR, "[%s] %m\n", __func__); + sheepfs_pr("%m\n"); goto err; } inode = xzalloc(sizeof(*inode)); inode->vid = *vid; if (setup_socket_pool(inode->socket_pool, SOCKET_POOL_SIZE) < 0) { - syslog(LOG_ERR, "[%s] failed to setup socket pool\n", - __func__); + sheepfs_pr("failed to setup socket pool\n"); goto err; } /* we need insert inode before calling volume_rw_object */ @@ -433,8 +430,7 @@ static int init_vdi_info(const char *entry, uint32_t *vid, size_t *size) if (volume_rw_object(inode_buf, vid_to_vdi_oid(*vid), SD_INODE_SIZE, 0, VOLUME_READ) < 0) { rb_erase(&inode->rb, &vdi_inode_tree); - syslog(LOG_ERR, "[%s] failed to read inode for %"PRIx32"\n", - __func__, *vid); + sheepfs_pr("failed to read inode for %"PRIx32"\n", *vid); goto err; } inode->inode = inode_buf; @@ -503,8 +499,7 @@ static int volume_sync_and_delete(uint32_t vid) put_socket_fd(vdi, idx); if (ret || rsp->result != SD_RES_SUCCESS) { - syslog(LOG_ERR, "[%s] failed to flush vdi %" PRIx32 "\n", - __func__, vid); + sheepfs_pr("failed to flush vdi %" PRIx32 "\n", vid); return -1; } -- 1.7.10.2 |