@@ -87,18 +87,19 @@ static void format_rfc3339_timestamp(const struct timeval *tv, char *header_time
8787/* The Syslog Protocol RFC5424 format :
8888 * <pri>version sp timestamp sp hostname sp app-name sp procid sp msgid sp [sd-id]s sp msg
8989 */
90- int manager_push_to_network (Manager * m ,
91- int severity ,
92- int facility ,
93- const char * identifier ,
94- const char * message ,
95- const char * hostname ,
96- const char * pid ,
97- const struct timeval * tv ) {
98- char header_priority [ sizeof ( "< >1 " )];
90+ static int format_rfc5424 (Manager * m ,
91+ int severity ,
92+ int facility ,
93+ const char * identifier ,
94+ const char * message ,
95+ const char * hostname ,
96+ const char * pid ,
97+ const struct timeval * tv ) {
98+
9999 char header_time [FORMAT_TIMESTAMP_MAX ];
100- uint8_t makepri ;
100+ char header_priority [ sizeof ( "< >1 " )] ;
101101 struct iovec iov [14 ];
102+ uint8_t makepri ;
102103 int n = 0 ;
103104
104105 assert (m );
@@ -153,7 +154,7 @@ int manager_push_to_network(Manager *m,
153154 /* Ninth: message */
154155 IOVEC_SET_STRING (iov [n ++ ], message );
155156
156- /* Tenth: Optional newline message separator, if not implicitly terminated by end of UDP frame
157+ /* Last Optional newline message separator, if not implicitly terminated by end of UDP frame
157158 * De facto standard: separate messages by a newline
158159 */
159160 if (m -> protocol == SYSLOG_TRANSMISSION_PROTOCOL_TCP )
@@ -162,8 +163,101 @@ int manager_push_to_network(Manager *m,
162163 return network_send (m , iov , n );
163164}
164165
165- void manager_close_network_socket (Manager * m ) {
166+ static int format_rfc3339 (Manager * m ,
167+ int severity ,
168+ int facility ,
169+ const char * identifier ,
170+ const char * message ,
171+ const char * hostname ,
172+ const char * pid ,
173+ const struct timeval * tv ) {
174+
175+ char header_priority [sizeof ("< >1 " )];
176+ char header_time [FORMAT_TIMESTAMP_MAX ];
177+ struct iovec iov [14 ];
178+ uint8_t makepri ;
179+ int n = 0 ;
180+
166181 assert (m );
182+ assert (message );
183+
184+ makepri = (facility << 3 ) + severity ;
185+
186+ /* rfc3339
187+ * <35>Oct 12 22:14:15 client_machine su: 'su root' failed for joe on /dev/pts/2
188+ */
189+
190+ /* First: priority field '<pri>' */
191+ snprintf (header_priority , sizeof (header_priority ), "<%i>" , makepri );
192+ IOVEC_SET_STRING (iov [n ++ ], header_priority );
193+
194+ /* Third: timestamp */
195+ format_rfc3339_timestamp (tv , header_time , sizeof (header_time ));
196+ IOVEC_SET_STRING (iov [n ++ ], header_time );
197+
198+ /* Fourth: hostname */
199+ if (hostname )
200+ IOVEC_SET_STRING (iov [n ++ ], hostname );
201+ else
202+ IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
203+
204+ IOVEC_SET_STRING (iov [n ++ ], " " );
205+
206+ /* Fifth: identifier */
207+ if (identifier )
208+ IOVEC_SET_STRING (iov [n ++ ], identifier );
209+ else
210+ IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
211+
212+ IOVEC_SET_STRING (iov [n ++ ], "[" );
213+
214+ /* Sixth: procid */
215+ if (pid )
216+ IOVEC_SET_STRING (iov [n ++ ], pid );
217+ else
218+ IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
219+
220+ IOVEC_SET_STRING (iov [n ++ ], "]: " );
221+
222+ /* Ninth: message */
223+ IOVEC_SET_STRING (iov [n ++ ], message );
224+
225+ /* Last Optional newline message separator, if not implicitly terminated by end of UDP frame
226+ * De facto standard: separate messages by a newline
227+ */
228+ if (m -> protocol == SYSLOG_TRANSMISSION_PROTOCOL_TCP )
229+ IOVEC_SET_STRING (iov [n ++ ], "\n" );
230+
231+ return network_send (m , iov , n );
232+ }
233+
234+ int manager_push_to_network (Manager * m ,
235+ int severity ,
236+ int facility ,
237+ const char * identifier ,
238+ const char * message ,
239+ const char * hostname ,
240+ const char * pid ,
241+ const struct timeval * tv ) {
242+
243+ int r ;
244+
245+ assert (m );
246+ assert (message );
247+
248+ if (m -> log_format == SYSLOG_TRANSMISSION_LOG_FORMAT_RFC_5424 )
249+ r = format_rfc5424 (m , severity , facility , identifier , message , hostname , pid , tv );
250+ else
251+ r = format_rfc3339 (m , severity , facility , identifier , message , hostname , pid , tv );
252+
253+ if (r < 0 )
254+ return 0 ;
255+
256+ return 0 ;
257+ }
258+
259+ void manager_close_network_socket (Manager * m ) {
260+ assert (m );
167261
168262 if (m -> protocol == SYSLOG_TRANSMISSION_PROTOCOL_TCP ) {
169263 int r = shutdown (m -> socket , SHUT_RDWR );
0 commit comments