⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logging.c

📁 bind 9.3结合mysql数据库
💻 C
📖 第 1 页 / 共 2 页
字号:
		if (!log_check_channel(lc, level, chan))			continue;		if (!did_vsprintf) {			if (VSPRINTF((lc->buffer, format, args)) >			    (size_t)LOG_BUFFER_SIZE) {				syslog(LOG_CRIT,				       "memory overrun in log_vwrite()");				exit(1);			}			did_vsprintf = 1;		}		switch (chan->type) {		case log_syslog:			if (level >= log_critical)				pri = (level >= 0) ? 0 : -level;			else				pri = -log_critical;			syslog(chan->out.facility|syslog_priority[pri],			       "%s%s%s%s",			       (chan->flags & LOG_TIMESTAMP) ?	time_buf : "",			       (chan->flags & LOG_PRINT_CATEGORY) ?			       category_name : "",			       (chan->flags & LOG_PRINT_LEVEL) ?			       level_str : "",			       lc->buffer);			break;		case log_file:			stream = chan->out.file.stream;			if (stream == NULL) {				stream = log_open_stream(chan);				if (stream == NULL)					break;			}			if (chan->out.file.max_size != ULONG_MAX) {				long pos;								pos = ftell(stream);				if (pos >= 0 &&				    (unsigned long)pos >				    chan->out.file.max_size) {					/*					 * try to roll over the log files,					 * ignoring all all return codes					 * except the open (we don't want					 * to write any more anyway)					 */					log_close_stream(chan);					version_rename(chan);					stream = log_open_stream(chan);					if (stream == NULL)						break;				}			}			fprintf(stream, "%s%s%s%s\n", 				(chan->flags & LOG_TIMESTAMP) ?	time_buf : "",				(chan->flags & LOG_PRINT_CATEGORY) ?				category_name : "",				(chan->flags & LOG_PRINT_LEVEL) ?				level_str : "",				lc->buffer);			fflush(stream);			break;		case log_null:			break;		default:			syslog(LOG_ERR,			       "unknown channel type in log_vwrite()");		}	}}voidlog_write(log_context lc, int category, int level, const char *format, ...) {	va_list args;	va_start(args, format);	log_vwrite(lc, category, level, format, args);	va_end(args);}/* * Functions to create, set, or destroy contexts */intlog_new_context(int num_categories, char **category_names, log_context *lc) {	log_context nlc;	nlc = memget(sizeof (struct log_context));	if (nlc == NULL) {		errno = ENOMEM;		return (-1);	}	nlc->num_categories = num_categories;	nlc->category_names = category_names;	nlc->categories = memget(num_categories * sizeof (log_channel_list));	if (nlc->categories == NULL) {		memput(nlc, sizeof (struct log_context));		errno = ENOMEM;		return (-1);	}	memset(nlc->categories, '\0',	       num_categories * sizeof (log_channel_list));	nlc->flags = 0U;	nlc->level = 0;	*lc = nlc;	return (0);}voidlog_free_context(log_context lc) {	log_channel_list lcl, lcl_next;	log_channel chan;	int i;	REQUIRE(lc != NULL);	for (i = 0; i < lc->num_categories; i++)		for (lcl = lc->categories[i]; lcl != NULL; lcl = lcl_next) {			lcl_next = lcl->next;			chan = lcl->channel;			(void)log_free_channel(chan);			memput(lcl, sizeof (struct log_channel_list));		}	memput(lc->categories,	       lc->num_categories * sizeof (log_channel_list));	memput(lc, sizeof (struct log_context));}intlog_add_channel(log_context lc, int category, log_channel chan) {	log_channel_list lcl;	if (lc == NULL || category < 0 || category >= lc->num_categories) {		errno = EINVAL;		return (-1);	}	lcl = memget(sizeof (struct log_channel_list));	if (lcl == NULL) {		errno = ENOMEM;		return(-1);	}	lcl->channel = chan;	lcl->next = lc->categories[category];	lc->categories[category] = lcl;	chan->references++;	return (0);}intlog_remove_channel(log_context lc, int category, log_channel chan) {	log_channel_list lcl, prev_lcl, next_lcl;	int found = 0;	if (lc == NULL || category < 0 || category >= lc->num_categories) {		errno = EINVAL;		return (-1);	}	for (prev_lcl = NULL, lcl = lc->categories[category];	     lcl != NULL;	     lcl = next_lcl) {		next_lcl = lcl->next;		if (lcl->channel == chan) {			log_free_channel(chan);			if (prev_lcl != NULL)				prev_lcl->next = next_lcl;			else				lc->categories[category] = next_lcl;			memput(lcl, sizeof (struct log_channel_list));			/*			 * We just set found instead of returning because			 * the channel might be on the list more than once.			 */			found = 1;		} else			prev_lcl = lcl;	}	if (!found) {		errno = ENOENT;		return (-1);	}	return (0);}intlog_option(log_context lc, int option, int value) {	if (lc == NULL) {		errno = EINVAL;		return (-1);	}	switch (option) {	case LOG_OPTION_DEBUG:		if (value)			lc->flags |= option;		else			lc->flags &= ~option;		break;	case LOG_OPTION_LEVEL:		lc->level = value;		break;	default:		errno = EINVAL;		return (-1);	}	return (0);}intlog_category_is_active(log_context lc, int category) {	if (lc == NULL) {		errno = EINVAL;		return (-1);	}	if (category >= 0 && category < lc->num_categories &&	    lc->categories[category] != NULL)		return (1);	return (0);}log_channellog_new_syslog_channel(unsigned int flags, int level, int facility) {	log_channel chan;	chan = memget(sizeof (struct log_channel));	if (chan == NULL) {		errno = ENOMEM;		return (NULL);	}	chan->type = log_syslog;	chan->flags = flags;	chan->level = level;	chan->out.facility = facility;	chan->references = 0;	return (chan);}log_channellog_new_file_channel(unsigned int flags, int level,		     const char *name, FILE *stream, unsigned int versions,		     unsigned long max_size) {	log_channel chan;	chan = memget(sizeof (struct log_channel));	if (chan == NULL) {		errno = ENOMEM;		return (NULL);	}	chan->type = log_file;	chan->flags = flags;	chan->level = level;	if (name != NULL) {		size_t len;				len = strlen(name);		/* 		 * Quantize length to a multiple of 256.  There's space for the		 * NUL, since if len is a multiple of 256, the size chosen will		 * be the next multiple.		 */		chan->out.file.name_size = ((len / 256) + 1) * 256;		chan->out.file.name = memget(chan->out.file.name_size);		if (chan->out.file.name == NULL) {			memput(chan, sizeof (struct log_channel));			errno = ENOMEM;			return (NULL);		}		/* This is safe. */		strcpy(chan->out.file.name, name);	} else {		chan->out.file.name_size = 0;		chan->out.file.name = NULL;	}	chan->out.file.stream = stream;	chan->out.file.versions = versions;	chan->out.file.max_size = max_size;	chan->out.file.owner = getuid();	chan->out.file.group = getgid();	chan->references = 0;	return (chan);}intlog_set_file_owner(log_channel chan, uid_t owner, gid_t group) {	if (chan->type != log_file) {		errno = EBADF;		return (-1);	}	chan->out.file.owner = owner;	chan->out.file.group = group;	return (0);}log_channellog_new_null_channel() {	log_channel chan;	chan = memget(sizeof (struct log_channel));	if (chan == NULL) {		errno = ENOMEM;		return (NULL);	}	chan->type = log_null;	chan->flags = LOG_CHANNEL_OFF;	chan->level = log_info;	chan->references = 0;	return (chan);}intlog_inc_references(log_channel chan) {	if (chan == NULL) {		errno = EINVAL;		return (-1);	}	chan->references++;	return (0);}intlog_dec_references(log_channel chan) {	if (chan == NULL || chan->references <= 0) {		errno = EINVAL;		return (-1);	}	chan->references--;	return (0);}log_channel_typelog_get_channel_type(log_channel chan) {	REQUIRE(chan != NULL);		return (chan->type);}intlog_free_channel(log_channel chan) {	if (chan == NULL || chan->references <= 0) {		errno = EINVAL;		return (-1);	}	chan->references--;	if (chan->references == 0) {		if (chan->type == log_file) {			if ((chan->flags & LOG_CLOSE_STREAM) &&			    chan->out.file.stream != NULL)				(void)fclose(chan->out.file.stream);			if (chan->out.file.name != NULL)				memput(chan->out.file.name,				       chan->out.file.name_size);		}		memput(chan, sizeof (struct log_channel));	}	return (0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -