From b535c44fdd2b71c50a8fcbcf1e1e1c2d1c0340e9 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Thu, 17 Aug 2017 11:21:11 +0530 Subject: logger: support logdir choosing via Environment variable Currently the default logdir is DATADIR /log/gluster-block/ This patch will provide a way to change this default logdir via Env variable $ export GB_LOGDIR=/var/log/gluster-block-new-path/ Note: make sure to restart the processes (cli & daemon) after you set GB_LOGDIR Change-Id: Id142e4a4dfe7b6ebc9cf8296b8ceb8bff37691b8 Signed-off-by: Prasanna Kumar Kalever --- utils/common.h | 22 -------------------- utils/utils.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- utils/utils.h | 46 +++++++++++++++++++++++++++++++++++------- 3 files changed, 101 insertions(+), 30 deletions(-) (limited to 'utils') diff --git a/utils/common.h b/utils/common.h index 6503218..2d1202a 100644 --- a/utils/common.h +++ b/utils/common.h @@ -15,28 +15,6 @@ # include "utils.h" # include "block.h" -# define GB_LOGDIR DATADIR "/log/gluster-block" -# define GB_INFODIR DATADIR "/run" - -# define GB_LOCK_FILE GB_INFODIR "/gluster-blockd.lock" -# define GB_UNIX_ADDRESS GB_INFODIR "/gluster-blockd.socket" -# define GB_TCP_PORT 24006 - -# define DAEMON_LOG_FILE GB_LOGDIR "/gluster-blockd.log" -# define CLI_LOG_FILE GB_LOGDIR "/gluster-block-cli.log" -#define DEVNULLPATH "/dev/null" - -# define GFAPI_LOG_FILE GB_LOGDIR "/gluster-block-gfapi.log" -# define GFAPI_LOG_LEVEL 7 - -# define CONFIGSHELL_LOG_FILE GB_LOGDIR "/gluster-block-configshell.log" - -# define GB_METADIR "/block-meta" -# define GB_STOREDIR "/block-store" -# define GB_TXLOCKFILE "meta.lock" - -# define SUN_PATH_MAX (sizeof(struct sockaddr_un) - sizeof(unsigned short int)) /*sun_family*/ - static const char *const JsonResponseFormatLookup[] = { [GB_JSON_NONE] = "", diff --git a/utils/utils.c b/utils/utils.c index d66db8d..3c2b9ad 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -9,11 +9,14 @@ */ +# include +# include + # include "utils.h" # include "config.h" -size_t logLevel = GB_LOG_INFO; +struct gbConf gbConf = {GB_LOG_INFO, GB_LOGDIR, '\0', '\0', '\0', '\0'}; const char *argp_program_version = "" \ PACKAGE_NAME" ("PACKAGE_VERSION")" \ @@ -172,6 +175,64 @@ out: } +static bool +glusterBlockLogdirCreate(void) +{ + DIR* dir = opendir(gbConf.logDir); + + + if (dir) { + closedir(dir); + } else if (errno == ENOENT) { + if (mkdir(gbConf.logDir, 0755) == -1) { + fprintf(stderr, "mkdir(%s) failed (%s)", gbConf.logDir, strerror (errno)); + return 0; /* False */ + } + } else { + fprintf(stderr, "opendir(%s) failed (%s)", gbConf.logDir, strerror (errno)); + return 0; /* False */ + } + + return 1; +} + + +int +initLogging(void) +{ + char *logDir = NULL; + + + logDir = getenv("GB_LOGDIR"); + if (!logDir) { + logDir = GB_LOGDIR; + } + + if (strlen(logDir) > PATH_MAX - GB_MAX_LOGFILENAME) { + fprintf(stderr, "strlen of logDir Path > PATH_MAX: %s\n", logDir); + return EXIT_FAILURE; + } + + /* set logfile paths */ + snprintf(gbConf.logDir, PATH_MAX, + "%s", logDir); + snprintf(gbConf.daemonLogFile, PATH_MAX, + "%s/gluster-blockd.log", logDir); + snprintf(gbConf.cliLogFile, PATH_MAX, + "%s/gluster-block-cli.log", logDir); + snprintf(gbConf.gfapiLogFile, PATH_MAX, + "%s/gluster-block-gfapi.log", logDir); + snprintf(gbConf.configShellLogFile, PATH_MAX, + "%s/gluster-block-configshell.log", logDir); + + if(!glusterBlockLogdirCreate()) { + return EXIT_FAILURE; + } + + return 0; +} + + int gbAlloc(void *ptrptr, size_t size, const char *filename, const char *funcname, size_t linenr) diff --git a/utils/utils.h b/utils/utils.h index 1d82963..8e951fd 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -22,8 +22,28 @@ # include # include # include +# include # include +# define GB_LOGDIR DATADIR "/log/gluster-block" +# define GB_INFODIR DATADIR "/run" + +# define GB_LOCK_FILE GB_INFODIR "/gluster-blockd.lock" +# define GB_UNIX_ADDRESS GB_INFODIR "/gluster-blockd.socket" +# define GB_TCP_PORT 24006 + +# define GFAPI_LOG_LEVEL 7 + +# define DEVNULLPATH "/dev/null" + +# define GB_METADIR "/block-meta" +# define GB_STOREDIR "/block-store" +# define GB_TXLOCKFILE "meta.lock" + +# define GB_MAX_LOGFILENAME 64 /* max strlen of file name */ + +# define SUN_PATH_MAX (sizeof(struct sockaddr_un) - sizeof(unsigned short int)) /*sun_family*/ + # define GB_TIME_STRING_BUFLEN \ (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 6 + 1 + 5) /* Yr Mon Day Hour Min Sec Ms NULL Round-off(32) @@ -83,19 +103,29 @@ fprintf(stdout, fmt, __VA_ARGS__); \ } while (0) -extern size_t logLevel; + +struct gbConf { + int logLevel; + char logDir[PATH_MAX]; + char daemonLogFile[PATH_MAX]; + char cliLogFile[PATH_MAX]; + char gfapiLogFile[PATH_MAX]; + char configShellLogFile[PATH_MAX]; +}; + +extern struct gbConf gbConf; # define LOG(str, level, fmt, ...) \ do { \ FILE *fd; \ char timestamp[GB_TIME_STRING_BUFLEN] = {0}; \ - if (level <= logLevel) { \ + if (level <= gbConf.logLevel) { \ if (!strcmp(str, "mgmt")) \ - fd = fopen (DAEMON_LOG_FILE, "a"); \ - else if (strcmp(str, "cli")) \ - fd = fopen (CLI_LOG_FILE, "a"); \ - else if (strcmp(str, "gfapi")) \ - fd = fopen (GFAPI_LOG_FILE, "a"); \ + fd = fopen (gbConf.daemonLogFile, "a"); \ + else if (!strcmp(str, "cli")) \ + fd = fopen (gbConf.cliLogFile, "a"); \ + else if (!strcmp(str, "gfapi")) \ + fd = fopen (gbConf.gfapiLogFile, "a"); \ else \ fd = stderr; \ if (fd == NULL) { \ @@ -406,6 +436,8 @@ int blockRemoteCreateRespEnumParse(const char *opt); void logTimeNow(char* buf, size_t bufSize); +int initLogging(void); + int gbAlloc(void *ptrptr, size_t size, const char *filename, const char *funcname, size_t linenr); -- cgit