void LogSinkImpl::run() { _running = true; mx_print('A', "Logging thread started\n"); while (true) { boost::unique_lock<mutex_t> lock(_mutex); while (!_terminate && _queue.empty()) _cond.wait(_mutex); if (_terminate && _queue.empty()) break; Buffer buf = _queue.front(); _queue.pop(); lock.unlock(); if (!buf.ptr && !buf.size) mx_rotate(true); else { mx_write(buf.ptr, buf.wrt); if (FREE_SIZE) { boost::lock_guard<spinlock> guard(_free_lck); if (_free.size() < FREE_SIZE) { _free.push(buf); buf.ptr = 0; } } if (buf.ptr) std::free(buf.ptr); } } mx_print('A', "Logging thread finished\n"); _running = false; }
// MUTEX must be hold externally bool LogSinkImpl::mx_print(char lvl, const char* s) { LogTimestamp ts; ts.update(lvl); return mx_print(ts, s); }
void write_frame() { mxtype *v; int i; /* Decide what to do with partial bins */ if (save_partial) { if (rows_accumulated != 0) frame_h++; /* Keep partial row always */ } else if (frame_h == 0 && rows_accumulated != 0) frame_h++; /* Keep partial row if it is the only one */ else { /* Ignore partial row; update accounting */ v = matrix + frame_h*frame_w; for (i=0; i < frame_w; i++) { recorded_counts -= v[i]; ignored_counts += v[i]; } } /* Check for consistent number of rows in frame */ if (rows == 0) { rows = frame_h; } else if (frame_h == 0) { /* ICP dropped the frame so fill with zeros */ frame_w = columns; for (frame_h = 0; frame_h < rows; frame_h++) clear_row(); // printf("recover dropped frame to %d x %d\n", frame_h, frame_w); } else if (rows != frame_h) { if (warn_dims) fprintf(stderr,"inconsistent number of rows\n"); warn_dims = 0; while (frame_h < rows) { clear_row(); frame_h++; } frame_h = rows; /* in case it was bigger */ } #ifdef DEBUG printf("=========== %d x %d \n", frame_w, frame_h); mx_print(frame_h,frame_w,matrix); printf("===========\n"); #endif /* Transpose the matrix if necessary */ if (do_transpose) { mx_transpose(frame_h,frame_w,matrix,matrix); i = frame_w; frame_w = frame_h; frame_h = i; } /* Output the rows one by one */ v = matrix; for (i=0; i < frame_h-1; i++) { switch(output) { case VTK: vtk_save(outfile,v,frame_w,1); break; case ICP: icp_save(outfile,v,frame_w,1); break; } v += frame_w; } switch (output) { case VTK: vtk_save(outfile,v,frame_w,0); break; case ICP: icp_save(outfile,v,frame_w,0); break; } }
void LogSinkImpl::mx_close() { if (_fd < 0) return; if (!_prog.empty()) mx_print('A', "Log file will be closed!\n"); int fd = _fd; _fd = -1; ::close(fd); }
void LogSinkImpl::mx_rotate(bool print_msg) { char buffer[80]; struct tm timeinfo; localtime_r(&_time, &timeinfo); strftime(buffer, sizeof(buffer), "%Y%m%d-", &timeinfo); std::string logname = _dir + "/" + buffer + _prog + ".log"; if (print_msg) mx_print('A', "Log file will be rotated!\n"); mx_open(logname.c_str()); }