1 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "base/debug_util.h"
6 :
7 : #include "base/platform_thread.h"
8 :
9 : #include <stdarg.h>
10 : #include <stdio.h>
11 : #include <stdlib.h>
12 :
13 : #ifdef OS_WIN
14 : #include <io.h>
15 : #else
16 : #include <unistd.h>
17 : #endif
18 :
19 : #ifndef STDOUT_FILENO
20 : #define STDOUT_FILENO 1
21 : #endif
22 :
23 0 : bool DebugUtil::WaitForDebugger(int wait_seconds, bool silent) {
24 0 : for (int i = 0; i < wait_seconds * 10; ++i) {
25 0 : if (BeingDebugged()) {
26 0 : if (!silent)
27 0 : BreakDebugger();
28 0 : return true;
29 : }
30 0 : PlatformThread::Sleep(100);
31 : }
32 0 : return false;
33 : }
34 :
35 0 : const void *const *StackTrace::Addresses(size_t* count) {
36 0 : *count = trace_.size();
37 0 : if (trace_.size())
38 0 : return &trace_[0];
39 0 : return NULL;
40 : }
41 :
42 : namespace mozilla {
43 :
44 1464 : EnvironmentLog::EnvironmentLog(const char* varname)
45 : {
46 1464 : const char *e = getenv(varname);
47 1464 : if (e && *e)
48 22 : fname_ = e;
49 1464 : }
50 :
51 1487 : EnvironmentLog::~EnvironmentLog()
52 : {
53 1487 : }
54 :
55 : void
56 0 : EnvironmentLog::print(const char* format, ...)
57 : {
58 0 : if (!fname_.size())
59 0 : return;
60 :
61 : FILE* f;
62 0 : if (fname_.compare("-") == 0)
63 0 : f = fdopen(dup(STDOUT_FILENO), "a");
64 : else
65 0 : f = fopen(fname_.c_str(), "a");
66 :
67 0 : if (!f)
68 0 : return;
69 :
70 : va_list a;
71 0 : va_start(a, format);
72 0 : vfprintf(f, format, a);
73 0 : va_end(a);
74 0 : fclose(f);
75 : }
76 :
77 4392 : } // namespace mozilla
|