From 4ae3853d857e584189289b33f8ba929fbd9cc7e1 Mon Sep 17 00:00:00 2001 From: shishir gowda Date: Tue, 31 Aug 2010 04:10:17 +0000 Subject: 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 Signed-off-by: Vijay Bellur BUG: 1404 (need a dump of all the op/mgmt commands) URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1404 --- libglusterfs/src/logging.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'libglusterfs/src/logging.c') 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); + +} -- cgit