1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
117824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#include "webrtc/system_wrappers/source/trace_impl.h"
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
133f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <assert.h>
147824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#include <stdarg.h>
157824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#include <stdio.h>
167824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#include <string.h>
17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
18b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifdef _WIN32
197824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#include "webrtc/system_wrappers/source/trace_win.h"
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
217824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#include "webrtc/system_wrappers/source/trace_posix.h"
227824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#endif  // _WIN32
23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
247824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#include "webrtc/system_wrappers/interface/sleep.h"
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
26b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define KEY_LEN_CHARS 31
27b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifdef _WIN32
29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#pragma warning(disable:4355)
307824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org#endif  // _WIN32
31b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
32b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc {
337824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
344c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.orgconst int Trace::kBoilerplateLength = 71;
354c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.orgconst int Trace::kTimestampPosition = 13;
364c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.orgconst int Trace::kTimestampLength = 12;
3706eaa5465d57f416c14bb3a587ba4146290d6a58andrew@webrtc.orguint32_t Trace::level_filter_ = kTraceDefault;
38b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
39b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Construct On First Use idiom. Avoids "static initialization order fiasco".
40b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTraceImpl* TraceImpl::StaticInstance(CountOperation count_operation,
417824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                                     const TraceLevel level) {
427824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // Sanities to avoid taking lock unless absolutely necessary (for
437824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // performance reasons). count_operation == kAddRefNoCreate implies that a
447824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // message will be written to file.
457824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if ((level != kTraceAll) && (count_operation == kAddRefNoCreate)) {
4606eaa5465d57f416c14bb3a587ba4146290d6a58andrew@webrtc.org    if (!(level & level_filter())) {
477824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      return NULL;
48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
497824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
507824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  TraceImpl* impl =
517824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    GetStaticInstance<TraceImpl>(count_operation);
527824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return impl;
53b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
54b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
557824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgTraceImpl* TraceImpl::GetTrace(const TraceLevel level) {
567824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return StaticInstance(kAddRefNoCreate, level);
57b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
58b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
597824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgTraceImpl* TraceImpl::CreateInstance() {
60b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if defined(_WIN32)
617824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return new TraceWindows();
62b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
637824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return new TracePosix();
64b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
65b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
66b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
67b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgTraceImpl::TraceImpl()
687824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    : critsect_interface_(CriticalSectionWrapper::CreateCriticalSection()),
697824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      callback_(NULL),
707824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      row_count_text_(0),
717824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      file_count_text_(0),
727824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      trace_file_(*FileWrapper::Create()),
737824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      thread_(*ThreadWrapper::CreateThread(TraceImpl::Run, this,
74b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                                           kHighestPriority, "Trace")),
757824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      event_(*EventWrapper::Create()),
767824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      critsect_array_(CriticalSectionWrapper::CreateCriticalSection()),
777824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      next_free_idx_(),
787824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      level_(),
797824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      length_(),
807824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      message_queue_(),
817824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      active_queue_(0) {
827824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  next_free_idx_[0] = 0;
837824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  next_free_idx_[1] = 0;
847824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
857824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  unsigned int tid = 0;
867824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  thread_.Start(tid);
877824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
887824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
897824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
907824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      message_queue_[m][n] = new
917824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      char[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
92b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
937824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
94b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
95b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
967824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgbool TraceImpl::StopThread() {
977824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // Release the worker thread so that it can flush any lingering messages.
987824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  event_.Set();
997824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
1007824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // Allow 10 ms for pending messages to be flushed out.
1017824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // TODO(hellner): why not use condition variables to do this? Or let the
1027824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  //                worker thread die and let this thread flush remaining
1037824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  //                messages?
1047824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  SleepMs(10);
1057824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
1067824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  thread_.SetNotAlive();
1077824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // Make sure the thread finishes as quickly as possible (instead of having
1087824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // to wait for the timeout).
1097824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  event_.Set();
1107824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  bool stopped = thread_.Stop();
1117824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
1127824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  CriticalSectionScoped lock(critsect_interface_);
1137824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  trace_file_.Flush();
1147824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  trace_file_.CloseFile();
1157824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return stopped;
116b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
117b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
1187824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgTraceImpl::~TraceImpl() {
1197824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  StopThread();
1207824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  delete &event_;
1217824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  delete &trace_file_;
1227824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  delete &thread_;
1237824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  delete critsect_interface_;
1247824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  delete critsect_array_;
1257824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
1267824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
1277824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
1287824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      delete [] message_queue_[m][n];
129b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
1307824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
131b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
132b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
133c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t TraceImpl::AddThreadId(char* trace_message) const {
134c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  uint32_t thread_id = ThreadWrapper::GetThreadId();
135b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  // Messages is 12 characters.
1367824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return sprintf(trace_message, "%10u; ", thread_id);
137b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
138b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
139c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t TraceImpl::AddLevel(char* sz_message, const TraceLevel level) const {
1407824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  const int kMessageLength = 12;
1417824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  switch (level) {
1427824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceTerseInfo:
1437824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      // Add the appropriate amount of whitespace.
1447824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      memset(sz_message, ' ', kMessageLength);
1457824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sz_message[kMessageLength] = '\0';
1467824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1477824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceStateInfo:
1487824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "STATEINFO ; ");
1497824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1507824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceWarning:
1517824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "WARNING   ; ");
1527824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1537824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceError:
1547824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "ERROR     ; ");
1557824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1567824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceCritical:
1577824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "CRITICAL  ; ");
1587824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1597824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceInfo:
1607824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "DEBUGINFO ; ");
1617824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1627824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceModuleCall:
1637824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "MODULECALL; ");
1647824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1657824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceMemory:
1667824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "MEMORY    ; ");
1677824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1687824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceTimer:
1697824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "TIMER     ; ");
1707824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1717824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceStream:
1727824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "STREAM    ; ");
1737824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1747824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceApiCall:
1757824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "APICALL   ; ");
1767824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1777824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    case kTraceDebug:
1787824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      sprintf(sz_message, "DEBUG     ; ");
1797824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
1807824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    default:
1817824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      assert(false);
1827824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      return 0;
1837824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
1847824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // All messages are 12 characters.
1857824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return kMessageLength;
186b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
187b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
188c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t TraceImpl::AddModuleAndId(char* trace_message,
189c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org                                  const TraceModule module,
190c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org                                  const int32_t id) const {
1917824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // Use long int to prevent problems with different definitions of
192c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  // int32_t.
1937824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // TODO(hellner): is this actually a problem? If so, it should be better to
194c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  //                clean up int32_t
1957824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  const long int idl = id;
1967824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  const int kMessageLength = 25;
1977824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (idl != -1) {
1987824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    const unsigned long int id_engine = id >> 16;
1997824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    const unsigned long int id_channel = id & 0xffff;
2007824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
2017824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    switch (module) {
2027824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceUndefined:
2037824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        // Add the appropriate amount of whitespace.
2047824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        memset(trace_message, ' ', kMessageLength);
2057824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        trace_message[kMessageLength] = '\0';
2067824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2077824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVoice:
2087824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "       VOICE:%5ld %5ld;", id_engine,
2097824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2107824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2117824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideo:
2127824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "       VIDEO:%5ld %5ld;", id_engine,
2137824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2147824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2157824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceUtility:
2167824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "     UTILITY:%5ld %5ld;", id_engine,
2177824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2187824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2197824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceRtpRtcp:
2207824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "    RTP/RTCP:%5ld %5ld;", id_engine,
2217824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2227824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2237824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceTransport:
2247824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "   TRANSPORT:%5ld %5ld;", id_engine,
2257824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2267824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2277824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioCoding:
2287824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine,
2297824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2307824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2317824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceSrtp:
2327824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "        SRTP:%5ld %5ld;", id_engine,
2337824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2347824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2357824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioMixerServer:
2367824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine,
2377824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2387824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2397824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioMixerClient:
2407824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine,
2417824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2427824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2437824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideoCoding:
2447824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine,
2457824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2467824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2477824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideoMixer:
2487824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        // Print sleep time and API call
2497824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "   VIDEO MIX:%5ld %5ld;", id_engine,
2507824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2517824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2527824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceFile:
2537824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "        FILE:%5ld %5ld;", id_engine,
2547824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2557824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2567824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioProcessing:
2577824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "  AUDIO PROC:%5ld %5ld;", id_engine,
2587824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2597824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2607824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioDevice:
2617824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine,
2627824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2637824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2647824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideoRenderer:
2657824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine,
2667824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2677824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2687824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideoCapture:
2697824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine,
2707824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                id_channel);
2717824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
27246f72884ad5c4078fa324626aa69e4860e4d4ae2pbos@webrtc.org      case kTraceRemoteBitrateEstimator:
27346f72884ad5c4078fa324626aa69e4860e4d4ae2pbos@webrtc.org        sprintf(trace_message, "     BWE RBE:%5ld %5ld;", id_engine,
27446f72884ad5c4078fa324626aa69e4860e4d4ae2pbos@webrtc.org                id_channel);
27546f72884ad5c4078fa324626aa69e4860e4d4ae2pbos@webrtc.org        break;
2767824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    }
2777824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  } else {
2787824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    switch (module) {
2797824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceUndefined:
2807824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        // Add the appropriate amount of whitespace.
2817824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        memset(trace_message, ' ', kMessageLength);
2827824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        trace_message[kMessageLength] = '\0';
2837824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2847824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVoice:
2857824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "       VOICE:%11ld;", idl);
2867824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2877824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideo:
2887824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "       VIDEO:%11ld;", idl);
2897824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2907824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceUtility:
2917824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "     UTILITY:%11ld;", idl);
2927824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2937824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceRtpRtcp:
2947824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "    RTP/RTCP:%11ld;", idl);
2957824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2967824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceTransport:
2977824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "   TRANSPORT:%11ld;", idl);
2987824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
2997824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioCoding:
3007824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "AUDIO CODING:%11ld;", idl);
3017824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3027824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceSrtp:
3037824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "        SRTP:%11ld;", idl);
3047824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3057824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioMixerServer:
3067824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl);
3077824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3087824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioMixerClient:
3097824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl);
3107824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3117824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideoCoding:
3127824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "VIDEO CODING:%11ld;", idl);
3137824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3147824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideoMixer:
3157824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "   VIDEO MIX:%11ld;", idl);
3167824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3177824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceFile:
3187824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "        FILE:%11ld;", idl);
3197824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3207824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioProcessing:
3217824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "  AUDIO PROC:%11ld;", idl);
3227824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3237824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceAudioDevice:
3247824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl);
3257824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3267824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideoRenderer:
3277824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "VIDEO RENDER:%11ld;", idl);
3287824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
3297824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      case kTraceVideoCapture:
3307824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl);
3317824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        break;
33246f72884ad5c4078fa324626aa69e4860e4d4ae2pbos@webrtc.org      case kTraceRemoteBitrateEstimator:
33346f72884ad5c4078fa324626aa69e4860e4d4ae2pbos@webrtc.org        sprintf(trace_message, "     BWE RBE:%11ld;", idl);
33446f72884ad5c4078fa324626aa69e4860e4d4ae2pbos@webrtc.org        break;
335b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
3367824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
3377824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return kMessageLength;
338b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
339b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
340c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t TraceImpl::SetTraceFileImpl(const char* file_name_utf8,
341c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org                                    const bool add_file_counter) {
3427824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  CriticalSectionScoped lock(critsect_interface_);
3437824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
3447824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  trace_file_.Flush();
3457824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  trace_file_.CloseFile();
3467824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
3477824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (file_name_utf8) {
3487824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (add_file_counter) {
3497824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      file_count_text_ = 1;
3507824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
3517824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize];
3527824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      CreateFileName(file_name_utf8, file_name_with_counter_utf8,
3537824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                     file_count_text_);
3547824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      if (trace_file_.OpenFile(file_name_with_counter_utf8, false, false,
3557824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                               true) == -1) {
3567824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        return -1;
3577824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      }
3587824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    } else {
3597824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      file_count_text_ = 0;
3607824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      if (trace_file_.OpenFile(file_name_utf8, false, false, true) == -1) {
3617824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        return -1;
3627824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      }
363b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
3647824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
3657824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  row_count_text_ = 0;
3667824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return 0;
367b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
368b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
369c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t TraceImpl::TraceFileImpl(
3707824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    char file_name_utf8[FileWrapper::kMaxFileNameSize]) {
3717824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  CriticalSectionScoped lock(critsect_interface_);
3727824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return trace_file_.FileName(file_name_utf8, FileWrapper::kMaxFileNameSize);
373b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
374b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
375c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) {
3767824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  CriticalSectionScoped lock(critsect_interface_);
3777824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  callback_ = callback;
3787824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return 0;
379b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
380b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
381c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t TraceImpl::AddMessage(
3827824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    char* trace_message,
383b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
384c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org    const uint16_t written_so_far) const {
3857824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  int length = 0;
3867824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
3877824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    return -1;
3887824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
3897824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // - 2 to leave room for newline and NULL termination.
390b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifdef _WIN32
3917824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  length = _snprintf(trace_message,
3927824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                     WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
3937824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                     "%s", msg);
3947824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (length < 0) {
3957824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
3967824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    trace_message[length] = 0;
3977824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
398b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
3997824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  length = snprintf(trace_message,
4007824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                    WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
4017824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                    "%s", msg);
4027824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (length < 0 ||
4037824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
4047824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
4057824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    trace_message[length] = 0;
4067824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
407b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
4087824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // Length with NULL termination.
4097824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return length + 1;
410b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
411b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
412b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgvoid TraceImpl::AddMessageToList(
4134c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
414c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org    const uint16_t length,
4154c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    const TraceLevel level) {
416203d65641672709b535b923f99fa4f38bab78d71solenberg@webrtc.org// NOTE(andresp): Enabled externally.
417203d65641672709b535b923f99fa4f38bab78d71solenberg@webrtc.org#ifdef WEBRTC_DIRECT_TRACE
418203d65641672709b535b923f99fa4f38bab78d71solenberg@webrtc.org  if (callback_) {
419203d65641672709b535b923f99fa4f38bab78d71solenberg@webrtc.org    callback_->Print(level, trace_message, length);
420203d65641672709b535b923f99fa4f38bab78d71solenberg@webrtc.org  }
421203d65641672709b535b923f99fa4f38bab78d71solenberg@webrtc.org  return;
422203d65641672709b535b923f99fa4f38bab78d71solenberg@webrtc.org#endif
423203d65641672709b535b923f99fa4f38bab78d71solenberg@webrtc.org
4247824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  CriticalSectionScoped lock(critsect_array_);
4257824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
4267824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (next_free_idx_[active_queue_] >= WEBRTC_TRACE_MAX_QUEUE) {
4277824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (!trace_file_.Open() && !callback_) {
4287824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      // Keep at least the last 1/4 of old messages when not logging.
4297824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      // TODO(hellner): isn't this redundant. The user will make it known
4307824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      //                when to start logging. Why keep messages before
4317824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      //                that?
4327824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE / 4; ++n) {
4337824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        const int last_quarter_offset = (3 * WEBRTC_TRACE_MAX_QUEUE / 4);
4347824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        memcpy(message_queue_[active_queue_][n],
4357824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org               message_queue_[active_queue_][n + last_quarter_offset],
4367824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org               WEBRTC_TRACE_MAX_MESSAGE_SIZE);
4377824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      }
4387824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      next_free_idx_[active_queue_] = WEBRTC_TRACE_MAX_QUEUE / 4;
4397824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    } else {
4407824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      // More messages are being written than there is room for in the
4417824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      // buffer. Drop any new messages.
4427824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      // TODO(hellner): its probably better to drop old messages instead
4437824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      //                of new ones. One step further: if this happens
4447824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      //                it's due to writing faster than what can be
4457824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      //                processed. Maybe modify the filter at this point.
4467824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      //                E.g. turn of STREAM.
4477824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      return;
448b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
4497824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
4507824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
451c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  uint16_t idx = next_free_idx_[active_queue_];
4527824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  next_free_idx_[active_queue_]++;
4537824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
4547824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  level_[active_queue_][idx] = level;
4557824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  length_[active_queue_][idx] = length;
4567824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  memcpy(message_queue_[active_queue_][idx], trace_message, length);
4577824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
4587824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (next_free_idx_[active_queue_] == WEBRTC_TRACE_MAX_QUEUE - 1) {
4597824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    // Logging more messages than can be worked off. Log a warning.
4607824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    const char warning_msg[] = "WARNING MISSING TRACE MESSAGES\n";
4617824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    level_[active_queue_][next_free_idx_[active_queue_]] = kTraceWarning;
4627824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    length_[active_queue_][next_free_idx_[active_queue_]] = strlen(warning_msg);
4637824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    memcpy(message_queue_[active_queue_][next_free_idx_[active_queue_]],
4647824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org           warning_msg, strlen(warning_msg));
4657824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    next_free_idx_[active_queue_]++;
4667824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
467b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
468b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
4697824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgbool TraceImpl::Run(void* obj) {
4707824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return static_cast<TraceImpl*>(obj)->Process();
471b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
472b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
4737824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgbool TraceImpl::Process() {
4747824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (event_.Wait(1000) == kEventSignaled) {
4754c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    // This slightly odd construction is to avoid locking |critsect_interface_|
4764c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    // while calling WriteToFile() since it's locked inside the function.
4774c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    critsect_interface_->Enter();
4784c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    bool write_to_file = trace_file_.Open() || callback_;
4794c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    critsect_interface_->Leave();
4804c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    if (write_to_file) {
4817824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      WriteToFile();
482b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
4837824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  } else {
4844c27c038149965cddfbe4043284af39bacfc2bcdandrew@webrtc.org    CriticalSectionScoped lock(critsect_interface_);
4857824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    trace_file_.Flush();
4867824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
4877824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return true;
488b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
489b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
4907824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgvoid TraceImpl::WriteToFile() {
491c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  uint8_t local_queue_active = 0;
492c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  uint16_t local_next_free_idx = 0;
4937824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
4947824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // There are two buffers. One for reading (for writing to file) and one for
4957824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // writing (for storing new messages). Let new messages be posted to the
4967824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  // unused buffer so that the current buffer can be flushed safely.
4977824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  {
4987824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    CriticalSectionScoped lock(critsect_array_);
4997824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    local_next_free_idx = next_free_idx_[active_queue_];
5007824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    next_free_idx_[active_queue_] = 0;
5017824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    local_queue_active = active_queue_;
5027824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (active_queue_ == 0) {
5037824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      active_queue_ = 1;
5047824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    } else {
5057824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      active_queue_ = 0;
506b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
5077824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
5087824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (local_next_free_idx == 0) {
5097824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    return;
5107824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
5117824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
5127824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  CriticalSectionScoped lock(critsect_interface_);
5137824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
514c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  for (uint16_t idx = 0; idx < local_next_free_idx; ++idx) {
5157824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    TraceLevel local_level = level_[local_queue_active][idx];
5167824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (callback_) {
5177824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      callback_->Print(local_level, message_queue_[local_queue_active][idx],
5187824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                       length_[local_queue_active][idx]);
519b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
5207824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (trace_file_.Open()) {
5217824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
5227824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        // wrap file
5237824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        row_count_text_ = 0;
5247824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        trace_file_.Flush();
5257824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
5267824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        if (file_count_text_ == 0) {
5277824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          trace_file_.Rewind();
5287824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        } else {
5297824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          char old_file_name[FileWrapper::kMaxFileNameSize];
5307824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          char new_file_name[FileWrapper::kMaxFileNameSize];
531b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
5327824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          // get current name
5337824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          trace_file_.FileName(old_file_name,
5347824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                               FileWrapper::kMaxFileNameSize);
5357824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          trace_file_.CloseFile();
536b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
5377824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          file_count_text_++;
5387824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
5397824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          UpdateFileName(old_file_name, new_file_name, file_count_text_);
5407824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
5417824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          if (trace_file_.OpenFile(new_file_name, false, false,
5427824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                                   true) == -1) {
5437824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org            return;
5447824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          }
545b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        }
5467824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      }
5477824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      if (row_count_text_ ==  0) {
5487824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
549c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org        int32_t length = AddDateTimeInfo(message);
5507824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        if (length != -1) {
5517824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          message[length] = 0;
5527824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          message[length - 1] = '\n';
5537824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          trace_file_.Write(message, length);
5547824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          row_count_text_++;
555b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        }
5567824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        length = AddBuildInfo(message);
5577824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        if (length != -1) {
5587824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          message[length + 1] = 0;
5597824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          message[length] = '\n';
5607824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          message[length - 1] = '\n';
5617824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          trace_file_.Write(message, length + 1);
5627824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          row_count_text_++;
5637824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          row_count_text_++;
5647824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        }
5657824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      }
566c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org      uint16_t length = length_[local_queue_active][idx];
5677824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      message_queue_[local_queue_active][idx][length] = 0;
5687824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      message_queue_[local_queue_active][idx][length - 1] = '\n';
5697824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      trace_file_.Write(message_queue_[local_queue_active][idx], length);
5707824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      row_count_text_++;
571b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
5727824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
573b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
574b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
575b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgvoid TraceImpl::AddImpl(const TraceLevel level, const TraceModule module,
576c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org                        const int32_t id,
5777824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org                        const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
5787824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (TraceCheck(level)) {
5797824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
5807824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    char* message_ptr = trace_message;
581b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
582c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org    int32_t len = 0;
583c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org    int32_t ack_len = 0;
584b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
5857824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    len = AddLevel(message_ptr, level);
5867824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (len == -1) {
5877824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      return;
5887824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    }
5897824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    message_ptr += len;
5907824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ack_len += len;
591b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
5927824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    len = AddTime(message_ptr, level);
5937824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (len == -1) {
5947824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      return;
5957824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    }
5967824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    message_ptr += len;
5977824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ack_len += len;
598b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
5997824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    len = AddModuleAndId(message_ptr, module, id);
6007824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (len == -1) {
6017824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      return;
6027824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    }
6037824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    message_ptr += len;
6047824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ack_len += len;
6057824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
6067824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    len = AddThreadId(message_ptr);
6077824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (len < 0) {
6087824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      return;
6097824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    }
6107824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    message_ptr += len;
6117824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ack_len += len;
612b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
613c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org    len = AddMessage(message_ptr, msg, (uint16_t)ack_len);
6147824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (len == -1) {
6157824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      return;
616b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
6177824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ack_len += len;
618c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org    AddMessageToList(trace_message, (uint16_t)ack_len, level);
6197824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
6207824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    // Make sure that messages are written as soon as possible.
6217824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    event_.Set();
6227824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
623b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
624b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
6257824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgbool TraceImpl::TraceCheck(const TraceLevel level) const {
62606eaa5465d57f416c14bb3a587ba4146290d6a58andrew@webrtc.org  return (level & level_filter()) ? true : false;
627b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
628b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
629b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgbool TraceImpl::UpdateFileName(
6307824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    const char file_name_utf8[FileWrapper::kMaxFileNameSize],
6317824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
632c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org    const uint32_t new_count) const {
633c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int32_t length = (int32_t)strlen(file_name_utf8);
6347824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (length < 0) {
6357824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    return false;
6367824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
6377824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
638c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int32_t length_without_file_ending = length - 1;
6397824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  while (length_without_file_ending > 0) {
6407824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (file_name_utf8[length_without_file_ending] == '.') {
6417824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
6427824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    } else {
6437824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      length_without_file_ending--;
644b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
6457824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
6467824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (length_without_file_ending == 0) {
6477824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    length_without_file_ending = length;
6487824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
649c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int32_t length_to_ = length_without_file_ending - 1;
6507824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  while (length_to_ > 0) {
6517824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (file_name_utf8[length_to_] == '_') {
6527824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
6537824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    } else {
6547824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      length_to_--;
655b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
6567824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
657b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
6587824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_);
6597824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
6607824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          static_cast<long unsigned int>(new_count),
6617824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          file_name_utf8 + length_without_file_ending);
6627824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return true;
663b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
664b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
665b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgbool TraceImpl::CreateFileName(
6667824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    const char file_name_utf8[FileWrapper::kMaxFileNameSize],
6677824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
668c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org    const uint32_t new_count) const {
669c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int32_t length = (int32_t)strlen(file_name_utf8);
6707824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (length < 0) {
6717824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    return false;
6727824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
6737824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org
674c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int32_t length_without_file_ending = length - 1;
6757824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  while (length_without_file_ending > 0) {
6767824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (file_name_utf8[length_without_file_ending] == '.') {
6777824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      break;
6787824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    } else {
6797824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      length_without_file_ending--;
680b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
6817824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
6827824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (length_without_file_ending == 0) {
6837824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    length_without_file_ending = length;
6847824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
6857824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  memcpy(file_name_with_counter_utf8, file_name_utf8,
6867824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org         length_without_file_ending);
6877824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
6887824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          static_cast<long unsigned int>(new_count),
6897824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org          file_name_utf8 + length_without_file_ending);
6907824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return true;
691b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
692b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
6937824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgvoid Trace::CreateTrace() {
6947824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  TraceImpl::StaticInstance(kAddRef);
695b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
696b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
6977824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.orgvoid Trace::ReturnTrace() {
6987824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  TraceImpl::StaticInstance(kRelease);
699b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
700b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
701c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) {
7027824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  TraceImpl* trace = TraceImpl::GetTrace();
7037824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (trace) {
7047824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    int ret_val = trace->TraceFileImpl(file_name);
7057824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ReturnTrace();
7067824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    return ret_val;
7077824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
7087824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return -1;
709b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
710b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
711c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t Trace::SetTraceFile(const char* file_name,
712c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org                            const bool add_file_counter) {
7137824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  TraceImpl* trace = TraceImpl::GetTrace();
7147824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (trace) {
7157824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
7167824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ReturnTrace();
7177824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    return ret_val;
7187824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
7197824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return -1;
720b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
721b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
722c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint32_t Trace::SetTraceCallback(TraceCallback* callback) {
7237824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  TraceImpl* trace = TraceImpl::GetTrace();
7247824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (trace) {
7257824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    int ret_val = trace->SetTraceCallbackImpl(callback);
7267824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ReturnTrace();
7277824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    return ret_val;
7287824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
7297824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  return -1;
730b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
731b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
732b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgvoid Trace::Add(const TraceLevel level, const TraceModule module,
733c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org                const int32_t id, const char* msg, ...) {
7347824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  TraceImpl* trace = TraceImpl::GetTrace(level);
7357824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  if (trace) {
7367824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    if (trace->TraceCheck(level)) {
7377824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
7387824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      char* buff = 0;
7397824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      if (msg) {
7407824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        va_list args;
7417824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        va_start(args, msg);
742b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifdef _WIN32
7437824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
744b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
7457824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
746b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
7477824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        va_end(args);
7487824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org        buff = temp_buff;
7497824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      }
7507824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org      trace->AddImpl(level, module, id, buff);
751b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
7527824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org    ReturnTrace();
7537824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org  }
754b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
755b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
7567824ff1bacbd3be5af8088fc7d81c33fe0647f36phoglund@webrtc.org}  // namespace webrtc
757