diff options
author | shishir gowda <shishirng@gluster.com> | 2010-08-31 04:10:17 +0000 |
---|---|---|
committer | Vijay Bellur <vijay@dev.gluster.com> | 2010-08-31 07:43:57 -0700 |
commit | 4ae3853d857e584189289b33f8ba929fbd9cc7e1 (patch) | |
tree | 4527a1a6ea019bc6a5198da01723c6f7c660203a | |
parent | cd5d2fc036003eb0c1a7f91755d3f5aa327067b4 (diff) |
Add command logging facility for glusterd
Added new command logging facility to gf_cmd_log() which can be used
to log all commands to a .cmd_log_history file situated in the
glusterd working directory.
Accepts 1 st argument a domain string (e.g: volume, peer..) followed
by msg string (similar to gf_log)
Signed-off-by: shishir gowda <shishirng@gluster.com>
Signed-off-by: Vijay Bellur <vijay@dev.gluster.com>
BUG: 1404 (need a dump of all the op/mgmt commands)
URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1404
-rw-r--r-- | libglusterfs/src/logging.c | 99 | ||||
-rw-r--r-- | libglusterfs/src/logging.h | 5 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd.c | 11 |
3 files changed, 114 insertions, 1 deletions
diff --git a/libglusterfs/src/logging.c b/libglusterfs/src/logging.c index 1998fdc3ea4..c152e4a28e4 100644 --- a/libglusterfs/src/logging.c +++ b/libglusterfs/src/logging.c @@ -51,6 +51,8 @@ static int gf_log_syslog = 0; gf_loglevel_t gf_log_loglevel; /* extern'd */ FILE *gf_log_logfile; +static char *cmd_log_filename = NULL; +static FILE *cmdlogfile = NULL; void gf_log_logrotate (int signum) @@ -412,3 +414,100 @@ gf_log_from_client (const char *msg, char *identifier) return 0; } + +int +gf_cmd_log_init (const char *filename) +{ + if (!filename){ + gf_log ("glusterd",GF_LOG_CRITICAL, "gf_cmd_log_init: no " + "filename specified\n"); + return -1; + } + + cmd_log_filename = gf_strdup (filename); + if (!filename) { + gf_log ("glusterd",GF_LOG_CRITICAL, "gf_cmd_log_init: strdup" + " error\n"); + return -1; + } + + cmdlogfile = fopen (cmd_log_filename, "a"); + if (!cmdlogfile){ + gf_log ("glusterd", GF_LOG_CRITICAL, + "gf_cmd_log_init: failed to open logfile \"%s\" " + "(%s)\n", cmd_log_filename, strerror (errno)); + return -1; + } + return 0; +} + +int +gf_cmd_log (const char *domain, const char *fmt, ...) +{ + va_list ap; + struct tm *tm = NULL; + char timestr[256]; + struct timeval tv = {0,}; + char *str1 = NULL; + char *str2 = NULL; + char *msg = NULL; + size_t len = 0; + int ret = 0; + + if (!cmdlogfile) + return -1; + + + if (!domain || !fmt) { + gf_log ("glusterd", GF_LOG_TRACE, + "logging: invalid argument\n"); + return -1; + } + + ret = gettimeofday (&tv, NULL); + if (ret == -1) + goto out; + + tm = localtime (&tv.tv_sec); + + va_start (ap, fmt); + strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm); + snprintf (timestr + strlen (timestr), 256 - strlen (timestr), + ".%"GF_PRI_SUSECONDS, tv.tv_usec); + + ret = gf_asprintf (&str1, "[%s] %s : ", + timestr, domain); + if (ret == -1) { + goto out; + } + + ret = vasprintf (&str2, fmt, ap); + if (ret == -1) { + goto out; + } + + va_end (ap); + + len = strlen (str1); + msg = GF_MALLOC (len + strlen (str2) + 1, gf_common_mt_char); + + strcpy (msg, str1); + strcpy (msg + len, str2); + + fprintf (cmdlogfile, "%s\n", msg); + fflush (cmdlogfile); + +out: + if (msg) { + GF_FREE (msg); + } + + if (str1) + GF_FREE (str1); + + if (str2) + FREE (str2); + + return (0); + +} diff --git a/libglusterfs/src/logging.h b/libglusterfs/src/logging.h index f727bfe421d..39b5938502f 100644 --- a/libglusterfs/src/logging.h +++ b/libglusterfs/src/logging.h @@ -120,4 +120,9 @@ void gf_log_set_xl_loglevel (void *xl, gf_loglevel_t level); #define GF_ERROR(xl, format, args...) \ gf_log ((xl)->name, GF_LOG_ERROR, format, ##args) +int +gf_cmd_log (const char *domain, const char *fmt, ...); + +int +gf_cmd_log_init (const char *filename); #endif /* __LOGGING_H__ */ diff --git a/xlators/mgmt/glusterd/src/glusterd.c b/xlators/mgmt/glusterd/src/glusterd.c index d23f06190d7..a038bc69817 100644 --- a/xlators/mgmt/glusterd/src/glusterd.c +++ b/xlators/mgmt/glusterd/src/glusterd.c @@ -232,7 +232,7 @@ init (xlator_t *this) int port_num = 0; char voldir [PATH_MAX] = {0,}; char dirname [PATH_MAX]; - + char cmd_log_filename [PATH_MAX] = {0,}; dir_data = dict_get (this->options, "working-directory"); @@ -273,6 +273,15 @@ init (xlator_t *this) gf_log (this->name, GF_LOG_NORMAL, "Using %s as working directory", dirname); + snprintf (cmd_log_filename, PATH_MAX,"%s/.cmd_log_history",dirname); + ret = gf_cmd_log_init (cmd_log_filename); + + if (ret == -1) { + gf_log ("this->name", GF_LOG_CRITICAL, + "Unable to create cmd log file %s", cmd_log_filename); + exit (1); + } + snprintf (voldir, PATH_MAX, "%s/vols", dirname); ret = mkdir (voldir, 0777); |