Exemple #1
0
void
log_close_debug_channels(log_context lc) {
	log_channel_list lcl;
	int i;

	for (i = 0; i < lc->num_categories; i++)
		for (lcl = lc->categories[i]; lcl != NULL; lcl = lcl->next)
			if (lcl->channel->type == log_file &&
			    lcl->channel->out.file.stream != NULL &&
			    lcl->channel->flags & LOG_REQUIRE_DEBUG)
				(void)log_close_stream(lcl->channel);
}
Exemple #2
0
void
log_file(const char *name)
{
  if (!name)
    return;

  struct log_stream *ls = log_new_file(name, FF_FD2_FOLLOWS);
  struct log_stream *def = log_stream_by_flags(0);
  log_rm_substream(def, NULL);
  log_add_substream(def, ls);
  log_close_stream(ls);
}
Exemple #3
0
void
log_vwrite(log_context lc, int category, int level, const char *format,
	   va_list args) {
	log_channel_list lcl;
	int pri, debugging, did_vsprintf = 0;
	int original_category;
	FILE *stream;
	log_channel chan;
	struct timeval tv;
	struct tm *local_tm;
#ifdef HAVE_TIME_R
	struct tm tm_tmp;
#endif
	time_t tt;
	const char *category_name;
	const char *level_str;
	char time_buf[256];
	char level_buf[256];

	REQUIRE(lc != NULL);

	debugging = (lc->flags & LOG_OPTION_DEBUG);

	/*
	 * If not debugging, short circuit debugging messages very early.
	 */
	if (level > 0 && !debugging)
		return;

	if (category < 0 || category > lc->num_categories)
		category = 0;		/*%< use default */
	original_category = category;
	lcl = lc->categories[category];
	if (lcl == NULL) {
		category = 0;
		lcl = lc->categories[0];
	}

	/*
	 * Get the current time and format it.
	 */
	time_buf[0]='\0';
	if (gettimeofday(&tv, NULL) < 0) {
		syslog(LOG_INFO, "gettimeofday failed in log_vwrite()");
	} else {
		tt = tv.tv_sec;
#ifdef HAVE_TIME_R
		local_tm = localtime_r(&tt, &tm_tmp);
#else
		local_tm = localtime(&tt);
#endif
		if (local_tm != NULL) {
			sprintf(time_buf, "%02d-%s-%4d %02d:%02d:%02d.%03ld ",
				local_tm->tm_mday, months[local_tm->tm_mon],
				local_tm->tm_year+1900, local_tm->tm_hour,
				local_tm->tm_min, local_tm->tm_sec,
				(long)tv.tv_usec/1000);
		}
	}

	/*
	 * Make a string representation of the current category and level
	 */

	if (lc->category_names != NULL &&
	    lc->category_names[original_category] != NULL)
		category_name = lc->category_names[original_category];
	else
		category_name = "";

	if (level >= log_critical) {
		if (level >= 0) {
			sprintf(level_buf, "debug %d: ", level);
			level_str = level_buf;
		} else
			level_str = level_text[-level-1];
	} else {
		sprintf(level_buf, "level %d: ", level);
		level_str = level_buf;
	}

	/*
	 * Write the message to channels.
	 */
	for ( /* nothing */; lcl != NULL; lcl = lcl->next) {
		chan = lcl->channel;

		if (!log_check_channel(lc, level, chan))
			continue;

		if (!did_vsprintf) {
			(void)vsprintf(lc->buffer, format, args);
			if (strlen(lc->buffer) > (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()");
		}
	}
}