📄 ncbiapp.cpp
字号:
NCBI_THROW(CAppException, eLoadConfig, "Registry data cannot be loaded: " + string(e.what())); } // Setup the debugging features from the config file. // Don't call till after LoadConfig() // NOTE: this will override environment variables, // except DIAG_POST_LEVEL which is Set*Fixed*. HonorDebugSettings(); // Call: Init() + Run() + Exit() int exit_code = 1; try { // Init application try { Init(); // If the app still has no arg description - provide default one if (!m_DisableArgDesc && !m_ArgDesc.get()) { auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions); arg_desc->SetUsageContext (GetArguments().GetProgramBasename(), "This program has no mandatory arguments"); SetupArgDescriptions(arg_desc.release()); } } catch (CArgHelpException& ) { if ( !m_DisableArgDesc ) { if ((m_HideArgs & fHideHelp) != 0) { if (m_ArgDesc->Exist("h")) { m_ArgDesc->Delete("h"); } } if ((m_HideArgs & fHideLogfile) == 0 && !m_ArgDesc->Exist(s_ArgLogFile + 1)) { m_ArgDesc->AddOptionalKey (s_ArgLogFile+1, "File_Name", "File to which the program log should be redirected", CArgDescriptions::eOutputFile); } if ((m_HideArgs & fHideConffile) == 0 && !m_ArgDesc->Exist(s_ArgCfgFile + 1)) { m_ArgDesc->AddOptionalKey (s_ArgCfgFile + 1, "File_Name", "Program's configuration (registry) data file", CArgDescriptions::eInputFile); } if ((m_HideArgs & fHideVersion) == 0 && !m_ArgDesc->Exist(s_ArgVersion + 1)) { m_ArgDesc->AddFlag (s_ArgVersion + 1, "Print version number; ignore other arguments"); } } // Print USAGE string str; LOG_POST(m_ArgDesc->PrintUsage(str)); exit_code = 0; } catch (CArgException& e) { NCBI_RETHROW_SAME(e, "Application's initialization failed"); } catch (CException& e) { NCBI_REPORT_EXCEPTION("Application's initialization failed", e); exit_code = -2; } catch (exception& e) { ERR_POST("Application's initialization failed: " << e.what()); exit_code = -2; } // Run application if (exit_code == 1) { try { exit_code = Run(); } catch (CArgException& e) { NCBI_RETHROW_SAME(e, "Application's execution failed"); } catch (CException& e) { NCBI_REPORT_EXCEPTION("Application's execution failed", e); exit_code = -3; } catch (exception& e) { ERR_POST("Application's execution failed: " << e.what()); exit_code = -3; } } // Close application try { Exit(); } catch (CArgException& e) { NCBI_RETHROW_SAME(e, "Application's cleanup failed"); } catch (CException& e) { NCBI_REPORT_EXCEPTION("Application's cleanup failed", e); } catch (exception& e) { ERR_POST("Application's cleanup failed: "<< e.what()); } } catch (CArgException& e) { // Print USAGE and the exception error message string str; LOG_POST(string(72, '=')); if ( m_ArgDesc.get() ) { LOG_POST(m_ArgDesc->PrintUsage(str) << string(72, '=')); } NCBI_REPORT_EXCEPTION("", e); exit_code = -1; } catch (...) { // MSVC++ 6.0 in Debug mode does not call destructors when // unwinding the stack unless the exception is caught at least // somewhere. ERR_POST(Warning << "Application has thrown an exception of unknown type"); throw; } // Exit return exit_code;}void CNcbiApplication::SetEnvironment(const string& name, const string& value){ SetEnvironment().Set(name, value);}void CNcbiApplication::SetVersion(const CVersionInfo& version){ m_Version.reset(new CVersionInfo(version));}CVersionInfo CNcbiApplication::GetVersion(void){ return *m_Version;}void CNcbiApplication::SetupArgDescriptions(CArgDescriptions* arg_desc){ m_ArgDesc.reset(arg_desc); if ( arg_desc ) { m_Args.reset(arg_desc->CreateArgs(GetArguments())); } else { m_Args.reset(); }}bool CNcbiApplication::SetupDiag(EAppDiagStream diag){ // Setup diagnostic stream switch ( diag ) { case eDS_ToStdout: { SetDiagStream(&NcbiCout); break; } case eDS_ToStderr: { SetDiagStream(&NcbiCerr); break; } case eDS_ToStdlog: { // open log.file string log = m_Arguments->GetProgramName() + ".log"; auto_ptr<CNcbiOfstream> os(new CNcbiOfstream(log.c_str())); if ( !os->good() ) { _TRACE("CNcbiApplication() -- cannot open log file: " << log); return false; } _TRACE("CNcbiApplication() -- opened log file: " << log); // (re)direct the global diagnostics to the log.file CNcbiOfstream* os_log = os.release(); SetDiagStream(os_log, true, s_DiagToStdlog_Cleanup, (void*) os_log); break; } case eDS_ToMemory: { // direct global diagnostics to the memory-resident output stream if ( !m_DiagStream.get() ) { m_DiagStream.reset(new CNcbiOstrstream); } SetDiagStream(m_DiagStream.get()); break; } case eDS_Disable: { SetDiagStream(0); break; } case eDS_User: { // dont change current diag.stream break; } case eDS_AppSpecific: { return SetupDiag_AppSpecific(); } case eDS_Default: { if ( !IsSetDiagHandler() ) { return CNcbiApplication::SetupDiag(eDS_AppSpecific); } // else eDS_User -- dont change current diag.stream break; } default: { _ASSERT(0); break; } } // switch ( diag ) return true;}bool CNcbiApplication::SetupDiag_AppSpecific(void){ return SetupDiag(eDS_ToStderr);}bool CNcbiApplication::LoadConfig(CNcbiRegistry& reg, const string* conf, CNcbiRegistry::TFlags reg_flags){ string basename (m_Arguments->GetProgramBasename(eIgnoreLinks)); string basename2(m_Arguments->GetProgramBasename(eFollowLinks)); CMetaRegistry::SEntry entry; if ( !conf ) { return false; } else if (conf->empty()) { entry = CMetaRegistry::Load(basename, CMetaRegistry::eName_Ini, CMetaRegistry::fDontOwn, reg_flags, ®); if ( !entry.registry && basename2 != basename ) { entry = CMetaRegistry::Load(basename2, CMetaRegistry::eName_Ini, CMetaRegistry::fDontOwn, reg_flags, ®); } } else { entry = CMetaRegistry::Load(*conf, CMetaRegistry::eName_AsIs, CMetaRegistry::fDontOwn, reg_flags, ®); } if ( !entry.registry ) { // failed; complain as appropriate string dir; CDirEntry::SplitPath(*conf, &dir, 0, 0); if (dir.empty()) { ERR_POST(Warning << "Registry file of application \"" << basename << "\" is not found"); } else { NCBI_THROW(CAppException, eNoRegistry, "Registry file \"" + *conf + "\" cannot be opened"); } return false; } else if (entry.registry != ®) { // should be impossible with new CMetaRegistry interface... if (® == m_Config && reg.Empty()) { if (m_OwnsConfig) { delete m_Config; } m_Config = entry.registry; m_OwnsConfig = false; } else { // copy into reg CNcbiStrstream str; entry.registry->Write(str); str.seekg(0); reg.Read(str); } } return true;}bool CNcbiApplication::LoadConfig(CNcbiRegistry& reg, const string* conf){ return LoadConfig(reg, conf, 0);}void CNcbiApplication::DisableArgDescriptions(void){ m_DisableArgDesc = true;}void CNcbiApplication::HideStdArgs(THideStdArgs hide_mask){ m_HideArgs = hide_mask;}void CNcbiApplication::SetStdioFlags(TStdioSetupFlags stdio_flags){ // do not call this function more than once // and from places other than App constructor _ASSERT(m_StdioFlags == 0); m_StdioFlags = stdio_flags;}void CNcbiApplication::x_SetupStdio(void){ if ((m_StdioFlags & fDefault_SyncWithStdio) == 0) { // SUN WorkShop STL stream library has significant performance loss // when sync_with_stdio is true (default), // so we turn off sync_with_stdio here. IOS_BASE::sync_with_stdio(false); } if ((m_StdioFlags & fDefault_CinBufferSize) == 0#ifdef NCBI_OS_UNIX && !isatty(0)#endif ) {#if defined(NCBI_COMPILER_GCC)# if NCBI_COMPILER_VERSION >= 300 _ASSERT(!m_CinBuffer); // Ugly work around for G++/Solaris C RTL interaction const size_t kCinBufSize = 5120; m_CinBuffer = new char[kCinBufSize]; cin.rdbuf()->pubsetbuf(m_CinBuffer, kCinBufSize);# endif#endif }}void CNcbiApplication::SetProgramDisplayName(const string& app_name){ m_ProgramDisplayName = app_name;}string CNcbiApplication::FindProgramExecutablePath(int /*argc*/, const char* const* argv){ string ret_val;#if defined (NCBI_OS_DARWIN) && defined (NCBI_COMPILER_METROWERKS) // We don't want to impose a dependency on Carbon when building // with GCC, since linking the framework *at all* makes remote // execution impossible. :-/ OSErr err; ProcessSerialNumber psn; FSRef fsRef; char filePath[1024]; err = GetCurrentProcess(&psn); if (err == noErr) { err = GetProcessBundleLocation(&psn, &fsRef); if (err == noErr) { err = FSRefMakePath(&fsRef, (UInt8*) filePath, sizeof(filePath)); } } ret_val = filePath; #elif defined(NCBI_OS_MSWIN) || defined(NCBI_OS_UNIX) string app_path = argv[0];# if defined (NCBI_OS_MSWIN) // MS Windows: Try more accurate method of detection try { // Load PSAPI dynamic library -- it should exists on MS-Win NT/2000/XP CDll dll_psapi("psapi.dll", CDll::eLoadNow, CDll::eAutoUnload); // Get function entry-point from DLL BOOL (STDMETHODCALLTYPE FAR * dllEnumProcessModules) (HANDLE hProcess, // handle to process HMODULE *lphModule, // array of module handles DWORD cb, // size of array LPDWORD lpcbNeeded // number of bytes required ) = NULL; dllEnumProcessModules = dll_psapi.GetEntryPoint_Func("EnumProcessModules", &dllEnumProcessModules); if ( !dllEnumProcessModules ) { NCBI_THROW(CException, eUnknown, kEmptyStr); } // Find executable file in the midst of all loaded modules HANDLE process = GetCurrentProcess(); HMODULE module = 0; DWORD needed = 0; // Get first module of current process (it should be .exe file) if ( dllEnumProcessModules(process, &module, sizeof(HMODULE), &needed) ) { if ( needed && module ) { char buf[MAX_PATH + 1]; DWORD ncount = GetModuleFileName(module, buf, MAX_PATH); if ( ncount ) { buf[ncount] = 0; return buf; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -