1e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent/*
2a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *
4e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  Use of this source code is governed by a BSD-style license
5e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  that can be found in the LICENSE file in the root of the source
6e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  tree. An additional intellectual property rights grant can be found
7e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  in the file PATENTS.  All contributing project authors may
8e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  be found in the AUTHORS file in the root of the source tree.
9e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent */
10e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
11e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "trace_impl.h"
12e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
13e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include <cassert>
14e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include <string.h> // memset
15e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
16e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#ifdef _WIN32
17c55a96383497a772a307b346368133960b02ad03Eric Laurent#include "trace_win.h"
18e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#else
19e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include <stdio.h>
20e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include <stdarg.h>
21c55a96383497a772a307b346368133960b02ad03Eric Laurent#include "trace_posix.h"
22e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#endif // _WIN32
23e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
24a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin#include "system_wrappers/interface/sleep.h"
25a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
26e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#define KEY_LEN_CHARS 31
27e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
28e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#ifdef _WIN32
29a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin#pragma warning(disable:4355)
30e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#endif // _WIN32
31e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
32e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentnamespace webrtc {
33e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentstatic WebRtc_UWord32 levelFilter = kTraceDefault;
34e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
35e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent// Construct On First Use idiom. Avoids "static initialization order fiasco".
36c55a96383497a772a307b346368133960b02ad03Eric LaurentTraceImpl* TraceImpl::StaticInstance(CountOperation count_operation,
37c55a96383497a772a307b346368133960b02ad03Eric Laurent                                     const TraceLevel level)
38e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
39c55a96383497a772a307b346368133960b02ad03Eric Laurent    // Sanities to avoid taking lock unless absolutely necessary (for
40c55a96383497a772a307b346368133960b02ad03Eric Laurent    // performance reasons).
41c55a96383497a772a307b346368133960b02ad03Eric Laurent    // count_operation == kAddRefNoCreate implies that a message will be
42c55a96383497a772a307b346368133960b02ad03Eric Laurent    // written to file.
43c55a96383497a772a307b346368133960b02ad03Eric Laurent    if((level != kTraceAll) && (count_operation == kAddRefNoCreate))
44e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
45e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        if(!(level & levelFilter))
46e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        {
47e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            return NULL;
48e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        }
49e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
50c55a96383497a772a307b346368133960b02ad03Eric Laurent    TraceImpl* impl =
51c55a96383497a772a307b346368133960b02ad03Eric Laurent        GetStaticInstance<TraceImpl>(count_operation);
52c55a96383497a772a307b346368133960b02ad03Eric Laurent    return impl;
53e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
54e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
55e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentTraceImpl* TraceImpl::GetTrace(const TraceLevel level)
56e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
57c55a96383497a772a307b346368133960b02ad03Eric Laurent    return StaticInstance(kAddRefNoCreate, level);
58e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
59e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
60c55a96383497a772a307b346368133960b02ad03Eric LaurentTraceImpl* TraceImpl::CreateInstance()
61e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
62e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#if defined(_WIN32)
63e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return new TraceWindows();
64e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#else
65c55a96383497a772a307b346368133960b02ad03Eric Laurent    return new TracePosix();
66e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#endif
67e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
68e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
69e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentTraceImpl::TraceImpl()
70c55a96383497a772a307b346368133960b02ad03Eric Laurent    : _critsectInterface(CriticalSectionWrapper::CreateCriticalSection()),
71e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _callback(NULL),
72e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _rowCountText(0),
73e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _fileCountText(0),
74e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _traceFile(*FileWrapper::Create()),
75e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _thread(*ThreadWrapper::CreateThread(TraceImpl::Run, this,
76e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                                           kHighestPriority, "Trace")),
77e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _event(*EventWrapper::Create()),
78c55a96383497a772a307b346368133960b02ad03Eric Laurent      _critsectArray(CriticalSectionWrapper::CreateCriticalSection()),
79e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _nextFreeIdx(),
80e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _level(),
81e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _length(),
82e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _messageQueue(),
83e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      _activeQueue(0)
84e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
85e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _nextFreeIdx[0] = 0;
86e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _nextFreeIdx[1] = 0;
87e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
88e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    unsigned int tid = 0;
89e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _thread.Start(tid);
90e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
91e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    for(int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; m++)
92e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
93e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        for(int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; n++)
94e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        {
95e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            _messageQueue[m][n] = new
96a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin                char[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
97e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        }
98e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
99e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
100e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
101e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentbool TraceImpl::StopThread()
102e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
103e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // Release the worker thread so that it can flush any lingering messages.
104e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _event.Set();
105e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
106e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // Allow 10 ms for pending messages to be flushed out.
107e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // TODO (hellner): why not use condition variables to do this? Or let the
108e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    //                 worker thread die and let this thread flush remaining
109e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    //                 messages?
110a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    SleepMs(10);
111e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
112e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _thread.SetNotAlive();
113e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // Make sure the thread finishes as quickly as possible (instead of having
114e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // to wait for the timeout).
115e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _event.Set();
116e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    bool stopped = _thread.Stop();
117e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
118e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    CriticalSectionScoped lock(_critsectInterface);
119e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _traceFile.Flush();
120e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _traceFile.CloseFile();
121e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return stopped;
122e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
123e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
124e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentTraceImpl::~TraceImpl()
125e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
126e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    StopThread();
127e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    delete &_event;
128e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    delete &_traceFile;
129e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    delete &_thread;
130c55a96383497a772a307b346368133960b02ad03Eric Laurent    delete _critsectInterface;
131c55a96383497a772a307b346368133960b02ad03Eric Laurent    delete _critsectArray;
132e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
133e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    for(int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; m++)
134e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
135e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        for(int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; n++)
136e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        {
137e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            delete [] _messageQueue[m][n];
138e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        }
139e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
140e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
141e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
142a6451827d543eb00824bc95097e47d0aac51ae93Alexander GutkinWebRtc_Word32 TraceImpl::AddThreadId(char* traceMessage) const {
143a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  WebRtc_UWord32 threadId = ThreadWrapper::GetThreadId();
144a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  // Messages is 12 characters.
145a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  return sprintf(traceMessage, "%10u; ", threadId);
146a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
147a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
148e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentWebRtc_Word32 TraceImpl::AddLevel(char* szMessage, const TraceLevel level) const
149e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
150e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    switch (level)
151e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
152e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceStateInfo:
153e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "STATEINFO ; ");
154e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
155e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceWarning:
156e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "WARNING   ; ");
157e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
158e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceError:
159e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "ERROR     ; ");
160e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
161e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceCritical:
162e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "CRITICAL  ; ");
163e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
164e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceInfo:
165e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "DEBUGINFO ; ");
166e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
167e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceModuleCall:
168e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "MODULECALL; ");
169e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
170e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceMemory:
171e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "MEMORY    ; ");
172e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
173e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceTimer:
174e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "TIMER     ; ");
175e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
176e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceStream:
177e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "STREAM    ; ");
178e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
179e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceApiCall:
180e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "APICALL   ; ");
181e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
182e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        case kTraceDebug:
183e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            sprintf (szMessage, "DEBUG     ; ");
184e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            break;
185e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        default:
186e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            assert(false);
187e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            return 0;
188e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
189e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // All messages are 12 characters.
190e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return 12;
191e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
192e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
193e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentWebRtc_Word32 TraceImpl::AddModuleAndId(char* traceMessage,
194e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                                        const TraceModule module,
195e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                                        const WebRtc_Word32 id) const
196e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
197e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // Use long int to prevent problems with different definitions of
198e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // WebRtc_Word32.
199e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // TODO (hellner): is this actually a problem? If so, it should be better to
200e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    //                 clean up WebRtc_Word32
201e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    const long int idl = id;
202e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if(idl != -1)
203e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
204e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        const unsigned long int idEngine = id>>16;
205e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        const unsigned long int idChannel = id & 0xffff;
206e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
207e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        switch (module)
208e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        {
209e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVoice:
210e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "       VOICE:%5ld %5ld;", idEngine,
211e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
212e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
213e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideo:
214e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "       VIDEO:%5ld %5ld;", idEngine,
215e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
216e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
217e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceUtility:
218e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "     UTILITY:%5ld %5ld;", idEngine,
219e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
220e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
221e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceRtpRtcp:
222e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "    RTP/RTCP:%5ld %5ld;", idEngine,
223e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
224e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
225e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceTransport:
226e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "   TRANSPORT:%5ld %5ld;", idEngine,
227e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
228e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
229e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioCoding:
230e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "AUDIO CODING:%5ld %5ld;", idEngine,
231e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
232e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
233e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceSrtp:
234e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "        SRTP:%5ld %5ld;", idEngine,
235e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
236e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
237e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioMixerServer:
238e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, " AUDIO MIX/S:%5ld %5ld;", idEngine,
239e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
240e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
241e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioMixerClient:
242e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, " AUDIO MIX/C:%5ld %5ld;", idEngine,
243e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
244e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
245e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoCoding:
246e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "VIDEO CODING:%5ld %5ld;", idEngine,
247e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
248e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
249e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoMixer:
250e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                // Print sleep time and API call
251e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "   VIDEO MIX:%5ld %5ld;", idEngine,
252e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
253e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
254e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceFile:
255e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "        FILE:%5ld %5ld;", idEngine,
256e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
257e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
258e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioProcessing:
259e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "  AUDIO PROC:%5ld %5ld;", idEngine,
260e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
261e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
262e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioDevice:
263e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "AUDIO DEVICE:%5ld %5ld;", idEngine,
264e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
265e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
266e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoRenderer:
267e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "VIDEO RENDER:%5ld %5ld;", idEngine,
268e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
269e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
270e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoCapture:
271e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "VIDEO CAPTUR:%5ld %5ld;", idEngine,
272e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
273e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
274e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoPreocessing:
275e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf(traceMessage, "  VIDEO PROC:%5ld %5ld;", idEngine,
276e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                        idChannel);
277e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
278e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        }
279e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    } else {
280e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        switch (module)
281e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        {
282e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVoice:
283e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "       VOICE:%11ld;", idl);
284e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
285e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideo:
286e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "       VIDEO:%11ld;", idl);
287e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
288e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceUtility:
289e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "     UTILITY:%11ld;", idl);
290e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
291e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceRtpRtcp:
292e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "    RTP/RTCP:%11ld;", idl);
293e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
294e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceTransport:
295e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "   TRANSPORT:%11ld;", idl);
296e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
297e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioCoding:
298e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "AUDIO CODING:%11ld;", idl);
299e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
300e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceSrtp:
301e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "        SRTP:%11ld;", idl);
302e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
303e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioMixerServer:
304e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, " AUDIO MIX/S:%11ld;", idl);
305e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
306e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioMixerClient:
307e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, " AUDIO MIX/C:%11ld;", idl);
308e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
309e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoCoding:
310e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "VIDEO CODING:%11ld;", idl);
311e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
312e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoMixer:
313e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "   VIDEO MIX:%11ld;", idl);
314e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
315e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceFile:
316e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "        FILE:%11ld;", idl);
317e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
318e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioProcessing:
319e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "  AUDIO PROC:%11ld;", idl);
320e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
321e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceAudioDevice:
322e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "AUDIO DEVICE:%11ld;", idl);
323e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
324e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoRenderer:
325e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "VIDEO RENDER:%11ld;", idl);
326e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
327e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoCapture:
328e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "VIDEO CAPTUR:%11ld;", idl);
329e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
330e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            case kTraceVideoPreocessing:
331e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                sprintf (traceMessage, "  VIDEO PROC:%11ld;", idl);
332e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                break;
333e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        }
334e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
335e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // All messages are 25 characters.
336e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return 25;
337e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
338e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
339a6451827d543eb00824bc95097e47d0aac51ae93Alexander GutkinWebRtc_Word32 TraceImpl::SetTraceFileImpl(const char* fileNameUTF8,
340e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                                          const bool addFileCounter)
341e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
342e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    CriticalSectionScoped lock(_critsectInterface);
343e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
344e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _traceFile.Flush();
345e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _traceFile.CloseFile();
346e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
347e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if(fileNameUTF8)
348e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
349e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        if(addFileCounter)
350e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        {
351e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            _fileCountText = 1;
352e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
353a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin            char fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize];
354e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            CreateFileName(fileNameUTF8, fileNameWithCounterUTF8,
355e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                           _fileCountText);
356e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            if(_traceFile.OpenFile(fileNameWithCounterUTF8, false, false,
357e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                                   true) == -1)
358e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            {
359e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                return -1;
360e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            }
361e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        }else {
362e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            _fileCountText = 0;
363e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            if(_traceFile.OpenFile(fileNameUTF8, false, false, true) == -1)
364e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            {
365e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                return -1;
366e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            }
367e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        }
368e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
369e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _rowCountText = 0;
370e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return 0;
371e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
372e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
373e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentWebRtc_Word32 TraceImpl::TraceFileImpl(
374a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    char fileNameUTF8[FileWrapper::kMaxFileNameSize])
375e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
376e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    CriticalSectionScoped lock(_critsectInterface);
377e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return _traceFile.FileName(fileNameUTF8, FileWrapper::kMaxFileNameSize);
378e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
379e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
380e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentWebRtc_Word32 TraceImpl::SetTraceCallbackImpl(TraceCallback* callback)
381e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
382e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    CriticalSectionScoped lock(_critsectInterface);
383e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    _callback = callback;
384e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return 0;
385e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
386e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
387e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentWebRtc_Word32 TraceImpl::AddMessage(
388e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    char* traceMessage,
389e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
390e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    const WebRtc_UWord16 writtenSoFar) const
391e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
392e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{
393e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    int length = 0;
394e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if(writtenSoFar >= WEBRTC_TRACE_MAX_MESSAGE_SIZE)
395e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
396e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        return -1;
397e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
398e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // - 2 to leave room for newline and NULL termination
399e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#ifdef _WIN32
400e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    length = _snprintf(traceMessage,
401e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                       WEBRTC_TRACE_MAX_MESSAGE_SIZE - writtenSoFar - 2,
402e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                       "%s",msg);
403e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if(length < 0)
404e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
405e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - writtenSoFar - 2;
406e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        traceMessage[length] = 0;
407e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
408e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#else
409e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    length = snprintf(traceMessage,
410e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                      WEBRTC_TRACE_MAX_MESSAGE_SIZE-writtenSoFar-2, "%s",msg);
411e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if(length < 0 || length > WEBRTC_TRACE_MAX_MESSAGE_SIZE-writtenSoFar - 2)
412e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
413e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - writtenSoFar - 2;
414e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        traceMessage[length] = 0;
415e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
416e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#endif
417e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    // Length with NULL termination.
418e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return length+1;
419e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
420e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
421e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentvoid TraceImpl::AddMessageToList(
422e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    const char traceMessage[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
423e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    const WebRtc_UWord16 length,
424a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    const TraceLevel level) {
425a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin#ifdef WEBRTC_DIRECT_TRACE
426a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    if (_callback) {
427a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin      _callback->Print(level, traceMessage, length);
428a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    }
429a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    return;
430a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin#endif
431a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
432e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    CriticalSectionScoped lock(_critsectArray);
433e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
434e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if(_nextFreeIdx[_activeQueue] >= WEBRTC_TRACE_MAX_QUEUE)
435e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    {
436e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        if( ! _traceFile.Open() &&
437e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            !_callback)
438e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        {
439e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            // Keep at least the last 1/4 of old messages when not logging.
440e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            // TODO (hellner): isn't this redundant. The user will make it known
441e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            //                 when to start logging. Why keep messages before
442e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            //                 that?
443e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            for(int n = 0; n < WEBRTC_TRACE_MAX_QUEUE/4; n++)
444e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            {
445e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                const int lastQuarterOffset = (3*WEBRTC_TRACE_MAX_QUEUE/4);
446e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                memcpy(_messageQueue[_activeQueue][n],
447e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                       _messageQueue[_activeQueue][n + lastQuarterOffset],
448e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent                       WEBRTC_TRACE_MAX_MESSAGE_SIZE);
449e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            }
450e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            _nextFreeIdx[_activeQueue] = WEBRTC_TRACE_MAX_QUEUE/4;
451e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        } else {
452e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            // More messages are being written than there is room for in the
453e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            // buffer. Drop any new messages.
454e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            // TODO (hellner): its probably better to drop old messages instead
455e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            //                 of new ones. One step further: if this happens
456e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            //                 it's due to writing faster than what can be
457e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            //                 processed. Maybe modify the filter at this point.
458e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            //                 E.g. turn of STREAM.
459e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent            return;
460e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        }
461e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
462e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
463