FreeBSD/sys/kern/subr_msgbuf.c
/*
 * Append a NUL-terminated string with a priority to a message buffer.
 * Filter carriage returns if the caller requests it.
 *
 * XXX The carriage return filtering behavior is present in the
 * msglogchar() API, however testing has shown that we don't seem to send
 * carriage returns down this path.  So do we still need it?
 */
Append a NUL-terminated string with a priority to a message buffer.
Filter carriage returns if the caller requests it.

XXX The carriage return filtering behavior is present in the
msglogchar() API, however testing has shown that we don't seem to send
carriage returns down this path.
Append a NUL - terminated string with a priority to a message buffer . Filter carriage returns if the caller requests it . XXX The carriage return filtering behavior is present in the msglogchar () API however testing has shown that we don ' t seem to send carriage returns down this path .
void
msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int filter_cr)
{
	u_int seq;
	size_t len, prefix_len;
	char prefix[MAXPRIBUF];
	char buf[32];
	int nl, i, j, needtime;
	len = strlen(str);
	prefix_len = 0;
	nl = 0;
	if (len == 0)
		return;
	mtx_lock_spin(&mbp->msg_lock);
	if (pri != -1)
		prefix_len = sprintf(prefix, "<%d>", pri);
	seq = mbp->msg_wseq;
	if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) {
		msgbuf_do_addchar(mbp, &seq, '\n');
		mbp->msg_flags &= ~MSGBUF_NEEDNL;
	}
	needtime = 1;
	for (i = 0; i < len; i++) {
		if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) {
			int j;
			for (j = 0; j < prefix_len; j++)
				msgbuf_do_addchar(mbp, &seq, prefix[j]);
		}
		if (msgbuf_show_timestamp && needtime == 1 &&
		    (mbp->msg_flags & MSGBUF_NEEDNL) == 0) {
			snprintf(buf, sizeof(buf), "[%jd] ",
			    (intmax_t)time_uptime);
			for (j = 0; buf[j] != '\0'; j++)
				msgbuf_do_addchar(mbp, &seq, buf[j]);
			needtime = 0;
		}
		if ((filter_cr != 0) && (str[i] == '\r'))
			continue;
		if (str[i] == '\n')
			mbp->msg_flags &= ~MSGBUF_NEEDNL;
		else
			mbp->msg_flags |= MSGBUF_NEEDNL;
		msgbuf_do_addchar(mbp, &seq, str[i]);
	}
	mbp->msg_wseq = seq;
	mbp->msg_lastpri = pri;
	mtx_unlock_spin(&mbp->msg_lock);
}
void msgbuf_addstr ( struct msgbuf * mbp , int pri , char * str , int filter_cr ) { u_int seq ; size_t len , prefix_len ; char prefix [ MAXPRIBUF ] ; char buf [ 32 ] ; int nl , i , j , needtime ; len = strlen ( str ) ; prefix_len = 0 ; nl = 0 ; if ( len == 0 ) return ; mtx_lock_spin ( & mbp -> msg_lock ) ; if ( pri != -1 ) prefix_len = sprintf ( prefix , " " , pri ) ; seq = mbp -> msg_wseq ; if ( mbp -> msg_lastpri != pri && ( mbp -> msg_flags & MSGBUF_NEEDNL ) != 0 ) { msgbuf_do_addchar ( mbp , & seq , ' \n ' ) ; mbp -> msg_flags &= ~ MSGBUF_NEEDNL ; } needtime = 1 ; for ( i = 0 ; i < len ; i ++ ) { if ( ( mbp -> msg_flags & MSGBUF_NEEDNL ) == 0 && prefix_len != 0 ) { int j ; for ( j = 0 ; j < prefix_len ; j ++ ) msgbuf_do_addchar ( mbp , & seq , prefix [ j ] ) ; } if ( msgbuf_show_timestamp && needtime == 1 && ( mbp -> msg_flags & MSGBUF_NEEDNL ) == 0 ) { snprintf ( buf , sizeof ( buf ) , " " , ( intmax_t ) time_uptime ) ; for ( j = 0 ; buf [ j ] != ' \0 ' ; j ++ ) msgbuf_do_addchar ( mbp , & seq , buf [ j ] ) ; needtime = 0 ; } if ( ( filter_cr != 0 ) && ( str [ i ] == ' \r ' ) ) continue ; if ( str [ i ] == ' \n ' ) mbp -> msg_flags &= ~ MSGBUF_NEEDNL ; else mbp -> msg_flags |= MSGBUF_NEEDNL ; msgbuf_do_addchar ( mbp , & seq , str [ i ] ) ; } mbp -> msg_wseq = seq ; mbp -> msg_lastpri = pri ; mtx_unlock_spin ( & mbp -> msg_lock ) ; }
Append a NUL-terminated string with a priority to a message buffer.
Append a NUL - terminated string with a priority to a message buffer .
/* If we have a zero-length string, no need to do anything. */ /*
	 * If this is true, we may need to insert a new priority sequence,
	 * so prepare the prefix.
	 */ /*
	 * Starting write sequence number.
	 */ /*
	 * Whenever there is a change in priority, we have to insert a
	 * newline, and a priority prefix if the priority is not -1.  Here
	 * we detect whether there was a priority change, and whether we
	 * did not end with a newline.  If that is the case, we need to
	 * insert a newline before this string.
	 */ /*
		 * If we just had a newline, and the priority is not -1
		 * (and therefore prefix_len != 0), then we need a priority
		 * prefix for this line.
		 */ /*
		 * Don't copy carriage returns if the caller requested
		 * filtering.
		 * 
		 * XXX This matches the behavior of msglogchar(), but is it
		 * necessary?  Testing has shown that we don't seem to get
		 * carriage returns here.
		 */ /*
		 * Clear this flag if we see a newline.  This affects whether
		 * we need to insert a new prefix or insert a newline later.
		 */ /*
	 * Update the write sequence number for the actual number of
	 * characters we put in the message buffer.  (Depends on whether
	 * carriage returns are filtered.)
	 */ /*
	 * Set the last priority.
	 */