205 {
206 std::lock_guard<std::mutex> lock(lock_);
207
208
209 if (base_filename_selected_ && base_filename_.empty()) {
210 return;
211 }
212
214 if (file_ != nullptr) {
215 fclose(file_);
216 }
217 file_ = nullptr;
218 file_length_ = bytes_since_flush_ = 0;
219 rollover_attempt_ = kRolloverAttemptFrequency - 1;
220 }
221
222
223 if (file_ == nullptr) {
224
225
226
227 if (++rollover_attempt_ != kRolloverAttemptFrequency) {
228 return;
229 }
230 rollover_attempt_ = 0;
231
232 struct ::tm tm_time;
233 localtime_r(×tamp, &tm_time);
234
235
236 ostringstream time_pid_stream;
237 time_pid_stream.fill('0');
238 time_pid_stream << 1900 + tm_time.tm_year << setw(2) << 1 + tm_time.tm_mon
239 << setw(2) << tm_time.tm_mday << '-' << setw(2)
240 << tm_time.tm_hour << setw(2) << tm_time.tm_min << setw(2)
242 const string& time_pid_string = time_pid_stream.str();
243
244
245 if (base_filename_selected_) {
246 if (!CreateLogfile(time_pid_string)) {
247 perror("Could not create log file");
248 fprintf(stderr, "COULD NOT CREATE LOGFILE '%s'!\n",
249 time_pid_string.c_str());
250 return;
251 }
252 }
253
254
255 ostringstream file_header_stream;
256 file_header_stream.fill('0');
257 file_header_stream << "Log file created at: " << 1900 + tm_time.tm_year
258 << '/' << setw(2) << 1 + tm_time.tm_mon << '/' << setw(2)
259 << tm_time.tm_mday << ' ' << setw(2) << tm_time.tm_hour
260 << ':' << setw(2) << tm_time.tm_min << ':' << setw(2)
261 << tm_time.tm_sec << '\n'
262 <<
"Running on machine: " <<
hostname() <<
'\n'
263 << "Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu "
264 << "threadid file:line] msg" << '\n';
265 const string& file_header_string = file_header_stream.str();
266
267 const int header_len = static_cast<int>(file_header_string.size());
268 if (file_ == nullptr) {
269 return;
270 }
271 fwrite(file_header_string.data(), 1, header_len, file_);
272 file_length_ += header_len;
273 bytes_since_flush_ += header_len;
274 }
275
276
277 if (!stop_writing) {
278
279
280
281
282
283 errno = 0;
284 fwrite(message, 1, message_len, file_);
285 if (FLAGS_stop_logging_if_full_disk &&
286 errno == ENOSPC) {
287 stop_writing = true;
288 return;
289 } else {
290 file_length_ += message_len;
291 bytes_since_flush_ += message_len;
292 }
293 } else {
295 stop_writing = false;
296 }
297 return;
298 }
299
300
301
302 if (force_flush || (bytes_since_flush_ >= 1000000) ||
305 }
306}
const string & hostname()
int32_t GetMainThreadPid()