bool CTestRegApp::TestApp_Init(void) { // Initialize the C logging API with default MT locking imlementation NcbiLog_InitMT(GetAppName().c_str()); // Set output to files in current directory NcbiLog_SetDestination(eNcbiLog_Cwd); // Start application NcbiLog_AppStart(NULL); NcbiLog_AppRun(); return true; }
void CTest::RunTest(CTempString name, FTestCase testcase) { cout << name; string basename = "clog-test." + (string)name; string log_path = basename + ".out"; // Redirect stderr to file ::fflush(stderr); int saved_stderr = ::dup(fileno(stderr)); if (!::freopen(log_path.c_str(), "w", stderr)) { _TROUBLE; } // Initialize API (single-threaded mode) NcbiLogP_ReInit(); NcbiLog_InitST(GetAppName().c_str()); // Set CLog to use stderr for output, that will be redirected // to the file specified above. NcbiLog_SetDestination(eNcbiLog_Stderr); NcbiLog_SetPostLevel(eNcbiLog_Info); // Default initialization NcbiLog_SetHost("TESTHOST"); // Run specified test case testcase(); // Done logging NcbiLog_Destroy(); // Restore original stderr ::fflush(stderr); if (::dup2(saved_stderr, fileno(stderr)) < 0) { _TROUBLE; } close(saved_stderr); clearerr(stderr); // Compare output string template_path = CDir::ConcatPath(kTemplatesDir, basename + ".tpl"); try { CRegexpTemplateTester tester(CRegexpTemplateTester::fSkipEmptyTemplateLines); tester.Compare(log_path, template_path); cout << ": OK" << endl; m_Passed++; } catch (CRegexpTemplateTesterException& e) { cout << e.what() << endl; m_Failed++; } return; }
int main(int argc, const char* argv[] /*, const char* envp[]*/) { TNcbiLog_MTLock mt_lock = NcbiLog_MTLock_Create(NULL, Test_MT_Handler); /* Initialize logging API */ NcbiLog_Init(argv[0], mt_lock, eNcbiLog_MT_TakeOwnership); /* Or, NcbiLog_InitMT(argv[0]); -- use default MT handler as above. NcbiLog_InitST(argv[0]); -- only for single-threaded applications */ /* Set logging destination */ NcbiLog_SetDestination(eNcbiLog_Stdout); /* Or, NcbiLog_SetDestination(eNcbiLog_Default); -- default, can be skipped NcbiLog_SetDestination(eNcbiLog_Stdlog); NcbiLog_SetDestination(eNcbiLog_Stdout); NcbiLog_SetDestination(eNcbiLog_Stderr); NcbiLog_SetDestination(eNcbiLog_Disable); */ /* Set host name NcbiLog_SetHost("SOMEHOSTNAME"); */ /* Set log_site NcbiLog_SetLogSite(""); */ /* Set process/thread ID NcbiLog_SetProcessId(pid); NcbiLog_SetThreadId(tid); */ /* Start application */ NcbiLog_AppStart(argv); NcbiLog_AppRun(); /* Standard messages */ {{ NcbiLog_SetPostLevel(eNcbiLog_Warning); NcbiLog_Trace("Message"); NcbiLog_Warning("Message"); NcbiLog_Error("Message"); NcbiLog_Critical("Message"); /* NcbiLog_Fatal("Message"); */ }} /* Standard messages with user provided time */ {{ time_t timer; NcbiLog_SetPostLevel(eNcbiLog_Trace); timer = time(0); NcbiLog_SetTime(timer, 0); NcbiLog_Trace("Use user provided time (1)"); NcbiLog_Trace("Use user provided time (2)"); NcbiLog_SetTime(0,0); NcbiLog_Trace("Use system local time"); }} /* Request without parameters */ {{ NcbiLog_ReqStart(NULL); NcbiLog_ReqStop(200, 1, 2); }} /* Message printed between requests */ {{ NcbiLog_Error("Message printed between requests"); }} /* Request without parameters -- new ID */ {{ NcbiLog_SetRequestId(10); NcbiLog_ReqStart(NULL); NcbiLog_ReqStop(403, 5, 6); }} /* Request with parameters */ {{ static const SNcbiLog_Param params[] = { { "k1", "v1" }, { "k2", "v2" }, { "", "v3" }, { "k4", "" }, { "", "" }, { "k5", "v5" }, { NULL, NULL } }; NcbiLog_SetSession("session name"); NcbiLog_SetClient("192.168.1.1"); NcbiLog_ReqStart(params); NcbiLog_ReqRun(); NcbiLog_ReqStop(500, 3, 4); }} /* Extra & performance logging */ {{ double timespan = 1.2345678; static const SNcbiLog_Param params[] = { { "resource", "test" }, { "key", "value" }, { NULL, NULL } }; NcbiLog_Extra(params); NcbiLog_Perf(200, timespan, params); }} /* Stop application with exit code 0 */ NcbiLog_AppStop(0); /* Deinitialize logging API */ NcbiLog_Destroy(); return 0; }