📄 osutils.cxx
字号:
static unsigned PTraceBlockIndentLevel = 0;static PTimeInterval ApplicationStartTick = PTimer::Tick();void PTrace::SetStream(ostream * s){#ifndef __NUCLEUS_PLUS__ PTraceStream = s != NULL ? s : &cerr;#else PTraceStream = s;#endif}void PTrace::SetOptions(unsigned options){ PTraceOptions |= options;}void PTrace::ClearOptions(unsigned options){ PTraceOptions &= ~options;}unsigned PTrace::GetOptions(){ return PTraceOptions;}void PTrace::SetLevel(unsigned level){ PTraceLevelThreshold = level;}unsigned PTrace::GetLevel(){ return PTraceLevelThreshold;}BOOL PTrace::CanTrace(unsigned level){ return level <= PTraceLevelThreshold;}static PMutex & PTraceMutex(){ static PMutex mutex; return mutex;}ostream & PTrace::Begin(unsigned level, const char * fileName, int lineNum){ PTraceMutex().Wait(); if (level == UINT_MAX) return *PTraceStream; if ((PTraceOptions&SystemLogStream) != 0) { unsigned lvl = level+PSystemLog::Warning; if (lvl >= PSystemLog::NumLogLevels) lvl = PSystemLog::NumLogLevels-1; ((PSystemLog*)PTraceStream)->SetLevel((PSystemLog::Level)lvl); } else { if ((PTraceOptions&DateAndTime) != 0) { PTime now; *PTraceStream << now.AsString("yyyy/MM/dd hh:mm:ss\t"); } if ((PTraceOptions&Timestamp) != 0) *PTraceStream << setprecision(3) << setw(10) << (PTimer::Tick()-ApplicationStartTick) << '\t'; if ((PTraceOptions&Thread) != 0) { PThread * thread = PThread::Current(); if (thread == NULL) *PTraceStream << setw(23) << "<<unknown>>"; else { PString name = thread->GetThreadName(); if (!name) *PTraceStream << setw(23) << name.Left(23); else { name = thread->GetClass(); if ((PTraceOptions&ThreadAddress) != 0) *PTraceStream << setw(23) << name.Left(23); else *PTraceStream << setw(15) << name.Left(15) << ':' << hex << setfill('0') << setw(7) << (unsigned)thread << dec << setfill(' '); } } *PTraceStream << '\t'; } if ((PTraceOptions&ThreadAddress) != 0) *PTraceStream << hex << setfill('0') << setw(7) << (unsigned)PThread::Current() << dec << setfill(' ') << '\t'; } if ((PTraceOptions&TraceLevel) != 0) *PTraceStream << level << '\t'; if ((PTraceOptions&FileAndLine) != 0 && fileName != NULL) { const char * file = strrchr(fileName, '/'); if (file != NULL) file++; else { file = strrchr(fileName, '\\'); if (file != NULL) file++; else file = fileName; } *PTraceStream << setw(16) << file << '(' << lineNum << ")\t"; } return *PTraceStream;}ostream & PTrace::End(ostream & s){ if (s.rdbuf()->out_waiting() > 0) { if ((PTraceOptions&SystemLogStream) != 0) s.flush(); else s << endl; } PTraceMutex().Signal(); return s;}PTrace::Block::Block(const char * fileName, int lineNum, const char * traceName){ file = fileName; line = lineNum; name = traceName; PTraceBlockIndentLevel += 2; if ((PTraceOptions&Blocks) != 0) { ostream & s = PTrace::Begin(1, file, line); for (unsigned i = 0; i < PTraceBlockIndentLevel; i++) s << '='; s << "> " << name << PTrace::End; }}PTrace::Block::~Block(){ if ((PTraceOptions&Blocks) != 0) { ostream & s = PTrace::Begin(1, file, line); s << '<'; for (unsigned i = 0; i < PTraceBlockIndentLevel; i++) s << '='; s << ' ' << name << PTrace::End; } PTraceBlockIndentLevel -= 2;}///////////////////////////////////////////////////////////////////////////////// PDirectoryvoid PDirectory::CloneContents(const PDirectory * d){ CopyContents(*d);}///////////////////////////////////////////////////////////////////////////////// PTimerPTimer::PTimer(long millisecs, int seconds, int minutes, int hours, int days) : resetTime(millisecs, seconds, minutes, hours, days){ state = Stopped; timeoutThread = NULL; StartRunning(TRUE);}PTimer::PTimer(const PTimeInterval & time) : resetTime(time){ state = Stopped; timeoutThread = NULL; StartRunning(TRUE);}PTimer & PTimer::operator=(DWORD milliseconds){ resetTime = (PInt64)milliseconds; StartRunning(oneshot); return *this;}PTimer & PTimer::operator=(const PTimeInterval & time){ resetTime = time; StartRunning(oneshot); return *this;}PTimer::~PTimer(){ PAssert(timeoutThread == NULL || PThread::Current() != timeoutThread, "Timer destroyed in OnTimeout()"); if (IsRunning()) PProcess::Current().GetTimerList()->RemoveTimer(this);}void PTimer::RunContinuous(const PTimeInterval & time){ resetTime = time; StartRunning(FALSE);}void PTimer::StartRunning(BOOL once){ if (IsRunning() && timeoutThread == NULL) PProcess::Current().GetTimerList()->RemoveTimer(this); PTimeInterval::operator=(resetTime); oneshot = once; state = (*this) != 0 ? Starting : Stopped; if (IsRunning()) { if (timeoutThread == NULL) PProcess::Current().GetTimerList()->AppendTimer(this);#if defined(P_PLATFORM_HAS_THREADS) else PProcess::Current().SignalTimerChange();#endif }}void PTimer::Stop(){ if (IsRunning() && timeoutThread == NULL) PProcess::Current().GetTimerList()->RemoveTimer(this); state = Stopped; SetInterval(0);}void PTimer::Pause(){ if (IsRunning()) { if (timeoutThread == NULL) PProcess::Current().GetTimerList()->RemoveTimer(this); state = Paused; }}void PTimer::Resume(){ if (state == Paused) { if (timeoutThread == NULL) PProcess::Current().GetTimerList()->AppendTimer(this); state = Starting; }}void PTimer::OnTimeout(){ if (!callback.IsNULL()) callback(*this, IsRunning());}BOOL PTimer::Process(const PTimeInterval & delta, PTimeInterval & minTimeLeft){ if (state == Starting) { state = Running; if (resetTime < minTimeLeft) minTimeLeft = resetTime; return FALSE; } operator-=(delta); if (milliseconds > 0) { if (milliseconds < minTimeLeft.GetMilliSeconds()) minTimeLeft = milliseconds; return FALSE; } timeoutThread = PThread::Current(); if (oneshot) { operator=(PTimeInterval(0)); state = Stopped; } else { operator=(resetTime); if (resetTime < minTimeLeft) minTimeLeft = resetTime; } return TRUE;}///////////////////////////////////////////////////////////////////////////////// PTimerListPTimerList::PTimerList(){ DisallowDeleteObjects();}void PTimerList::AppendTimer(PTimer * timer){ mutex.Wait(); PInternalTimerList::InsertAt(0, timer); mutex.Signal();#if defined(P_PLATFORM_HAS_THREADS) PProcess::Current().SignalTimerChange();#endif}void PTimerList::RemoveTimer(PTimer * timer){ mutex.Wait(); PInternalTimerList::Remove(timer); mutex.Signal();#if defined(P_PLATFORM_HAS_THREADS) PProcess::Current().SignalTimerChange();#endif}PTimeInterval PTimerList::Process(){ PINDEX i; PTimeInterval minTimeLeft = PMaxTimeInterval; PInternalTimerList timeouts; timeouts.DisallowDeleteObjects(); mutex.Wait(); PTimeInterval now = PTimer::Tick(); PTimeInterval sampleTime; if (lastSample == 0) sampleTime = 0; else { sampleTime = now - lastSample; if (now < lastSample) sampleTime += PMaxTimeInterval; } lastSample = now; for (i = 0; i < GetSize(); i++) if ((*this)[i].Process(sampleTime, minTimeLeft)) timeouts.Append(RemoveAt(i--)); mutex.Signal(); for (i = 0; i < timeouts.GetSize(); i++) timeouts[i].OnTimeout(); mutex.Wait(); for (i = 0; i < timeouts.GetSize(); i++) { timeouts[i].timeoutThread = NULL; if (timeouts[i].IsRunning()) Append(timeouts.GetAt(i)); } mutex.Signal(); return minTimeLeft;}///////////////////////////////////////////////////////////////////////////////// PArgListPArgList::PArgList(const char * theArgStr, const char * theArgumentSpec, BOOL optionsBeforeParams){ // get the program arguments if (theArgStr != NULL) SetArgs(theArgStr); // if we got an argument spec - so process them if (theArgumentSpec != NULL) Parse(theArgumentSpec, optionsBeforeParams);}PArgList::PArgList(const PString & theArgStr, const char * argumentSpecPtr, BOOL optionsBeforeParams){ // get the program arguments SetArgs(theArgStr); // if we got an argument spec - so process them if (argumentSpecPtr != NULL) Parse(argumentSpecPtr, optionsBeforeParams);}PArgList::PArgList(const PString & theArgStr, const PString & argumentSpecStr, BOOL optionsBeforeParams){ // get the program arguments SetArgs(theArgStr); // if we got an argument spec - so process them Parse(argumentSpecStr, optionsBeforeParams);}PArgList::PArgList(int theArgc, char ** theArgv, const char * theArgumentSpec, BOOL optionsBeforeParams){ // get the program arguments SetArgs(theArgc, theArgv); // if we got an argument spec - so process them if (theArgumentSpec != NULL) Parse(theArgumentSpec, optionsBeforeParams);}PArgList::PArgList(int theArgc, char ** theArgv, const PString & theArgumentSpec, BOOL optionsBeforeParams){ // get the program name and path SetArgs(theArgc, theArgv); // we got an argument spec - so process them Parse(theArgumentSpec, optionsBeforeParams);}void PArgList::SetArgs(const PString & argStr){ argumentArray.SetSize(0); const char * str = argStr; for (;;) { while (isspace(*str)) // Skip leading whitespace str++; if (*str == '\0') break; PString & arg = argumentArray[argumentArray.GetSize()]; while (*str != '\0' && !isspace(*str)) { switch (*str) { case '"' : str++; while (*str != '\0' && *str != '"') arg += *str++; if (*str != '\0') str++; break; case '\'' : str++; while (*str != '\0' && *str != '\'') arg += *str++; if (*str != '\0') str++; break; default : if (str[0] == '\\' && str[1] != '\0') str++; arg += *str++; } } } SetArgs(argumentArray);}void PArgList::SetArgs(const PStringArray & theArgs){ argumentArray = theArgs; shift = 0; optionLetters = ""; optionNames.SetSize(0); parameterIndex.SetSize(argumentArray.GetSize()); for (PINDEX i = 0; i < argumentArray.GetSize(); i++) parameterIndex[i] = i;}BOOL PArgList::Parse(const char * spec, BOOL optionsBeforeParams){ PAssertNULL(spec); // Find starting point, start at shift if first Parse() call. PINDEX arg = optionLetters.IsEmpty() ? shift : 0; // If not in parse all mode, have been parsed before, and had some parameters // from last time, then start argument parsing somewhere along instead of start. if (optionsBeforeParams && !optionLetters && parameterIndex.GetSize() > 0) arg = parameterIndex[parameterIndex.GetSize()-1] + 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -