Skip to content

Commit

Permalink
Improve progress reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed May 12, 2024
1 parent c9f8b66 commit a19325c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/Archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ bool Archive::file_ensure_hashes(uint64_t idx, size_t detector_id, int hashtypes
return false;
}

auto progress = Progress::Message("computing hashes for " + name + "/" + file.name);

if (detector_id == 0) {
Hashes hashes;
hashes.add_types(Hashes::TYPE_ALL);
Expand Down
19 changes: 12 additions & 7 deletions src/CkmameCache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ void CkmameCache::ensure_extra_maps() {
switch ((name_type(file))) {
case NAME_IMAGES:
case NAME_ZIP: {
Progress::set_message("currently scanning '" + file + "'");

Progress::push_message("scanning '" + file + "'");
auto a = Archive::open(file, entry.filetype, FILE_SUPERFLUOUS, 0);
// TODO: loose: add loose files in directory
Progress::pop_message();
break;
}

Expand All @@ -203,11 +203,12 @@ void CkmameCache::ensure_extra_maps() {
}
}

Progress::set_message("currently scanning '" + configuration.rom_directory + "'");
Progress::push_message("scanning '" + configuration.rom_directory + "'");

auto filetype = configuration.roms_zipped ? TYPE_DISK : TYPE_ROM;
auto a = Archive::open_toplevel(configuration.rom_directory, filetype, FILE_SUPERFLUOUS, 0);

Progress::pop_message();

for (const auto &directory : configuration.extra_directories) {
enter_dir_in_map_and_list(extra_delete_list, directory, FILE_EXTRA);
Expand Down Expand Up @@ -273,20 +274,22 @@ bool CkmameCache::enter_dir_in_map_and_list_unzipped(const DeleteListPtr &list,
continue;
}
if (std::filesystem::is_directory(filepath)) {
Progress::set_message("currently scanning '" + filepath.string() + "'");
Progress::push_message("scanning '" + filepath.string() + "'");
auto a = Archive::open(filepath, TYPE_ROM, where, 0);
if (a) {
list->add(a.get());
a->close();
}
Progress::pop_message();
}
}

Progress::set_message("currently scanning '" + directory_name + "'");
Progress::push_message("scanning '" + directory_name + "'");
auto a = Archive::open_toplevel(directory_name, TYPE_ROM, where, 0);
if (a) {
list->add(a.get());
}
Progress::pop_message();
}
catch (...) {
return false;
Expand All @@ -305,11 +308,12 @@ bool CkmameCache::enter_dir_in_map_and_list_zipped(const DeleteListPtr &list, co
enter_file_in_map_and_list(list, filepath, where);
}

Progress::set_message("currently scanning '" + dir_name + "'");
Progress::push_message("scanning '" + dir_name + "'");
auto a = Archive::open_toplevel(dir_name, TYPE_DISK, where, 0);
if (a) {
list->add(a.get());
}
Progress::pop_message();
}
catch (...) {
return false;
Expand All @@ -325,12 +329,13 @@ bool CkmameCache::enter_file_in_map_and_list(const DeleteListPtr &list, const st
switch ((nt = name_type(name))) {
case NAME_IMAGES:
case NAME_ZIP: {
Progress::set_message("currently scanning '" + name + "'");
Progress::push_message("scanning '" + name + "'");
auto a = Archive::open(name, nt == NAME_ZIP ? TYPE_ROM : TYPE_DISK, where, 0);
if (a) {
list->add(a.get());
a->close();
}
Progress::pop_message();
break;
}

Expand Down
13 changes: 9 additions & 4 deletions src/CkmameDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ void CkmameDB::find_file(filetype_t filetype, size_t detector_id, const FileData
}

void CkmameDB::refresh() {
auto progress = Progress::Message("refreshing directory '" + directory + "'");
if (configuration.roms_zipped) {
refresh_zipped();
}
Expand All @@ -454,17 +455,19 @@ void CkmameDB::refresh_unzipped() {
continue;
}
if (std::filesystem::is_directory(filepath)) {
Progress::set_message("currently scanning '" + filepath.string() + "'");
Progress::push_message("scanning directory '" + filepath.string() + "'");
auto a = Archive::open(filepath, TYPE_ROM, where, 0);
a->close();
Progress::pop_message();
}
}

Progress::set_message("currently scanning '" + directory + "'");
Progress::push_message("scanning loose files in '" + directory + "'");
auto a = Archive::open_toplevel(directory, TYPE_ROM, where, 0);
if (a) {
a->close();
}
Progress::pop_message();
}
catch (...) {}
}
Expand All @@ -480,11 +483,12 @@ void CkmameDB::refresh_zipped() {
switch ((nt = name_type(filepath))) {
case NAME_IMAGES:
case NAME_ZIP: {
Progress::set_message("currently scanning '" + filepath.string() + "'");
Progress::push_message("scanning archive '" + filepath.string() + "'");
auto a = Archive::open(filepath, nt == NAME_ZIP ? TYPE_ROM : TYPE_DISK, where, 0);
if (a) {
a->close();
}
Progress::pop_message();
break;
}

Expand All @@ -495,11 +499,12 @@ void CkmameDB::refresh_zipped() {
}
}

Progress::set_message("currently scanning '" + directory + "'");
Progress::push_message("scanning loose files in '" + directory + "'");
auto a = Archive::open_toplevel(directory, TYPE_DISK, where, 0);
if (a) {
a->close();
}
Progress::pop_message();
}
catch (...) {
}
Expand Down
38 changes: 29 additions & 9 deletions src/Progress.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,61 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "globals.h"
#include "ProgramName.h"

std::string Progress::current_message;
std::vector<std::string> Progress::messages;
volatile bool Progress::siginfo_caught = false;
bool Progress::trace = false;

void Progress::set_message(std::string message) {
current_message = std::move(message);
void Progress::push_message(std::string message) {
messages.emplace_back(std::move(message));

if (trace || siginfo_caught) {
print_message();
print_message(true);
}
}

void Progress::pop_message() {
if (messages.empty()) {
// TODO: warning?
return;
}
if (trace){
print_message(false);
}
messages.pop_back();
}

void Progress::sig_handler(int signal) {
#ifdef SIGINFO
if (signal == SIGINFO) {
siginfo_caught = true;
}
#endif
}
void Progress::print_message() {
if (current_message.empty()) {
std::cout << "no progress available" << std::endl;
void Progress::print_message(bool starting) {
if (!trace && !starting) {
return;
}

if (trace) {
if (messages.empty()) {
return;
}
// C++ 20:
// std::cout << std::format("%Y-%m-%d %H:%M:%S ", std::chrono::system_clock::now());
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << std::put_time(std::localtime(&now), "%Y-%m-%d %H:%M:%S ");
std::cout << std::put_time(std::localtime(&now), "%Y-%m-%d %H:%M:%S ") << (starting ? "start " : "done ");
}
else {
std::cout << ProgramName::get() << ": ";
if (messages.empty()) {
std::cout << "no progress available" << std::endl;
return;
}
else {
std::cout << "currently ";
}
}
std::cout << current_message;
std::cout << messages.back();
if (!configuration.set.empty()) {
std::cout << " in set " << configuration.set;
}
Expand Down
14 changes: 10 additions & 4 deletions src/Progress.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,26 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class Progress {
public:
static void set_message(std::string message);
static void update() { if (siginfo_caught) {print_message();}}
class Message {
public:
explicit Message(std::string message) {Progress::push_message(std::move(message));}
~Message() {Progress::pop_message();}
};
static void enable();
static void push_message(std::string message);
static void pop_message();
static void update() { if (siginfo_caught) {print_message(true);}}

static bool trace;

private:
static void print_message();
static void print_message(bool starting);

static void sig_handler(int sig);

static volatile bool siginfo_caught;

static std::string current_message;
static std::vector<std::string> messages;
};

#endif // PROGRESS_H
4 changes: 3 additions & 1 deletion src/Tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void Tree::traverse() {
void Tree::traverse_internal(GameArchives *ancestor_archives) {
GameArchives archives[] = { GameArchives(), ancestor_archives[0], ancestor_archives[1] };

Progress::set_message("currently checking " + name);
Progress::push_message("checking " + name);

auto flags = check ? ARCHIVE_FL_CREATE : 0;

Expand All @@ -138,6 +138,8 @@ void Tree::traverse_internal(GameArchives *ancestor_archives) {
process(archives);
}

Progress::pop_message();

for (const auto &it : children) {
it.second->traverse_internal(archives);
}
Expand Down

0 comments on commit a19325c

Please sign in to comment.