@@ -217,13 +217,14 @@ bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char
217
217
}
218
218
219
219
// to calendar time
220
- struct tm * tm ;
220
+ struct tm now;
221
+ // Each of these functions returns NULL in case an error was detected. @see https://linux.die.net/man/3/localtime_r
221
222
if (utc) {
222
- if (( tm = gmtime ( &tv.tv_sec ) ) == NULL ) {
223
+ if (gmtime_r ( &tv.tv_sec , &now ) == NULL ) {
223
224
return false ;
224
225
}
225
226
} else {
226
- if (( tm = localtime ( &tv.tv_sec ) ) == NULL ) {
227
+ if (localtime_r ( &tv.tv_sec , &now ) == NULL ) {
227
228
return false ;
228
229
}
229
230
}
@@ -233,24 +234,24 @@ bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char
233
234
if (tag) {
234
235
written = snprintf (buffer, size,
235
236
" [%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%d][%s] " ,
236
- 1900 + tm -> tm_year , 1 + tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min , tm -> tm_sec , (int )(tv.tv_usec / 1000 ),
237
+ 1900 + now. tm_year , 1 + now. tm_mon , now. tm_mday , now. tm_hour , now. tm_min , now. tm_sec , (int )(tv.tv_usec / 1000 ),
237
238
level, getpid (), cid.c_str (), errno, tag);
238
239
} else {
239
240
written = snprintf (buffer, size,
240
241
" [%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%d] " ,
241
- 1900 + tm -> tm_year , 1 + tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min , tm -> tm_sec , (int )(tv.tv_usec / 1000 ),
242
+ 1900 + now. tm_year , 1 + now. tm_mon , now. tm_mday , now. tm_hour , now. tm_min , now. tm_sec , (int )(tv.tv_usec / 1000 ),
242
243
level, getpid (), cid.c_str (), errno);
243
244
}
244
245
} else {
245
246
if (tag) {
246
247
written = snprintf (buffer, size,
247
248
" [%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%s] " ,
248
- 1900 + tm -> tm_year , 1 + tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min , tm -> tm_sec , (int )(tv.tv_usec / 1000 ),
249
+ 1900 + now. tm_year , 1 + now. tm_mon , now. tm_mday , now. tm_hour , now. tm_min , now. tm_sec , (int )(tv.tv_usec / 1000 ),
249
250
level, getpid (), cid.c_str (), tag);
250
251
} else {
251
252
written = snprintf (buffer, size,
252
253
" [%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s] " ,
253
- 1900 + tm -> tm_year , 1 + tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min , tm -> tm_sec , (int )(tv.tv_usec / 1000 ),
254
+ 1900 + now. tm_year , 1 + now. tm_mon , now. tm_mday , now. tm_hour , now. tm_min , now. tm_sec , (int )(tv.tv_usec / 1000 ),
254
255
level, getpid (), cid.c_str ());
255
256
}
256
257
}
0 commit comments