void logger_c::log(std::string const &message) { try { mm_text_io_c out(new mm_file_io_c(m_file_name.string(), bfs::exists(m_file_name) ? MODE_WRITE : MODE_CREATE)); out.setFilePointer(0, seek_end); out.puts(to_string(get_current_time_millis() - m_log_start) + " " + message + "\n"); } catch (mtx::mm_io::exception &ex) { } }
void mux_dialog::run() { auto &arg_list = static_cast<mmg_dialog *>(GetParent())->get_command_line_args(); opt_file_name.Printf(wxT("%smmg-mkvmerge-options-%d-%d"), get_temp_dir().c_str(), (int)wxGetProcessId(), (int)wxGetUTCTime()); try { const unsigned char utf8_bom[3] = {0xef, 0xbb, 0xbf}; wxFile opt_file{opt_file_name, wxFile::write}; opt_file.Write(utf8_bom, 3); for (size_t i = 1; i < arg_list.Count(); i++) { if (arg_list[i].IsEmpty()) opt_file.Write(wxT("#EMPTY#")); else { auto arg_utf8 = escape(to_utf8(arg_list[i])); opt_file.Write(arg_utf8.c_str(), arg_utf8.length()); } opt_file.Write(wxT("\n")); } } catch (mtx::mm_io::exception &ex) { wxString error; error.Printf(Z("Could not create a temporary file for mkvmerge's command line option called '%s' (error code %d, %s)."), opt_file_name.c_str(), errno, wxUCS(ex.error())); wxMessageBox(error, Z("File creation failed"), wxOK | wxCENTER | wxICON_ERROR); throw 0; } #if defined(SYS_WINDOWS) if (get_windows_version() >= WINDOWS_VERSION_7) { m_taskbar_progress = new taskbar_progress_c(mdlg); m_taskbar_progress->set_state(TBPF_NORMAL); m_taskbar_progress->set_value(0, 100); } #endif // SYS_WINDOWS update_label(Z("Muxing in progress.")); m_start_time = get_current_time_millis(); m_next_remaining_time_update = m_start_time + 8000; m_process = new mux_process{this}; m_pid = wxExecute(wxString::Format(wxT("\"%s\" \"@%s\""), arg_list[0].c_str(), opt_file_name.c_str()), wxEXEC_ASYNC, m_process); if (0 == m_pid) { wxCommandEvent evt(mux_process::event, mux_process::process_terminated); evt.SetInt(2); wxPostEvent(this, evt); } else { m_read_input_timer.SetOwner(this, ID_T_READ_INPUT); m_read_input_timer.Start(100); } ShowModal(); }
logger_c::logger_c(bfs::path const &file_name) : m_file_name(file_name) , m_log_start(get_current_time_millis()) { if (!m_file_name.is_absolute()) m_file_name = bfs::temp_directory_path() / m_file_name; if (bfs::exists(m_file_name)) { boost::system::error_code ec; bfs::remove(m_file_name, ec); } }
void mux_dialog::update_remaining_time() { int64_t now = get_current_time_millis(); if ((0 == m_progress) || (now < m_next_remaining_time_update)) return; int64_t total_time = (now - m_start_time) * 100 / m_progress; int64_t remaining_time = total_time - now + m_start_time; st_remaining_time->SetLabel(wxU(create_minutes_seconds_time_string(static_cast<unsigned int>(remaining_time / 1000)))); m_next_remaining_time_update = now + 1000; }
void scanning_for_playlists_dlg::on_progress_changed(wxCommandEvent &evt) { update_gauge(evt.GetInt()); uint64_t now = get_current_time_millis(); if ((0 == m_progress) || (now < m_next_remaining_time_update)) return; int64_t total_time = (now - m_start_time) * m_max_progress / m_progress; int64_t remaining_time = total_time - now + m_start_time; m_st_remaining_time->SetLabel(wxU(create_minutes_seconds_time_string(static_cast<unsigned int>(remaining_time / 1000)))); m_next_remaining_time_update = now + 500; }
int scanning_for_playlists_dlg::scan() { m_start_time = get_current_time_millis(); m_next_remaining_time_update = m_start_time + 8000; m_scanner->Create(); m_scanner->Run(); ShowModal(); if (m_aborted) return wxID_CANCEL; for (auto &pair : m_scanner->get_output()) parse_output(pair.first, pair.second); brng::sort(m_playlists, [](playlist_file_cptr const &a, playlist_file_cptr const &b) { return a->file_name < b->file_name; }); return wxID_OK; }
int scanning_for_playlists_dlg::scan() { m_start_time = get_current_time_millis(); m_next_remaining_time_update = m_start_time + 8000; m_scanner->Create(); m_scanner->Run(); ShowModal(); #if defined(SYS_WINDOWS) if (m_taskbar_progress) m_taskbar_progress->set_state(TBPF_NOPROGRESS); #endif // SYS_WINDOWS if (m_aborted) return wxID_CANCEL; for (auto &pair : m_scanner->get_output()) parse_output(pair.first, pair.second); brng::sort(m_playlists, [](playlist_file_cptr const &a, playlist_file_cptr const &b) { return a->file_name < b->file_name; }); return wxID_OK; }
mux_dialog::mux_dialog(wxWindow *parent): wxDialog(parent, -1, Z("mkvmerge is running"), wxDefaultPosition, #ifdef SYS_WINDOWS wxSize(700, 560), #else wxSize(700, 520), #endif wxDEFAULT_FRAME_STYLE) #if defined(SYS_WINDOWS) , pid(0) , m_taskbar_progress(NULL) , m_abort_button_changed(false) #endif // SYS_WINDOWS , m_exit_code(0) , m_progress(0) { char c; std::string arg_utf8, line; long value; wxString wx_line, tmp; wxInputStream *out; wxFile *opt_file; uint32_t i; wxArrayString *arg_list; wxBoxSizer *siz_all, *siz_buttons, *siz_line; wxStaticBoxSizer *siz_status, *siz_output; m_window_disabler = new wxWindowDisabler(this); c = 0; siz_status = new wxStaticBoxSizer(new wxStaticBox(this, -1, Z("Status and progress")), wxVERTICAL); st_label = new wxStaticText(this, -1, wxEmptyString); st_remaining_time_label = new wxStaticText(this, -1, Z("Remaining time:")); st_remaining_time = new wxStaticText(this, -1, Z("is being estimated")); siz_line = new wxBoxSizer(wxHORIZONTAL); siz_line->Add(st_label); siz_line->AddSpacer(5); siz_line->Add(st_remaining_time_label); siz_line->AddSpacer(5); siz_line->Add(st_remaining_time); siz_status->Add(siz_line, 0, wxGROW | wxALIGN_LEFT | wxALL, 5); g_progress = new wxGauge(this, -1, 100, wxDefaultPosition, wxSize(250, 15)); siz_status->Add(g_progress, 1, wxALL | wxGROW, 5); siz_output = new wxStaticBoxSizer(new wxStaticBox(this, -1, Z("Output")), wxVERTICAL); siz_output->Add(new wxStaticText(this, -1, Z("mkvmerge output:")), 0, wxALIGN_LEFT | wxALL, 5); tc_output = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxTE_BESTWRAP | wxTE_MULTILINE); siz_output->Add(tc_output, 2, wxGROW | wxALL, 5); siz_output->Add(new wxStaticText(this, -1, Z("Warnings:")), 0, wxALIGN_LEFT | wxALL, 5); tc_warnings = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxTE_BESTWRAP | wxTE_MULTILINE); siz_output->Add(tc_warnings, 1, wxGROW | wxALL, 5); siz_output->Add(new wxStaticText(this, -1, Z("Errors:")), 0, wxALIGN_LEFT | wxALL, 5); tc_errors = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxTE_BESTWRAP | wxTE_MULTILINE); siz_output->Add(tc_errors, 1, wxGROW | wxALL, 5); siz_buttons = new wxBoxSizer(wxHORIZONTAL); siz_buttons->AddStretchSpacer(); b_ok = new wxButton(this, ID_B_MUX_OK, Z("Ok")); b_ok->Enable(false); siz_buttons->Add(b_ok, 0, wxGROW); siz_buttons->AddStretchSpacer(); b_abort = new wxButton(this, ID_B_MUX_ABORT, Z("Abort")); siz_buttons->Add(b_abort, 0, wxGROW); siz_buttons->AddStretchSpacer(); b_save_log = new wxButton(this, ID_B_MUX_SAVELOG, Z("Save log")); siz_buttons->Add(b_save_log, 0, wxGROW); siz_buttons->AddStretchSpacer(); siz_all = new wxBoxSizer(wxVERTICAL); siz_all->Add(siz_status, 0, wxGROW | wxALL, 5); siz_all->Add(siz_output, 1, wxGROW | wxALL, 5); siz_all->Add(siz_buttons, 0, wxGROW | wxALL, 10); SetSizer(siz_all); update_window(Z("Muxing in progress.")); Show(true); process = new mux_process(this); opt_file_name.Printf(wxT("%smmg-mkvmerge-options-%d-%d"), get_temp_dir().c_str(), (int)wxGetProcessId(), (int)wxGetUTCTime()); try { const unsigned char utf8_bom[3] = {0xef, 0xbb, 0xbf}; opt_file = new wxFile(opt_file_name, wxFile::write); opt_file->Write(utf8_bom, 3); } catch (...) { wxString error; error.Printf(Z("Could not create a temporary file for mkvmerge's command line option called '%s' (error code %d, %s)."), opt_file_name.c_str(), errno, wxUCS(strerror(errno))); wxMessageBox(error, Z("File creation failed"), wxOK | wxCENTER | wxICON_ERROR); throw 0; } arg_list = &static_cast<mmg_dialog *>(parent)->get_command_line_args(); for (i = 1; i < arg_list->Count(); i++) { if ((*arg_list)[i].Length() == 0) opt_file->Write(wxT("#EMPTY#")); else { arg_utf8 = escape(wxMB((*arg_list)[i])); opt_file->Write(arg_utf8.c_str(), arg_utf8.length()); } opt_file->Write(wxT("\n")); } delete opt_file; #if defined(SYS_WINDOWS) if (get_windows_version() >= WINDOWS_VERSION_7) { m_taskbar_progress = new taskbar_progress_c(mdlg); m_taskbar_progress->set_state(TBPF_NORMAL); m_taskbar_progress->set_value(0, 100); } #endif // SYS_WINDOWS m_start_time = get_current_time_millis(); m_next_remaining_time_update = m_start_time + 8000; wxString command_line = wxString::Format(wxT("\"%s\" \"@%s\""), (*arg_list)[0].c_str(), opt_file_name.c_str()); pid = wxExecute(command_line, wxEXEC_ASYNC, process); if (0 == pid) { wxLogError(wxT("Execution of '%s' failed."), command_line.c_str()); done(2); return; } out = process->GetInputStream(); line = ""; log = wxEmptyString; while (1) { while (app->Pending()) app->Dispatch(); if (!out->CanRead() && !out->Eof()) { wxMilliSleep(5); continue; } if (!out->Eof()) c = out->GetC(); else c = '\n'; if ((c == '\n') || (c == '\r') || out->Eof()) { wx_line = wxU(line); log += wx_line; if (c != '\r') log += wxT("\n"); if (wx_line.Find(Z("Warning:")) == 0) tc_warnings->AppendText(wx_line + wxT("\n")); else if (wx_line.Find(Z("Error:")) == 0) tc_errors->AppendText(wx_line + wxT("\n")); else if (wx_line.Find(Z("Progress")) == 0) { if (wx_line.Find(wxT("%")) != 0) { wx_line.Remove(wx_line.Find(wxT("%"))); tmp = wx_line.AfterLast(wxT(' ')); tmp.ToLong(&value); if ((value >= 0) && (value <= 100)) update_gauge(value); } } else if (wx_line.Length() > 0) tc_output->AppendText(wx_line + wxT("\n")); line = ""; update_remaining_time(); } else if ((unsigned char)c != 0xff) line += c; if (out->Eof()) break; } }
/** *************************************************************************** * main() ;-) * */ int main(int argc, char * argv[]) { stats_main_start = get_current_time_millis(); int rv = 0; pthread_key_create(&thread_name, NULL); pthread_setspecific(thread_name, (char *)"[MAIN] "); pthread_key_create(&duplicate_path_buffer, NULL); rv = process_args(argc, argv); LOG(L_PROGRESS, "Log level: %s\n", log_level_name[log_level]); // If process_args returns non-zero it means we need to exit right away // with an exit code one less than the returned value. Need to exit via // the DONE section both to free any memory that may have been allocated // already and also to properly return (not exit) from main (see below). if (rv) { rv--; goto DONE; } // If bad --path values given, don't try to process them. Arguably one // could process the good ones (if any) but better to flag the path // error up front instead of spending time doing a partial scan. if (start_path_state == START_PATH_ERROR) { rv = 1; goto DONE; } LOG(L_INFO, "Claimed CPU cores: %d\n", cpu_cores()); max_open_files = get_file_limit() - 10; LOG(L_INFO, "Max open files: %d\n", max_open_files); signal(SIGUSR1, &handle_signal); signal(SIGUSR2, &handle_signal); switch (operation) { case COMMAND_scan: scan(); break; case COMMAND_refresh: operation_refresh(); break; case COMMAND_report: operation_report(); break; case COMMAND_uniques: operation_uniques(); break; case COMMAND_license: show_license(); break; case COMMAND_version: printf(DUPD_VERSION "\n"); break; case COMMAND_dups: operation_dups(); break; case COMMAND_file: operation_file(); break; case COMMAND_ls: operation_ls(); break; case COMMAND_rmsh: operation_shell_script(); break; case COMMAND_validate: rv = operation_validate(); break; case COMMAND_usage: show_usage(); break; case COMMAND_man: show_usage(); break; case COMMAND_help: show_help(); break; case COMMAND_testing: testing(); break; case COMMAND_hash: operation_hash_file(); break; case OPTGEN_NO_COMMAND: show_help(); rv = 1; break; default: // LCOV_EXCL_START printf("error: unknown operation [%d]\n", operation); rv = 1; } // LCOV_EXCL_STOP DONE: if (free_file_path) { free(file_path); } if (free_db_path) { free(db_path); } if (free_cache_db_path) { free(cache_db_path); } if (path_sep_string) { free(path_sep_string); } free_size_tree(); free_size_list(); free_path_block(); free_filecompare(); free_scanlist(); free_start_paths(); free_read_list(); free_dirtree(); free_path_buffer(); stats_time_total = get_current_time_millis() - stats_main_start; LOG(L_PROGRESS, "Total time: %ld ms\n", stats_time_total); if (stats_file != NULL) { save_stats(); } if (log_level >= 0) { if (operation == COMMAND_scan || operation == COMMAND_refresh || operation == COMMAND_license || operation == COMMAND_version || operation == COMMAND_validate || operation == COMMAND_usage || operation == COMMAND_man || operation == COMMAND_help) { if (!strcmp("dev", DUPD_VERSION + strlen(DUPD_VERSION) - 3)) { if (isatty(fileno(stdout))) { fprintf(stdout, "\nNote: This is a development version of dupd (" DUPD_VERSION ") (" GITHASH ")\n"); fprintf(stdout, "May contain known bugs or unstable work in progress!\n"); fprintf(stdout, "If stability is desired, use a release version of dupd.\n"); } } } } // Call return() instead of exit() just to make valgrind mark as // an error any reachable allocations. That makes them show up // when running the tests. return(rv); }
int main(void) { kii_error_code_t error_code; error_code = kii_global_init(); if (error_code != KIIE_OK) { goto END; } // Initialize application config_t* app_info = NULL; char* app_id = NULL; char* app_key = NULL; char* site_url = NULL; config_error_code_t conf_error_info = config_load("appinfo.json", &app_info); config_error_code_t read_error_appid = config_get(app_info, "AppID", &app_id); config_error_code_t read_error_appkey = config_get(app_info, "AppKey", &app_key); config_error_code_t read_error_site = config_get(app_info, "Site", &site_url); if (conf_error_info || read_error_appid || read_error_appkey || read_error_site) { goto CLEANUP_CONFIG_APPINFO; } // KiiThing config_t* credential = NULL; char* thing_id = NULL; char* thing_token = NULL; config_error_code_t conf_error_credential = config_load("credential.json", &credential); config_error_code_t read_error_id = config_get(credential, "ThingID", &thing_id); config_error_code_t read_error_token = config_get(credential, "ThingToken", &thing_token); if (conf_error_credential || read_error_id || read_error_token) { goto CLEANUP_CONFIG_CREDENTIAL; } kii_app_t app = kii_init_app(app_id, app_key, site_url); kii_thing_t kii_thing = kii_thing_deserialize(thing_id); // Initialize target kii bucket const char* bucket_name = "temperature"; kii_bucket_t kii_bucket = kii_init_thing_bucket(kii_thing, bucket_name); // Get temperature float temp = w1_get_temperature(); // Create json json_error_t json_error; json_t* json = NULL; printf("Current time: %lld\n", get_current_time_millis()); json = json_pack("{s:f, s:I}", "temperature", temp, "time", (json_int_t)(get_current_time_millis())); if (json == NULL) { goto CLEANUP_KIIBUCKET; } // Upload json as kii object kii_char_t* object_id = NULL; kii_char_t* etag = NULL; error_code = kii_create_new_object(app, thing_token, kii_bucket, json, &object_id, &etag); if (error_code != KIIE_OK) { goto CLEANUP_JSON; } printf("Bucket name: %s\n", bucket_name); printf("Temperature: %f\n", temp); printf("Object ID: %s\n", object_id); printf("ETag: %s\n", etag); kii_dispose_kii_char(object_id); kii_dispose_kii_char(etag); CLEANUP_JSON: json_decref(json); CLEANUP_KIIBUCKET: kii_dispose_bucket(kii_bucket); CLEANUP_KIITHING: kii_dispose_thing(kii_thing); kii_dispose_app(app); CLEANUP_CONFIG_CREDENTIAL: config_decref(credential); CLEANUP_CONFIG_APPINFO: config_decref(app_info); CLEANUP: kii_global_cleanup(); END: return EXIT_SUCCESS; }