diff options
author | Ryo Nakamura <upa@haeena.net> | 2023-03-03 22:14:54 +0900 |
---|---|---|
committer | Ryo Nakamura <upa@haeena.net> | 2023-03-03 22:14:54 +0900 |
commit | 1e57e8fb2fcbf64ad518a5f6aa763725cbb43244 (patch) | |
tree | b3924a58a4e01c8435759a221d61f463ea069020 /src | |
parent | 1b9ae5197463eed7d7015e6749bec1372844baea (diff) |
implementing messaging.
ToDo: remove pprint.
mscp should use mpr_* functions, and main.c should use
just fprintf(stdout, "\r\033[K" fmt, ...) for printing progress bar.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 5 | ||||
-rw-r--r-- | src/message.c | 33 | ||||
-rw-r--r-- | src/message.h | 29 | ||||
-rw-r--r-- | src/mscp.c | 30 | ||||
-rw-r--r-- | src/mscp.h | 42 |
5 files changed, 105 insertions, 34 deletions
@@ -184,6 +184,7 @@ int main(int argc, char **argv) memset(&s, 0, sizeof(s)); memset(&o, 0, sizeof(o)); + o.severity = MSCP_SEVERITY_WARN; while ((ch = getopt(argc, argv, "n:m:s:S:a:b:vqDrl:p:i:c:M:C:HdNh")) != -1) { switch (ch) { @@ -211,10 +212,10 @@ int main(int argc, char **argv) o.buf_sz = atoi(optarg); break; case 'v': - o.verbose_level++; + o.severity++; break; case 'q': - o.verbose_level = -1; + o.severity = MSCP_SEVERITY_NONE; break; case 'D': o.dryrun = true; diff --git a/src/message.c b/src/message.c index 2c69c3f..29f9970 100644 --- a/src/message.c +++ b/src/message.c @@ -2,9 +2,12 @@ #include <stdarg.h> #include <string.h> #include <limits.h> +#include <pthread.h> #include <message.h> +/* mscp error message buffer */ + #define MSCP_ERRMSG_SIZE (PATH_MAX * 2) static char errmsg[MSCP_ERRMSG_SIZE]; @@ -23,3 +26,33 @@ const char *mscp_get_error() { return errmsg; } + + +/* message print functions */ + +static int mprint_serverity = MSCP_SEVERITY_WARN; +static pthread_mutex_t mprint_lock = PTHREAD_MUTEX_INITIALIZER; + +void mprint_set_severity(int serverity) +{ + if (serverity < 0) + mprint_serverity = -1; /* no print */ + mprint_serverity = serverity; +} + +void mprint(int fd, int serverity, const char *fmt, ...) +{ + va_list va; + int ret; + + if (fd < 0) + return; + + if (serverity <= mprint_serverity) { + pthread_mutex_lock(&mprint_lock); + va_start(va, fmt); + vdprintf(fd, fmt, va); + va_end(va); + pthread_mutex_unlock(&mprint_lock); + } +} diff --git a/src/message.h b/src/message.h index b5dd6dc..bafbf5c 100644 --- a/src/message.h +++ b/src/message.h @@ -3,27 +3,26 @@ #include <libgen.h> -enum { - MSCP_SEVERITY_ERR = 0, - MSCP_SEVERITY_WARN, - MSCP_SEVERITY_NOTICE, - MSCP_SEVERITY_INFO, - MSCP_SEVERITY_DEBUG, -}; +#include <mscp.h> /* message print. printed messages are passed to application via msg_fd */ -//void mprint_set_severity(int severity); -//void mprint(int severity, const char *fmt, ...); +void mprint_set_severity(int severity); +void mprint(int fd, int severity, const char *fmt, ...); -#define mpr_err(fmt, ...) mprint(MSCP_SEVERITY_ERR, fmt, ##__VA_ARGS__) -#define mpr_warn(fmt, ...) mprint(MSCP_SEVERITY_WARN, fmt, ##__VA_ARGS__) -#define mpr_notice(fmt, ...) mprint(MSCP_SEVERITY_NOTICE, fmt, ##__VA_ARGS__) -#define mpr_info(fmt, ...) mprint(MSCP_SEVERITY_INFO, fmt, ##__VA_ARGS__) -#define mpr_debug(fmt, ...) mprint(MSCP_SEVERITY_DEBUG, fmt, ##__VA_ARGS__) +#define mpr_err(m, fmt, ...) \ + mprint(m->msg_fd, MSCP_SEVERITY_ERR, fmt, ##__VA_ARGS__) +#define mpr_warn(m, fmt, ...) \ + mprint(m->msg_fd, MSCP_SEVERITY_WARN, fmt, ##__VA_ARGS__) +#define mpr_notice(m, fmt, ...) \ + mprint(m->msg_fd, MSCP_SEVERITY_NOTICE, fmt, ##__VA_ARGS__) +#define mpr_info(m, fmt, ...) \ + mprint(m->msg_fd, MSCP_SEVERITY_INFO, fmt, ##__VA_ARGS__) +#define mpr_debug(m, fmt, ...) \ + mprint(m->msg_fd, MSCP_SEVERITY_DEBUG, fmt, ##__VA_ARGS__) /* error message buffer */ -#define mscp_set_error(fmt, ...) \ +#define mscp_set_error(fmt, ...) \ _mscp_set_error("%s:%d:%s: " fmt, \ basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) @@ -19,6 +19,8 @@ struct mscp { struct mscp_opts *opts; struct mscp_ssh_opts *ssh_opts; + int msg_fd; /* writer fd for message pipe */ + int *cores; /* usable cpu cores by COREMASK */ int nr_cores; /* length of array of cores */ @@ -197,6 +199,9 @@ struct mscp *mscp_init(const char *remote_host, return NULL; } + m->msg_fd = o->msg_fd; + mprint_set_severity(o->severity); + if (validate_and_set_defaut_params(o) < 0) goto free_out; @@ -214,17 +219,15 @@ struct mscp *mscp_init(const char *remote_host, if (strlen(o->coremask) > 0) { if (expand_coremask(o->coremask, &m->cores, &m->nr_cores) < 0) goto free_out; - pprint(1, "usable cpu cores:"); + mpr_notice(m, "usable cpu cores:"); for (n = 0; n < m->nr_cores; n++) - pprint(2, " %d", m->cores[n]); - pprint(1, "\n"); + mpr_notice(m, "%d", m->cores[n]); + mpr_notice(m, "\n"); } m->opts = o; m->ssh_opts = s; - pprint_set_level(o->verbose_level); - return m; free_out: @@ -232,6 +235,16 @@ free_out: return NULL; } +void mscp_set_msg_fd(struct mscp *m, int fd) +{ + m->msg_fd = fd; +} + +int mscp_get_msg_fd(struct mscp *m) +{ + return m->msg_fd; +} + int mscp_connect(struct mscp *m) { m->first = ssh_init_sftp_session(m->remote, m->ssh_opts); @@ -367,8 +380,8 @@ int mscp_start(struct mscp *m) int n, ret; if ((n = list_count(&m->chunk_list)) < m->opts->nr_threads) { - pprint1("we have only %d chunk(s). " - "set number of connections to %d\n", n, n); + mpr_notice(m, "we have only %d chunk(s). " + "set number of connections to %d\n", n, n); m->opts->nr_threads = n; } @@ -388,7 +401,8 @@ int mscp_start(struct mscp *m) m->first = NULL; } else { - pprint2("connecting to %s for a copy thread...\n", m->remote); + mpr_notice(m, "connecting to %s for a copy thread...\n", + m->remote); t->sftp = ssh_init_sftp_session(m->remote, m->ssh_opts); if (!t->sftp) return -1; @@ -20,10 +20,12 @@ struct mscp_opts { size_t buf_sz; char coremask[MSCP_MAX_COREMASK_STR]; - int verbose_level; - bool quiet; - bool dryrun; + /* messaging */ + int severity; /* messaging severity. set MSCP_SERVERITY_* */ + int msg_fd; /* fd to output message. default STDOUT, + * and -1 disables output */ + bool dryrun; }; #define MSCP_SSH_MAX_LOGIN_NAME 64 @@ -63,12 +65,6 @@ struct mscp; struct mscp *mscp_init(const char *remote_host, struct mscp_opts *o, struct mscp_ssh_opts *s); -/* return a fd for read message from mscp */ -int mscp_msg_fd(struct mscp *m); - -/* get message for the most recent error (not thread safe) */ -const char *mscp_get_error(); - /* establish the first SFTP session. mscp_prepare() and mscp_start() * requires mscp_connect() beforehand */ int mscp_connect(struct mscp *m); @@ -99,4 +95,32 @@ void mscp_cleanup(struct mscp *m); /* free mscp instance */ void mscp_free(struct mscp *m); + +/* messaging with mscp */ + +/* severity filter for messages. specifiy it with mscp_opts->serverity. + */ +enum { + MSCP_SEVERITY_NONE = -1, + MSCP_SEVERITY_ERR = 0, + MSCP_SEVERITY_WARN = 1, + MSCP_SEVERITY_NOTICE = 2, + MSCP_SEVERITY_INFO = 3, + MSCP_SEVERITY_DEBUG = 4, +}; + +/* set fd to which mscp writes messages. default is STDOUT. + * supposed fd is pipe write fd. + */ +void mscp_set_msg_fd(struct mscp *m, int fd); + +/* retrieve the fd for read message from mscp */ +int mscp_get_msg_fd(struct mscp *m); + +/* get message for the most recent error (not thread safe) */ +const char *mscp_get_error(); + + + + #endif /* _MSCP_H_ */ |