コード例 #1
0
ファイル: logger.c プロジェクト: alisw/uuid
static void logger_stdin(struct logger_ctl *ctl)
{
	int default_priority = ctl->pri;
	int last_pri = default_priority;
	size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
	char *const buf = xmalloc(max_usrmsg_size + 2 + 2);
	int pri;
	int c;
	size_t i;

	c = getchar();
	while (c != EOF) {
		i = 0;
		if (ctl->prio_prefix) {
			if (c == '<') {
				pri = 0;
				buf[i++] = c;
				while (isdigit(c = getchar()) && pri <= 191) {
					buf[i++] = c;
					pri = pri * 10 + c - '0';
				}
				if (c != EOF && c != '\n')
					buf[i++] = c;
				if (c == '>' && 0 <= pri && pri <= 191) { /* valid RFC PRI values */
					i = 0;
					if (pri < 8)
						pri |= 8; /* kern facility is forbidden */
					ctl->pri = pri;
				} else
					ctl->pri = default_priority;

				if (ctl->pri != last_pri) {
					generate_syslog_header(ctl);
					max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
					last_pri = ctl->pri;
				}
				if (c != EOF && c != '\n')
					c = getchar();
			}
		}

		while (c != EOF && c != '\n' && i < max_usrmsg_size) {
			buf[i++] = c;
			c = getchar();
		}
		buf[i] = '\0';

		if (i > 0 || !ctl->skip_empty_lines)
			write_output(ctl, buf);

		if (c == '\n')	/* discard line terminator */
			c = getchar();
	}
}
コード例 #2
0
ファイル: logger.c プロジェクト: alisw/uuid
static void logger_open(struct logger_ctl *ctl)
{
	if (ctl->server) {
		ctl->fd = inet_socket(ctl->server, ctl->port, &ctl->socket_type);
		if (!ctl->syslogfp)
			ctl->syslogfp = syslog_rfc5424_header;
	} else {
		if (!ctl->unix_socket)
			ctl->unix_socket = _PATH_DEVLOG;

		ctl->fd = unix_socket(ctl, ctl->unix_socket, &ctl->socket_type);
		if (!ctl->syslogfp)
			ctl->syslogfp = syslog_local_header;
	}
	if (!ctl->tag)
		ctl->tag = xgetlogin();
	generate_syslog_header(ctl);
}
コード例 #3
0
ファイル: logger.c プロジェクト: sebras/util-linux
static void logger_stdin(struct logger_ctl *ctl)
{
	/* note: we re-generate the syslog header for each log message to
	 * update header timestamps and to reflect possible priority changes.
	 * The initial header is generated by logger_open().
	 */
	int has_header = 1;
	int default_priority = ctl->pri;
	int last_pri = default_priority;
	size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
	char *const buf = xmalloc(max_usrmsg_size + 2 + 2);
	int pri;
	int c;
	size_t i;

	c = getchar();
	while (c != EOF) {
		i = 0;
		if (ctl->prio_prefix && c == '<') {
			pri = 0;
			buf[i++] = c;
			while (isdigit(c = getchar()) && pri <= 191) {
				buf[i++] = c;
				pri = pri * 10 + c - '0';
			}
			if (c != EOF && c != '\n')
				buf[i++] = c;
			if (c == '>' && 0 <= pri && pri <= 191) {
				/* valid RFC PRI values */
				i = 0;
				if (pri < 8)	/* kern facility is forbidden */
					pri |= 8;
				ctl->pri = pri;
			} else
				ctl->pri = default_priority;

			if (ctl->pri != last_pri) {
				has_header = 0;
				max_usrmsg_size =
				    ctl->max_message_size - strlen(ctl->hdr);
				last_pri = ctl->pri;
			}
			if (c != EOF && c != '\n')
				c = getchar();
		}

		while (c != EOF && c != '\n' && i < max_usrmsg_size) {
			buf[i++] = c;
			c = getchar();
		}
		buf[i] = '\0';

		if (i > 0 || !ctl->skip_empty_lines) {
			if (!has_header)
				generate_syslog_header(ctl);
			write_output(ctl, buf);
			has_header = 0;
		}

		if (c == '\n')	/* discard line terminator */
			c = getchar();
	}
}