Revision ebc2733a
Added by David Sorber almost 8 years ago
| software/photo_compress_archiver/PhotoCompressArchiver.cc | ||
|---|---|---|
|
#include <chrono>
|
||
|
#include <cmath>
|
||
|
#include <cstdio>
|
||
|
#include <fstream>
|
||
|
#include <iomanip>
|
||
|
#include <iostream>
|
||
|
#include <limits.h>
|
||
|
#include <thread>
|
||
|
#include <sys/stat.h>
|
||
|
#include <sys/wait.h>
|
||
|
#include <unistd.h>
|
||
| ... | ... | |
|
#include <packjpg.h>
|
||
|
|
||
|
#include "PhotoCompressArchiver.hh"
|
||
|
#include "ProgressBar.h"
|
||
|
|
||
|
#define ERROR std::cerr << BOLD << RED << "ERROR: " << ENDC
|
||
|
|
||
|
#define WRKR_OUT_REG(msg) { \
|
||
|
std::lock_guard<std::mutex> lock(m_output_mutex); \
|
||
|
std::cout << "T[" << std::setw(2) << tid << "] " << msg << std::endl; \
|
||
|
if (m_verbose) { \
|
||
|
std::lock_guard<std::mutex> lock(m_output_mutex); \
|
||
|
std::cout << "T[" << std::setw(2) << tid << "] " << msg << std::endl; \
|
||
|
} \
|
||
|
}
|
||
|
|
||
|
#define WRKR_OUT_ERR(msg) { \
|
||
|
std::lock_guard<std::mutex> lock(m_output_mutex); \
|
||
|
std::cerr << "T[" << std::setw(2) << tid << "] " << BOLD << RED \
|
||
|
<< "ERROR: " << ENDC << msg << std::endl; \
|
||
|
if (m_verbose) { \
|
||
|
std::lock_guard<std::mutex> lock(m_output_mutex); \
|
||
|
std::cerr << "T[" << std::setw(2) << tid << "] " << BOLD << RED \
|
||
|
<< "ERROR: " << ENDC << msg << std::endl; \
|
||
|
} \
|
||
|
}
|
||
|
|
||
|
// Helper functions
|
||
|
// Helper function
|
||
|
std::string _format_num_bytes(uint64_t num_bytes)
|
||
|
{
|
||
|
const uint64_t tera = (1024 * 1024 * 1024 * 1024L);
|
||
| ... | ... | |
|
uint32_t num_threads,
|
||
|
bool decompress,
|
||
|
bool delete_orig,
|
||
|
bool verbose,
|
||
|
boost::regex& filter_regex)
|
||
|
: m_path(path),
|
||
|
m_num_threads(num_threads),
|
||
|
m_decompress(decompress),
|
||
|
m_delete_orig(delete_orig),
|
||
|
m_verbose(verbose),
|
||
|
m_terminate_flag(false),
|
||
|
m_files_processed(0),
|
||
|
m_num_input_files(0),
|
||
|
m_total_uncompressed_size(0),
|
||
|
m_total_compressed_size(0),
|
||
|
m_running_compressed_size(0),
|
||
|
p_finder_thread(nullptr),
|
||
|
m_filter_empty(true),
|
||
|
m_filter_regex(filter_regex)
|
||
| ... | ... | |
|
|
||
|
// Join the file finder thread
|
||
|
p_finder_thread->join();
|
||
|
std::cout << "File finder completed:" << std::endl;
|
||
|
|
||
|
// Error out of no input files were found
|
||
|
if (m_num_input_files == 0)
|
||
| ... | ... | |
|
return -1;
|
||
|
}
|
||
|
|
||
|
std::cout << " Found " << m_num_input_files << " files" << std::endl;
|
||
|
|
||
|
std::cout << "File finder completed: " << m_num_input_files << " files";
|
||
|
if (! m_decompress)
|
||
|
{
|
||
|
std::cout << " Total uncompressed bytes: "
|
||
|
std::cout << "; "
|
||
|
<< _format_num_bytes(m_total_uncompressed_size) << "\n"
|
||
|
<< std::endl;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
std::cout << std::endl;
|
||
|
}
|
||
|
|
||
|
// Add terminator for each worker thread
|
||
|
for (uint32_t idx = 0; idx < m_num_threads; ++idx)
|
||
| ... | ... | |
|
delete p_finder_thread;
|
||
|
p_finder_thread = nullptr;
|
||
|
|
||
|
// Display status
|
||
|
std::cout << BOLD << "\n>>> Status:" << ENDC << std::endl;
|
||
|
std::string line(100, '-');
|
||
|
std::cout << line << std::endl;
|
||
|
if (! m_verbose)
|
||
|
{
|
||
|
// If not in verbose mode display running status
|
||
|
ProgressBar pbar = ProgressBar("Progress: ", PROGRESS_BAR_WIDTH);
|
||
|
double percent_done = 0.0;
|
||
|
while ((percent_done < 100.0) && (! m_terminate_flag))
|
||
|
{
|
||
|
// Grab current percent done
|
||
|
percent_done = get_global_percent_done();
|
||
|
|
||
|
// Calculate current elapsed time
|
||
|
auto current = std::chrono::system_clock::now();
|
||
|
auto diff = current - start;
|
||
|
double duration = std::chrono::duration<double>(diff).count();
|
||
|
|
||
|
double ratio = ((double)m_total_compressed_size /
|
||
|
m_running_compressed_size) * 100;
|
||
|
|
||
|
uint32_t num_errors = m_errors.size();
|
||
|
|
||
|
std::cout << "Elapsed time: " << std::fixed
|
||
|
<< std::setprecision(3) << duration << " s "
|
||
|
<< "Files processed: "
|
||
|
<< m_files_processed << " / "
|
||
|
<< m_num_input_files << " "
|
||
|
<< "Errors: " << num_errors << " "
|
||
|
<< std::endl;
|
||
|
|
||
|
pbar.update(std::lround(percent_done));
|
||
|
|
||
|
std::cout << "Compressed/uncompressed size: "
|
||
|
<< _format_num_bytes(m_total_compressed_size) << " / "
|
||
|
<< _format_num_bytes(m_running_compressed_size)
|
||
|
<< " Ratio: " << std::fixed
|
||
|
<< std::setprecision(2) << ratio << "% "
|
||
|
<< std::endl;
|
||
|
|
||
|
std::cout << line << std::endl;
|
||
|
|
||
|
// Delay slightly to prevent flickering
|
||
|
if (percent_done < 100.0)
|
||
|
{
|
||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||
|
std::cout << UP_ONE << UP_ONE << UP_ONE << UP_ONE;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Now join the worker threads
|
||
|
for (auto worker : m_worker_threads)
|
||
|
{
|
||
| ... | ... | |
|
const bfs::path& dir_path,
|
||
|
const boost::regex& file_regex)
|
||
|
{
|
||
|
// Check the terminate flag before proceeding
|
||
|
if (m_terminate_flag)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
bfs::directory_iterator end_iter;
|
||
|
for (bfs::directory_iterator dir_iter(dir_path); dir_iter != end_iter; ++dir_iter)
|
||
|
{
|
||
| ... | ... | |
|
|
||
|
// Blocking wait for next work unit
|
||
|
work_unit = m_file_list.pop_front();
|
||
|
if (m_terminate_flag)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (work_unit == nullptr)
|
||
|
{
|
||
|
break;
|
||
| ... | ... | |
|
//~ WRKR_OUT_REG("Output size: " << out_size)
|
||
|
|
||
|
// Increment counts
|
||
|
m_running_compressed_size += work_unit->m_file_size;
|
||
|
m_total_compressed_size += out_size;
|
||
|
++m_files_processed;
|
||
|
|
||
| ... | ... | |
|
|
||
|
double PhotoCompressArchiver::get_global_percent_done()
|
||
|
{
|
||
|
return ((double)m_files_processed / m_num_input_files) * 100;
|
||
|
return ((double)(m_files_processed + m_errors.size())
|
||
|
/ m_num_input_files) * 100;
|
||
|
}
|
||
|
|
||
|
void PhotoCompressArchiver::terminate()
|
||
|
{
|
||
|
m_terminate_flag = true;
|
||
|
m_file_list.terminate();
|
||
|
}
|
||
Adding non-verbose mode with "running" status information and progress
bar. Also added terminate function in preparation of adding proper
SIGINT handling.