[sheepdog] [PATCH v2 3/3] let logger work on rhel5 environment

Liu Yuan namei.unix at gmail.com
Wed Aug 21 09:53:48 CEST 2013


On Wed, Aug 21, 2013 at 02:37:37PM +0800, Robin Dong wrote:
> From: Robin Dong <sanbai at taobao.com>
> 
> In gcc-4.1.2, it dose not support attribute like "constructor(priority)" (it only support
> contstructor without 'priority'), so call regist_json/server/default_log_formatter in
> init_log_formatter() directly.
> 
> Signed-off-by: Robin Dong <sanbai at taobao.com>
> ---
>  lib/logger.c |  184 +++++++++++++++++++++++++++++----------------------------
>  1 files changed, 94 insertions(+), 90 deletions(-)
> 
> diff --git a/lib/logger.c b/lib/logger.c
> index 6c2f96f..24a3e2c 100644
> --- a/lib/logger.c
> +++ b/lib/logger.c
> @@ -102,7 +102,7 @@ struct log_format {
>  };
>  
>  #define log_format_register(n, formatter_fn)				\
> -	static void __attribute__((constructor(101)))			\
> +	static void							\
>  	regist_ ## formatter_fn(void) {					\
>  		static struct log_format f =				\
>  			{ .name = n, .formatter = formatter_fn };	\
> @@ -141,95 +141,6 @@ static const char *format_thread_name(char *str, size_t size, const char *name,
>  	return str;
>  }
>  
> -/*
> - * We need to set default log formatter because dog doesn't want to call
> - * select_log_formatter().
> - */
> -static void __attribute__((constructor(65535)))
> -init_log_formatter(void)
> -{
> -	struct log_format *f;
> -
> -	list_for_each_entry(f, &log_formats, list) {
> -		if (!strcmp(f->name, "default")) {
> -			format = f;
> -			return;
> -		}
> -	}
> -	syslog(LOG_ERR, "failed to set default formatter\n");
> -	exit(1);
> -}
> -
> -static int logarea_init(int size)
> -{
> -	int shmid;
> -
> -	shmid = shmget(IPC_PRIVATE, sizeof(struct logarea),
> -		       0644 | IPC_CREAT | IPC_EXCL);
> -	if (shmid == -1) {
> -		syslog(LOG_ERR, "shmget logarea failed: %m");
> -		return 1;
> -	}
> -
> -	la = shmat(shmid, NULL, 0);
> -	if (!la) {
> -		syslog(LOG_ERR, "shmat logarea failed: %m");
> -		return 1;
> -	}
> -
> -	shmctl(shmid, IPC_RMID, NULL);
> -
> -	if (size < MAX_MSG_SIZE)
> -		size = LOG_SPACE_SIZE;
> -
> -	shmid = shmget(IPC_PRIVATE, size, 0644 | IPC_CREAT | IPC_EXCL);
> -	if (shmid == -1) {
> -		syslog(LOG_ERR, "shmget msg failed: %m");
> -		shmdt(la);
> -		return 1;
> -	}
> -
> -	la->start = shmat(shmid, NULL, 0);
> -	if (!la->start) {
> -		syslog(LOG_ERR, "shmat msg failed: %m");
> -		shmdt(la);
> -		return 1;
> -	}
> -	memset(la->start, 0, size);
> -
> -	shmctl(shmid, IPC_RMID, NULL);
> -
> -	la->end = la->start + size;
> -	la->tail = la->start;
> -
> -	la->semid = semget(semkey, 1, 0666 | IPC_CREAT);
> -	if (la->semid < 0) {
> -		syslog(LOG_ERR, "semget failed: %m");
> -		shmdt(la->start);
> -		shmdt(la);
> -		return 1;
> -	}
> -
> -	la->semarg.val = 1;
> -	if (semctl(la->semid, 0, SETVAL, la->semarg) < 0) {
> -		syslog(LOG_ERR, "semctl failed: %m");
> -		shmdt(la->start);
> -		shmdt(la);
> -		return 1;
> -	}
> -
> -	return 0;
> -}
> -
> -static void free_logarea(void)
> -{
> -	if (log_fd >= 0)
> -		close(log_fd);
> -	semctl(la->semid, 0, IPC_RMID, la->semarg);
> -	shmdt(la->start);
> -	shmdt(la);
> -}
> -
>  static int server_log_formatter(char *buff, size_t size,
>  				const struct logmsg *msg)
>  {
> @@ -318,6 +229,99 @@ static int json_log_formatter(char *buff, size_t size,
>  }
>  log_format_register("json", json_log_formatter);
>  
> +/*
> + * We need to set default log formatter because dog doesn't want to call
> + * select_log_formatter().
> + */
> +static void __attribute__((constructor))
> +init_log_formatter(void)
> +{
> +	struct log_format *f;
> +
> +	regist_json_log_formatter();
> +	regist_server_log_formatter();
> +	regist_default_log_formatter();

How about removing log_format_register macro and write a function
log_format_register() and then simply call
      log_format_register("json", json_log_formatter) 
inside the init_log_formatter()?

Thanks
Yuan



More information about the sheepdog mailing list