niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/trace_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <cassert> |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 14 | #include <stdarg.h> |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
| 18 | #ifdef _WIN32 |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/source/trace_win.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #else |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/source/trace_posix.h" |
| 22 | #endif // _WIN32 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/interface/sleep.h" |
hta@webrtc.org | 41adcdb | 2012-06-18 11:24:57 +0000 | [diff] [blame] | 25 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | #define KEY_LEN_CHARS 31 |
| 27 | |
| 28 | #ifdef _WIN32 |
andrew@webrtc.org | 5dffebc | 2012-08-16 04:24:05 +0000 | [diff] [blame] | 29 | #pragma warning(disable:4355) |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 30 | #endif // _WIN32 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 33 | |
| 34 | static WebRtc_UWord32 level_filter = kTraceDefault; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
| 36 | // Construct On First Use idiom. Avoids "static initialization order fiasco". |
henrike@webrtc.org | 315282c | 2011-12-09 17:46:20 +0000 | [diff] [blame] | 37 | TraceImpl* TraceImpl::StaticInstance(CountOperation count_operation, |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 38 | const TraceLevel level) { |
| 39 | // Sanities to avoid taking lock unless absolutely necessary (for |
| 40 | // performance reasons). count_operation == kAddRefNoCreate implies that a |
| 41 | // message will be written to file. |
| 42 | if ((level != kTraceAll) && (count_operation == kAddRefNoCreate)) { |
| 43 | if (!(level & level_filter)) { |
| 44 | return NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 46 | } |
| 47 | TraceImpl* impl = |
| 48 | GetStaticInstance<TraceImpl>(count_operation); |
| 49 | return impl; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 52 | TraceImpl* TraceImpl::GetTrace(const TraceLevel level) { |
| 53 | return StaticInstance(kAddRefNoCreate, level); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | } |
| 55 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 56 | TraceImpl* TraceImpl::CreateInstance() { |
henrike@webrtc.org | 2f47b5a | 2011-12-10 00:44:47 +0000 | [diff] [blame] | 57 | #if defined(_WIN32) |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 58 | return new TraceWindows(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | #else |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 60 | return new TracePosix(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | #endif |
| 62 | } |
| 63 | |
| 64 | TraceImpl::TraceImpl() |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 65 | : critsect_interface_(CriticalSectionWrapper::CreateCriticalSection()), |
| 66 | callback_(NULL), |
| 67 | row_count_text_(0), |
| 68 | file_count_text_(0), |
| 69 | trace_file_(*FileWrapper::Create()), |
| 70 | thread_(*ThreadWrapper::CreateThread(TraceImpl::Run, this, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | kHighestPriority, "Trace")), |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 72 | event_(*EventWrapper::Create()), |
| 73 | critsect_array_(CriticalSectionWrapper::CreateCriticalSection()), |
| 74 | next_free_idx_(), |
| 75 | level_(), |
| 76 | length_(), |
| 77 | message_queue_(), |
| 78 | active_queue_(0) { |
| 79 | next_free_idx_[0] = 0; |
| 80 | next_free_idx_[1] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 82 | unsigned int tid = 0; |
| 83 | thread_.Start(tid); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 85 | for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) { |
| 86 | for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) { |
| 87 | message_queue_[m][n] = new |
| 88 | char[WEBRTC_TRACE_MAX_MESSAGE_SIZE]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 90 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | } |
| 92 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 93 | bool TraceImpl::StopThread() { |
| 94 | // Release the worker thread so that it can flush any lingering messages. |
| 95 | event_.Set(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 97 | // Allow 10 ms for pending messages to be flushed out. |
| 98 | // TODO(hellner): why not use condition variables to do this? Or let the |
| 99 | // worker thread die and let this thread flush remaining |
| 100 | // messages? |
| 101 | SleepMs(10); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 103 | thread_.SetNotAlive(); |
| 104 | // Make sure the thread finishes as quickly as possible (instead of having |
| 105 | // to wait for the timeout). |
| 106 | event_.Set(); |
| 107 | bool stopped = thread_.Stop(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 109 | CriticalSectionScoped lock(critsect_interface_); |
| 110 | trace_file_.Flush(); |
| 111 | trace_file_.CloseFile(); |
| 112 | return stopped; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | } |
| 114 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 115 | TraceImpl::~TraceImpl() { |
| 116 | StopThread(); |
| 117 | delete &event_; |
| 118 | delete &trace_file_; |
| 119 | delete &thread_; |
| 120 | delete critsect_interface_; |
| 121 | delete critsect_array_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 123 | for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) { |
| 124 | for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) { |
| 125 | delete [] message_queue_[m][n]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 127 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | } |
| 129 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 130 | WebRtc_Word32 TraceImpl::AddThreadId(char* trace_message) const { |
| 131 | WebRtc_UWord32 thread_id = ThreadWrapper::GetThreadId(); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 132 | // Messages is 12 characters. |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 133 | return sprintf(trace_message, "%10u; ", thread_id); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 134 | } |
| 135 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 136 | WebRtc_Word32 TraceImpl::AddLevel(char* sz_message, |
| 137 | const TraceLevel level) const { |
| 138 | const int kMessageLength = 12; |
| 139 | switch (level) { |
| 140 | case kTraceTerseInfo: |
| 141 | // Add the appropriate amount of whitespace. |
| 142 | memset(sz_message, ' ', kMessageLength); |
| 143 | sz_message[kMessageLength] = '\0'; |
| 144 | break; |
| 145 | case kTraceStateInfo: |
| 146 | sprintf(sz_message, "STATEINFO ; "); |
| 147 | break; |
| 148 | case kTraceWarning: |
| 149 | sprintf(sz_message, "WARNING ; "); |
| 150 | break; |
| 151 | case kTraceError: |
| 152 | sprintf(sz_message, "ERROR ; "); |
| 153 | break; |
| 154 | case kTraceCritical: |
| 155 | sprintf(sz_message, "CRITICAL ; "); |
| 156 | break; |
| 157 | case kTraceInfo: |
| 158 | sprintf(sz_message, "DEBUGINFO ; "); |
| 159 | break; |
| 160 | case kTraceModuleCall: |
| 161 | sprintf(sz_message, "MODULECALL; "); |
| 162 | break; |
| 163 | case kTraceMemory: |
| 164 | sprintf(sz_message, "MEMORY ; "); |
| 165 | break; |
| 166 | case kTraceTimer: |
| 167 | sprintf(sz_message, "TIMER ; "); |
| 168 | break; |
| 169 | case kTraceStream: |
| 170 | sprintf(sz_message, "STREAM ; "); |
| 171 | break; |
| 172 | case kTraceApiCall: |
| 173 | sprintf(sz_message, "APICALL ; "); |
| 174 | break; |
| 175 | case kTraceDebug: |
| 176 | sprintf(sz_message, "DEBUG ; "); |
| 177 | break; |
| 178 | default: |
| 179 | assert(false); |
| 180 | return 0; |
| 181 | } |
| 182 | // All messages are 12 characters. |
| 183 | return kMessageLength; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 186 | WebRtc_Word32 TraceImpl::AddModuleAndId(char* trace_message, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | const TraceModule module, |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 188 | const WebRtc_Word32 id) const { |
| 189 | // Use long int to prevent problems with different definitions of |
| 190 | // WebRtc_Word32. |
| 191 | // TODO(hellner): is this actually a problem? If so, it should be better to |
| 192 | // clean up WebRtc_Word32 |
| 193 | const long int idl = id; |
| 194 | const int kMessageLength = 25; |
| 195 | if (idl != -1) { |
| 196 | const unsigned long int id_engine = id >> 16; |
| 197 | const unsigned long int id_channel = id & 0xffff; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 199 | switch (module) { |
| 200 | case kTraceUndefined: |
| 201 | // Add the appropriate amount of whitespace. |
| 202 | memset(trace_message, ' ', kMessageLength); |
| 203 | trace_message[kMessageLength] = '\0'; |
| 204 | break; |
| 205 | case kTraceVoice: |
| 206 | sprintf(trace_message, " VOICE:%5ld %5ld;", id_engine, |
| 207 | id_channel); |
| 208 | break; |
| 209 | case kTraceVideo: |
| 210 | sprintf(trace_message, " VIDEO:%5ld %5ld;", id_engine, |
| 211 | id_channel); |
| 212 | break; |
| 213 | case kTraceUtility: |
| 214 | sprintf(trace_message, " UTILITY:%5ld %5ld;", id_engine, |
| 215 | id_channel); |
| 216 | break; |
| 217 | case kTraceRtpRtcp: |
| 218 | sprintf(trace_message, " RTP/RTCP:%5ld %5ld;", id_engine, |
| 219 | id_channel); |
| 220 | break; |
| 221 | case kTraceTransport: |
| 222 | sprintf(trace_message, " TRANSPORT:%5ld %5ld;", id_engine, |
| 223 | id_channel); |
| 224 | break; |
| 225 | case kTraceAudioCoding: |
| 226 | sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine, |
| 227 | id_channel); |
| 228 | break; |
| 229 | case kTraceSrtp: |
| 230 | sprintf(trace_message, " SRTP:%5ld %5ld;", id_engine, |
| 231 | id_channel); |
| 232 | break; |
| 233 | case kTraceAudioMixerServer: |
| 234 | sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine, |
| 235 | id_channel); |
| 236 | break; |
| 237 | case kTraceAudioMixerClient: |
| 238 | sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine, |
| 239 | id_channel); |
| 240 | break; |
| 241 | case kTraceVideoCoding: |
| 242 | sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine, |
| 243 | id_channel); |
| 244 | break; |
| 245 | case kTraceVideoMixer: |
| 246 | // Print sleep time and API call |
| 247 | sprintf(trace_message, " VIDEO MIX:%5ld %5ld;", id_engine, |
| 248 | id_channel); |
| 249 | break; |
| 250 | case kTraceFile: |
| 251 | sprintf(trace_message, " FILE:%5ld %5ld;", id_engine, |
| 252 | id_channel); |
| 253 | break; |
| 254 | case kTraceAudioProcessing: |
| 255 | sprintf(trace_message, " AUDIO PROC:%5ld %5ld;", id_engine, |
| 256 | id_channel); |
| 257 | break; |
| 258 | case kTraceAudioDevice: |
| 259 | sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine, |
| 260 | id_channel); |
| 261 | break; |
| 262 | case kTraceVideoRenderer: |
| 263 | sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine, |
| 264 | id_channel); |
| 265 | break; |
| 266 | case kTraceVideoCapture: |
| 267 | sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine, |
| 268 | id_channel); |
| 269 | break; |
| 270 | case kTraceVideoPreocessing: |
| 271 | sprintf(trace_message, " VIDEO PROC:%5ld %5ld;", id_engine, |
| 272 | id_channel); |
| 273 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 274 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 275 | } else { |
| 276 | switch (module) { |
| 277 | case kTraceUndefined: |
| 278 | // Add the appropriate amount of whitespace. |
| 279 | memset(trace_message, ' ', kMessageLength); |
| 280 | trace_message[kMessageLength] = '\0'; |
| 281 | break; |
| 282 | case kTraceVoice: |
| 283 | sprintf(trace_message, " VOICE:%11ld;", idl); |
| 284 | break; |
| 285 | case kTraceVideo: |
| 286 | sprintf(trace_message, " VIDEO:%11ld;", idl); |
| 287 | break; |
| 288 | case kTraceUtility: |
| 289 | sprintf(trace_message, " UTILITY:%11ld;", idl); |
| 290 | break; |
| 291 | case kTraceRtpRtcp: |
| 292 | sprintf(trace_message, " RTP/RTCP:%11ld;", idl); |
| 293 | break; |
| 294 | case kTraceTransport: |
| 295 | sprintf(trace_message, " TRANSPORT:%11ld;", idl); |
| 296 | break; |
| 297 | case kTraceAudioCoding: |
| 298 | sprintf(trace_message, "AUDIO CODING:%11ld;", idl); |
| 299 | break; |
| 300 | case kTraceSrtp: |
| 301 | sprintf(trace_message, " SRTP:%11ld;", idl); |
| 302 | break; |
| 303 | case kTraceAudioMixerServer: |
| 304 | sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl); |
| 305 | break; |
| 306 | case kTraceAudioMixerClient: |
| 307 | sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl); |
| 308 | break; |
| 309 | case kTraceVideoCoding: |
| 310 | sprintf(trace_message, "VIDEO CODING:%11ld;", idl); |
| 311 | break; |
| 312 | case kTraceVideoMixer: |
| 313 | sprintf(trace_message, " VIDEO MIX:%11ld;", idl); |
| 314 | break; |
| 315 | case kTraceFile: |
| 316 | sprintf(trace_message, " FILE:%11ld;", idl); |
| 317 | break; |
| 318 | case kTraceAudioProcessing: |
| 319 | sprintf(trace_message, " AUDIO PROC:%11ld;", idl); |
| 320 | break; |
| 321 | case kTraceAudioDevice: |
| 322 | sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl); |
| 323 | break; |
| 324 | case kTraceVideoRenderer: |
| 325 | sprintf(trace_message, "VIDEO RENDER:%11ld;", idl); |
| 326 | break; |
| 327 | case kTraceVideoCapture: |
| 328 | sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl); |
| 329 | break; |
| 330 | case kTraceVideoPreocessing: |
| 331 | sprintf(trace_message, " VIDEO PROC:%11ld;", idl); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | return kMessageLength; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 336 | } |
| 337 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 338 | WebRtc_Word32 TraceImpl::SetTraceFileImpl(const char* file_name_utf8, |
| 339 | const bool add_file_counter) { |
| 340 | CriticalSectionScoped lock(critsect_interface_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 341 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 342 | trace_file_.Flush(); |
| 343 | trace_file_.CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 344 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 345 | if (file_name_utf8) { |
| 346 | if (add_file_counter) { |
| 347 | file_count_text_ = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 348 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 349 | char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize]; |
| 350 | CreateFileName(file_name_utf8, file_name_with_counter_utf8, |
| 351 | file_count_text_); |
| 352 | if (trace_file_.OpenFile(file_name_with_counter_utf8, false, false, |
| 353 | true) == -1) { |
| 354 | return -1; |
| 355 | } |
| 356 | } else { |
| 357 | file_count_text_ = 0; |
| 358 | if (trace_file_.OpenFile(file_name_utf8, false, false, true) == -1) { |
| 359 | return -1; |
| 360 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 361 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 362 | } |
| 363 | row_count_text_ = 0; |
| 364 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | WebRtc_Word32 TraceImpl::TraceFileImpl( |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 368 | char file_name_utf8[FileWrapper::kMaxFileNameSize]) { |
| 369 | CriticalSectionScoped lock(critsect_interface_); |
| 370 | return trace_file_.FileName(file_name_utf8, FileWrapper::kMaxFileNameSize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 371 | } |
| 372 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 373 | WebRtc_Word32 TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) { |
| 374 | CriticalSectionScoped lock(critsect_interface_); |
| 375 | callback_ = callback; |
| 376 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | WebRtc_Word32 TraceImpl::AddMessage( |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 380 | char* trace_message, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 381 | const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE], |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 382 | const WebRtc_UWord16 written_so_far) const { |
| 383 | int length = 0; |
| 384 | if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) { |
| 385 | return -1; |
| 386 | } |
| 387 | // - 2 to leave room for newline and NULL termination. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 388 | #ifdef _WIN32 |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 389 | length = _snprintf(trace_message, |
| 390 | WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2, |
| 391 | "%s", msg); |
| 392 | if (length < 0) { |
| 393 | length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2; |
| 394 | trace_message[length] = 0; |
| 395 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 396 | #else |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 397 | length = snprintf(trace_message, |
| 398 | WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2, |
| 399 | "%s", msg); |
| 400 | if (length < 0 || |
| 401 | length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) { |
| 402 | length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2; |
| 403 | trace_message[length] = 0; |
| 404 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 405 | #endif |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 406 | // Length with NULL termination. |
| 407 | return length + 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void TraceImpl::AddMessageToList( |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 411 | const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE], |
| 412 | const WebRtc_UWord16 length, |
| 413 | const TraceLevel level) { |
pwestin@webrtc.org | 27fe1b7 | 2012-04-04 08:08:30 +0000 | [diff] [blame] | 414 | #ifdef WEBRTC_DIRECT_TRACE |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 415 | if (callback_) { |
| 416 | callback_->Print(level, trace_message, length); |
| 417 | } |
| 418 | return; |
pwestin@webrtc.org | 27fe1b7 | 2012-04-04 08:08:30 +0000 | [diff] [blame] | 419 | #endif |
| 420 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 421 | CriticalSectionScoped lock(critsect_array_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 422 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 423 | if (next_free_idx_[active_queue_] >= WEBRTC_TRACE_MAX_QUEUE) { |
| 424 | if (!trace_file_.Open() && !callback_) { |
| 425 | // Keep at least the last 1/4 of old messages when not logging. |
| 426 | // TODO(hellner): isn't this redundant. The user will make it known |
| 427 | // when to start logging. Why keep messages before |
| 428 | // that? |
| 429 | for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE / 4; ++n) { |
| 430 | const int last_quarter_offset = (3 * WEBRTC_TRACE_MAX_QUEUE / 4); |
| 431 | memcpy(message_queue_[active_queue_][n], |
| 432 | message_queue_[active_queue_][n + last_quarter_offset], |
| 433 | WEBRTC_TRACE_MAX_MESSAGE_SIZE); |
| 434 | } |
| 435 | next_free_idx_[active_queue_] = WEBRTC_TRACE_MAX_QUEUE / 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 436 | } else { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 437 | // More messages are being written than there is room for in the |
| 438 | // buffer. Drop any new messages. |
| 439 | // TODO(hellner): its probably better to drop old messages instead |
| 440 | // of new ones. One step further: if this happens |
| 441 | // it's due to writing faster than what can be |
| 442 | // processed. Maybe modify the filter at this point. |
| 443 | // E.g. turn of STREAM. |
| 444 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 445 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | WebRtc_UWord16 idx = next_free_idx_[active_queue_]; |
| 449 | next_free_idx_[active_queue_]++; |
| 450 | |
| 451 | level_[active_queue_][idx] = level; |
| 452 | length_[active_queue_][idx] = length; |
| 453 | memcpy(message_queue_[active_queue_][idx], trace_message, length); |
| 454 | |
| 455 | if (next_free_idx_[active_queue_] == WEBRTC_TRACE_MAX_QUEUE - 1) { |
| 456 | // Logging more messages than can be worked off. Log a warning. |
| 457 | const char warning_msg[] = "WARNING MISSING TRACE MESSAGES\n"; |
| 458 | level_[active_queue_][next_free_idx_[active_queue_]] = kTraceWarning; |
| 459 | length_[active_queue_][next_free_idx_[active_queue_]] = strlen(warning_msg); |
| 460 | memcpy(message_queue_[active_queue_][next_free_idx_[active_queue_]], |
| 461 | warning_msg, strlen(warning_msg)); |
| 462 | next_free_idx_[active_queue_]++; |
| 463 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 464 | } |
| 465 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 466 | bool TraceImpl::Run(void* obj) { |
| 467 | return static_cast<TraceImpl*>(obj)->Process(); |
| 468 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 469 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 470 | bool TraceImpl::Process() { |
| 471 | if (event_.Wait(1000) == kEventSignaled) { |
| 472 | if (trace_file_.Open() || callback_) { |
| 473 | // File mode (not callback mode). |
| 474 | WriteToFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 475 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 476 | } else { |
| 477 | trace_file_.Flush(); |
| 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | void TraceImpl::WriteToFile() { |
| 483 | WebRtc_UWord8 local_queue_active = 0; |
| 484 | WebRtc_UWord16 local_next_free_idx = 0; |
| 485 | |
| 486 | // There are two buffers. One for reading (for writing to file) and one for |
| 487 | // writing (for storing new messages). Let new messages be posted to the |
| 488 | // unused buffer so that the current buffer can be flushed safely. |
| 489 | { |
| 490 | CriticalSectionScoped lock(critsect_array_); |
| 491 | local_next_free_idx = next_free_idx_[active_queue_]; |
| 492 | next_free_idx_[active_queue_] = 0; |
| 493 | local_queue_active = active_queue_; |
| 494 | if (active_queue_ == 0) { |
| 495 | active_queue_ = 1; |
| 496 | } else { |
| 497 | active_queue_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 498 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 499 | } |
| 500 | if (local_next_free_idx == 0) { |
| 501 | return; |
| 502 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 503 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 504 | CriticalSectionScoped lock(critsect_interface_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 505 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 506 | for (WebRtc_UWord16 idx = 0; idx < local_next_free_idx; ++idx) { |
| 507 | TraceLevel local_level = level_[local_queue_active][idx]; |
| 508 | if (callback_) { |
| 509 | callback_->Print(local_level, message_queue_[local_queue_active][idx], |
| 510 | length_[local_queue_active][idx]); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 512 | if (trace_file_.Open()) { |
| 513 | if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) { |
| 514 | // wrap file |
| 515 | row_count_text_ = 0; |
| 516 | trace_file_.Flush(); |
| 517 | |
| 518 | if (file_count_text_ == 0) { |
| 519 | trace_file_.Rewind(); |
| 520 | } else { |
| 521 | char old_file_name[FileWrapper::kMaxFileNameSize]; |
| 522 | char new_file_name[FileWrapper::kMaxFileNameSize]; |
| 523 | |
| 524 | // get current name |
| 525 | trace_file_.FileName(old_file_name, |
| 526 | FileWrapper::kMaxFileNameSize); |
| 527 | trace_file_.CloseFile(); |
| 528 | |
| 529 | file_count_text_++; |
| 530 | |
| 531 | UpdateFileName(old_file_name, new_file_name, file_count_text_); |
| 532 | |
| 533 | if (trace_file_.OpenFile(new_file_name, false, false, |
| 534 | true) == -1) { |
| 535 | return; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | if (row_count_text_ == 0) { |
| 540 | char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1]; |
| 541 | WebRtc_Word32 length = AddDateTimeInfo(message); |
| 542 | if (length != -1) { |
| 543 | message[length] = 0; |
| 544 | message[length - 1] = '\n'; |
| 545 | trace_file_.Write(message, length); |
| 546 | row_count_text_++; |
| 547 | } |
| 548 | length = AddBuildInfo(message); |
| 549 | if (length != -1) { |
| 550 | message[length + 1] = 0; |
| 551 | message[length] = '\n'; |
| 552 | message[length - 1] = '\n'; |
| 553 | trace_file_.Write(message, length + 1); |
| 554 | row_count_text_++; |
| 555 | row_count_text_++; |
| 556 | } |
| 557 | } |
| 558 | WebRtc_UWord16 length = length_[local_queue_active][idx]; |
| 559 | message_queue_[local_queue_active][idx][length] = 0; |
| 560 | message_queue_[local_queue_active][idx][length - 1] = '\n'; |
| 561 | trace_file_.Write(message_queue_[local_queue_active][idx], length); |
| 562 | row_count_text_++; |
| 563 | } |
| 564 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void TraceImpl::AddImpl(const TraceLevel level, const TraceModule module, |
| 568 | const WebRtc_Word32 id, |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 569 | const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) { |
| 570 | if (TraceCheck(level)) { |
| 571 | char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE]; |
| 572 | char* message_ptr = trace_message; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 573 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 574 | WebRtc_Word32 len = 0; |
| 575 | WebRtc_Word32 ack_len = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 576 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 577 | len = AddLevel(message_ptr, level); |
| 578 | if (len == -1) { |
| 579 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 580 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 581 | message_ptr += len; |
| 582 | ack_len += len; |
| 583 | |
| 584 | len = AddTime(message_ptr, level); |
| 585 | if (len == -1) { |
| 586 | return; |
| 587 | } |
| 588 | message_ptr += len; |
| 589 | ack_len += len; |
| 590 | |
| 591 | len = AddModuleAndId(message_ptr, module, id); |
| 592 | if (len == -1) { |
| 593 | return; |
| 594 | } |
| 595 | message_ptr += len; |
| 596 | ack_len += len; |
| 597 | |
| 598 | len = AddThreadId(message_ptr); |
| 599 | if (len < 0) { |
| 600 | return; |
| 601 | } |
| 602 | message_ptr += len; |
| 603 | ack_len += len; |
| 604 | |
| 605 | len = AddMessage(message_ptr, msg, (WebRtc_UWord16)ack_len); |
| 606 | if (len == -1) { |
| 607 | return; |
| 608 | } |
| 609 | ack_len += len; |
| 610 | AddMessageToList(trace_message, (WebRtc_UWord16)ack_len, level); |
| 611 | |
| 612 | // Make sure that messages are written as soon as possible. |
| 613 | event_.Set(); |
| 614 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 615 | } |
| 616 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 617 | bool TraceImpl::TraceCheck(const TraceLevel level) const { |
| 618 | return (level & level_filter) ? true : false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | bool TraceImpl::UpdateFileName( |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 622 | const char file_name_utf8[FileWrapper::kMaxFileNameSize], |
| 623 | char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize], |
| 624 | const WebRtc_UWord32 new_count) const { |
| 625 | WebRtc_Word32 length = (WebRtc_Word32)strlen(file_name_utf8); |
| 626 | if (length < 0) { |
| 627 | return false; |
| 628 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 629 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 630 | WebRtc_Word32 length_without_file_ending = length - 1; |
| 631 | while (length_without_file_ending > 0) { |
| 632 | if (file_name_utf8[length_without_file_ending] == '.') { |
| 633 | break; |
| 634 | } else { |
| 635 | length_without_file_ending--; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 636 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 637 | } |
| 638 | if (length_without_file_ending == 0) { |
| 639 | length_without_file_ending = length; |
| 640 | } |
| 641 | WebRtc_Word32 length_to_ = length_without_file_ending - 1; |
| 642 | while (length_to_ > 0) { |
| 643 | if (file_name_utf8[length_to_] == '_') { |
| 644 | break; |
| 645 | } else { |
| 646 | length_to_--; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 647 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 648 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 649 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 650 | memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_); |
| 651 | sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s", |
| 652 | static_cast<long unsigned int>(new_count), |
| 653 | file_name_utf8 + length_without_file_ending); |
| 654 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | bool TraceImpl::CreateFileName( |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 658 | const char file_name_utf8[FileWrapper::kMaxFileNameSize], |
| 659 | char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize], |
| 660 | const WebRtc_UWord32 new_count) const { |
| 661 | WebRtc_Word32 length = (WebRtc_Word32)strlen(file_name_utf8); |
| 662 | if (length < 0) { |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | WebRtc_Word32 length_without_file_ending = length - 1; |
| 667 | while (length_without_file_ending > 0) { |
| 668 | if (file_name_utf8[length_without_file_ending] == '.') { |
| 669 | break; |
| 670 | } else { |
| 671 | length_without_file_ending--; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 672 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 673 | } |
| 674 | if (length_without_file_ending == 0) { |
| 675 | length_without_file_ending = length; |
| 676 | } |
| 677 | memcpy(file_name_with_counter_utf8, file_name_utf8, |
| 678 | length_without_file_ending); |
| 679 | sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s", |
| 680 | static_cast<long unsigned int>(new_count), |
| 681 | file_name_utf8 + length_without_file_ending); |
| 682 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 683 | } |
| 684 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 685 | void Trace::CreateTrace() { |
| 686 | TraceImpl::StaticInstance(kAddRef); |
tommi@webrtc.org | cde1e7f | 2011-11-15 12:23:36 +0000 | [diff] [blame] | 687 | } |
| 688 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 689 | void Trace::ReturnTrace() { |
| 690 | TraceImpl::StaticInstance(kRelease); |
tommi@webrtc.org | cde1e7f | 2011-11-15 12:23:36 +0000 | [diff] [blame] | 691 | } |
| 692 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 693 | WebRtc_Word32 Trace::SetLevelFilter(WebRtc_UWord32 filter) { |
| 694 | level_filter = filter; |
| 695 | return 0; |
tommi@webrtc.org | cde1e7f | 2011-11-15 12:23:36 +0000 | [diff] [blame] | 696 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 697 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 698 | WebRtc_Word32 Trace::LevelFilter(WebRtc_UWord32& filter) { |
| 699 | filter = level_filter; |
| 700 | return 0; |
tommi@webrtc.org | cde1e7f | 2011-11-15 12:23:36 +0000 | [diff] [blame] | 701 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 702 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 703 | WebRtc_Word32 Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) { |
| 704 | TraceImpl* trace = TraceImpl::GetTrace(); |
| 705 | if (trace) { |
| 706 | int ret_val = trace->TraceFileImpl(file_name); |
| 707 | ReturnTrace(); |
| 708 | return ret_val; |
| 709 | } |
| 710 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 711 | } |
| 712 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 713 | WebRtc_Word32 Trace::SetTraceFile(const char* file_name, |
| 714 | const bool add_file_counter) { |
| 715 | TraceImpl* trace = TraceImpl::GetTrace(); |
| 716 | if (trace) { |
| 717 | int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter); |
| 718 | ReturnTrace(); |
| 719 | return ret_val; |
| 720 | } |
| 721 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 722 | } |
| 723 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 724 | WebRtc_Word32 Trace::SetTraceCallback(TraceCallback* callback) { |
| 725 | TraceImpl* trace = TraceImpl::GetTrace(); |
| 726 | if (trace) { |
| 727 | int ret_val = trace->SetTraceCallbackImpl(callback); |
| 728 | ReturnTrace(); |
| 729 | return ret_val; |
| 730 | } |
| 731 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | void Trace::Add(const TraceLevel level, const TraceModule module, |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 735 | const WebRtc_Word32 id, const char* msg, ...) { |
| 736 | TraceImpl* trace = TraceImpl::GetTrace(level); |
| 737 | if (trace) { |
| 738 | if (trace->TraceCheck(level)) { |
| 739 | char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE]; |
| 740 | char* buff = 0; |
| 741 | if (msg) { |
| 742 | va_list args; |
| 743 | va_start(args, msg); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 744 | #ifdef _WIN32 |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 745 | _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 746 | #else |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 747 | vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 748 | #endif |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 749 | va_end(args); |
| 750 | buff = temp_buff; |
| 751 | } |
| 752 | trace->AddImpl(level, module, id, buff); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 753 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 754 | ReturnTrace(); |
| 755 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 756 | } |
tommi@webrtc.org | cde1e7f | 2011-11-15 12:23:36 +0000 | [diff] [blame] | 757 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 758 | } // namespace webrtc |