* 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? */
| 181 | * carriage returns down this path. So do we still need it? |
| 182 | */ |
| 183 | void |
| 184 | msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr) |
| 185 | { |
| 186 | u_int seq; |
| 187 | size_t len, prefix_len; |
| 188 | char prefix[MAXPRIBUF]; |
| 189 | char buf[32]; |
| 190 | int i, j, needtime; |
| 191 | |
| 192 | len = strlen(str); |
| 193 | prefix_len = 0; |
| 194 | |
| 195 | /* If we have a zero-length string, no need to do anything. */ |
| 196 | if (len == 0) |
| 197 | return; |
| 198 | |
| 199 | mtx_lock_spin(&mbp->msg_lock); |
| 200 | |
| 201 | /* |
| 202 | * If this is true, we may need to insert a new priority sequence, |
| 203 | * so prepare the prefix. |
| 204 | */ |
| 205 | if (pri != -1) |
| 206 | prefix_len = sprintf(prefix, "<%d>", pri); |
| 207 | |
| 208 | /* |
| 209 | * Starting write sequence number. |
| 210 | */ |
| 211 | seq = mbp->msg_wseq; |
| 212 | |
| 213 | /* |
| 214 | * Whenever there is a change in priority, we have to insert a |
| 215 | * newline, and a priority prefix if the priority is not -1. Here |
| 216 | * we detect whether there was a priority change, and whether we |
| 217 | * did not end with a newline. If that is the case, we need to |
| 218 | * insert a newline before this string. |
| 219 | */ |
| 220 | if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) { |
| 221 | msgbuf_do_addchar(mbp, &seq, '\n'); |
| 222 | mbp->msg_flags &= ~MSGBUF_NEEDNL; |
| 223 | } |
| 224 | |
| 225 | needtime = 1; |
| 226 | for (i = 0; i < len; i++) { |
| 227 | /* |
| 228 | * If we just had a newline, and the priority is not -1 |
| 229 | * (and therefore prefix_len != 0), then we need a priority |
| 230 | * prefix for this line. |
| 231 | */ |
| 232 | if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) { |
| 233 | int j; |
| 234 | |
| 235 | for (j = 0; j < prefix_len; j++) |
| 236 | msgbuf_do_addchar(mbp, &seq, prefix[j]); |
| 237 | } |
| 238 | |
| 239 | if (msgbuf_show_timestamp && needtime == 1 && |
| 240 | (mbp->msg_flags & MSGBUF_NEEDNL) == 0) { |
no test coverage detected