src/base/trace_event/trace_log.cc
std::string TraceLog::EventToConsoleMessage(unsigned char phase,
                                            const TimeTicks& timestamp,
                                            TraceEvent* trace_event) {
  HEAP_PROFILER_SCOPED_IGNORE;
  AutoLock thread_info_lock(thread_info_lock_);

  // The caller should translate TRACE_EVENT_PHASE_COMPLETE to
  // TRACE_EVENT_PHASE_BEGIN or TRACE_EVENT_END.
  DCHECK(phase != TRACE_EVENT_PHASE_COMPLETE);

  TimeDelta duration;
  int thread_id =
      trace_event ? trace_event->thread_id() : PlatformThread::CurrentId();
  if (phase == TRACE_EVENT_PHASE_END) {
    duration = timestamp - thread_event_start_times_[thread_id].top();
    thread_event_start_times_[thread_id].pop();
  }

  std::string thread_name = thread_names_[thread_id];
  if (thread_colors_.find(thread_name) == thread_colors_.end()) {
    size_t next_color = (thread_colors_.size() % 6) + 1;
    thread_colors_[thread_name] = next_color;
  }

  std::ostringstream log;
  log << base::StringPrintf("%s: \x1b[0;3%dm", thread_name.c_str(),
                            thread_colors_[thread_name]);

  size_t depth = 0;
  auto it = thread_event_start_times_.find(thread_id);
  if (it != thread_event_start_times_.end())
    depth = it->second.size();

  for (size_t i = 0; i < depth; ++i)
    log << "| ";

  if (trace_event)
    trace_event->AppendPrettyPrinted(&log);
  if (phase == TRACE_EVENT_PHASE_END)
    log << base::StringPrintf(" (%.3f ms)", duration.InMillisecondsF());

  log << "\x1b[0;m";

  if (phase == TRACE_EVENT_PHASE_BEGIN)
    thread_event_start_times_[thread_id].push(timestamp);

  return log.str();
}
// May be called when a COMPELETE event ends and the unfinished event has been
// recycled (phase == TRACE_EVENT_PHASE_END and trace_event == NULL).
May be called when a COMPELETE event ends and the unfinished event has been
recycled (phase == TRACE_EVENT_PHASE_END and trace_event == NULL).
May be called when a COMPELETE event ends and the unfinished event has been recycled ( phase == TRACE_EVENT_PHASE_END and trace_event == NULL ) .
std::string TraceLog::EventToConsoleMessage(unsigned char phase,
                                            const TimeTicks& timestamp,
                                            TraceEvent* trace_event) {
  HEAP_PROFILER_SCOPED_IGNORE;
  AutoLock thread_info_lock(thread_info_lock_);
  DCHECK(phase != TRACE_EVENT_PHASE_COMPLETE);
  TimeDelta duration;
  int thread_id =
      trace_event ? trace_event->thread_id() : PlatformThread::CurrentId();
  if (phase == TRACE_EVENT_PHASE_END) {
    duration = timestamp - thread_event_start_times_[thread_id].top();
    thread_event_start_times_[thread_id].pop();
  }
  std::string thread_name = thread_names_[thread_id];
  if (thread_colors_.find(thread_name) == thread_colors_.end()) {
    size_t next_color = (thread_colors_.size() % 6) + 1;
    thread_colors_[thread_name] = next_color;
  }
  std::ostringstream log;
  log << base::StringPrintf("%s: \x1b[0;3%dm", thread_name.c_str(),
                            thread_colors_[thread_name]);
  size_t depth = 0;
  auto it = thread_event_start_times_.find(thread_id);
  if (it != thread_event_start_times_.end())
    depth = it->second.size();
  for (size_t i = 0; i < depth; ++i)
    log << "| ";
  if (trace_event)
    trace_event->AppendPrettyPrinted(&log);
  if (phase == TRACE_EVENT_PHASE_END)
    log << base::StringPrintf(" (%.3f ms)", duration.InMillisecondsF());
  log << "\x1b[0;m";
  if (phase == TRACE_EVENT_PHASE_BEGIN)
    thread_event_start_times_[thread_id].push(timestamp);
  return log.str();
}
std :: string TraceLog :: EventToConsoleMessage ( unsigned char phase , const TimeTicks & timestamp , TraceEvent * trace_event ) { HEAP_PROFILER_SCOPED_IGNORE ; AutoLock thread_info_lock ( thread_info_lock_ ) ; DCHECK ( phase != TRACE_EVENT_PHASE_COMPLETE ) ; TimeDelta duration ; int thread_id = trace_event ? trace_event -> thread_id ( ) : PlatformThread :: CurrentId ( ) ; if ( phase == TRACE_EVENT_PHASE_END ) { duration = timestamp - thread_event_start_times_ [ thread_id ] . top ( ) ; thread_event_start_times_ [ thread_id ] . pop ( ) ; } std :: string thread_name = thread_names_ [ thread_id ] ; if ( thread_colors_ . find ( thread_name ) == thread_colors_ . end ( ) ) { size_t next_color = ( thread_colors_ . size ( ) % 6 ) + 1 ; thread_colors_ [ thread_name ] = next_color ; } std :: ostringstream log ; log << base :: StringPrintf ( " \x1b " , thread_name . c_str ( ) , thread_colors_ [ thread_name ] ) ; size_t depth = 0 ; auto it = thread_event_start_times_ . find ( thread_id ) ; if ( it != thread_event_start_times_ . end ( ) ) depth = it -> second . size ( ) ; for ( size_t i = 0 ; i < depth ; ++ i ) log << " " ; if ( trace_event ) trace_event -> AppendPrettyPrinted ( & log ) ; if ( phase == TRACE_EVENT_PHASE_END ) log << base :: StringPrintf ( " " , duration . InMillisecondsF ( ) ) ; log << " \x1b " ; if ( phase == TRACE_EVENT_PHASE_BEGIN ) thread_event_start_times_ [ thread_id ] . push ( timestamp ) ; return log . str ( ) ; }
May be called when a COMPELETE event ends and the unfinished event has been
recycled (phase == TRACE_EVENT_PHASE_END and trace_event == NULL).
May be called when a COMPELETE event ends and the unfinished event has been recycled ( phase == TRACE_EVENT_PHASE_END and trace_event == NULL ) .
// The caller should translate TRACE_EVENT_PHASE_COMPLETE to // TRACE_EVENT_PHASE_BEGIN or TRACE_EVENT_END.