Logo Search packages:      
Sourcecode: pwlib version File versions

ostream & PTrace::Begin ( unsigned  level,
const char *  fileName,
int  lineNum 
) [static]

Begin a trace output. If the trace stream output is used outside of the provided macros, it should be noted that a mutex is obtained on the call to PBeginTrace# which will prevent any other threads from using the trace stream until the PEndTrace# function is called.

So a typical usage would be: {verbatim} ostream & s = PTrace::Begin(3, __FILE__, __LINE__); s << "hello"; if (want_there) s << " there"; s << '!' << PTrace::End(); {verbatim}

Parameters:
level  Log level for output
fileName  Filename of source file being traced
lineNum  Line number of source file being traced.

Definition at line 983 of file osutils.cxx.

References PTime::AsString(), PThread::Current(), DateAndTime, FileAndLine, PString::GetLength(), PThread::GetThreadName(), PTime::GMT, GMTTime, PString::Left(), PTime::Local, PString::Right(), RotateDaily, SystemLogStream, Thread, ThreadAddress, PTimer::Tick(), Timestamp, and TraceLevel.

Referenced by PTrace::Block::Block(), and PTrace::Block::~Block().

{
  if (PTraceMutex == NULL) {
    PAssertAlways("Cannot use PTRACE before PProcess constructed.");
    return *PTraceStream;
  }

  if (level == UINT_MAX)
    return *PTraceStream;

  PTraceMutex->Wait();

  // Save log level for this message so End() function can use. This is
  // protected by the PTraceMutex
  PTraceCurrentLevel = level;

  if ((PTrace_Filename != NULL) && (PTraceOptions&RotateDaily) != 0) {
    int day = PTime((PTraceOptions&GMTTime) ? PTime::GMT : PTime::Local).GetDayOfYear();
    if (day != PTrace_lastDayOfYear) {
      delete PTraceStream;
      PTraceStream = NULL;
      OpenTraceFile();
      if (PTraceStream == NULL) {
        PTraceMutex->Signal();
        return *PTraceStream;
      }
    }
  }

  if ((PTraceOptions&SystemLogStream) == 0) {
    if ((PTraceOptions&DateAndTime) != 0) {
      PTime now;
      *PTraceStream << now.AsString("yyyy/MM/dd hh:mm:ss.uuu\t", (PTraceOptions&GMTTime) ? PTime::GMT : PTime::Local);
    }

    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 << "ThreadID=0x"
                      << setfill('0') << hex << setw(8)
                      << PThread::GetCurrentThreadId()
                      << setfill(' ') << dec;
      else {
        PString name = thread->GetThreadName();
        if (name.GetLength() <= 23)
          *PTraceStream << setw(23) << name;
        else
          *PTraceStream << name.Left(10) << "..." << name.Right(10);
      }
      *PTraceStream << '\t';
    }

    if ((PTraceOptions&ThreadAddress) != 0)
      *PTraceStream << hex << setfill('0')
                    << setw(7) << (void *)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;
}


Generated by  Doxygen 1.6.0   Back to index