Followup to bug 525090 - log new processes to a file based on the environment, instead of unconditionally to stdout, r=cjones

This commit is contained in:
Benjamin Smedberg 2009-11-19 14:52:11 -05:00
Родитель fa7b66ff5d
Коммит 49150adb6d
4 изменённых файлов: 76 добавлений и 11 удалений

Просмотреть файл

@ -6,6 +6,16 @@
#include "base/platform_thread.h"
#include <stdlib.h>
#ifdef OS_WIN
#include <io.h>
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
bool DebugUtil::WaitForDebugger(int wait_seconds, bool silent) {
for (int i = 0; i < wait_seconds * 10; ++i) {
if (BeingDebugged()) {
@ -24,3 +34,39 @@ const void *const *StackTrace::Addresses(size_t* count) {
return &trace_[0];
return NULL;
}
namespace mozilla {
EnvironmentLog::EnvironmentLog(const char* varname)
: fd_(NULL)
{
const char *e = getenv(varname);
if (e && *e) {
if (!strcmp(e, "-")) {
fd_ = fdopen(dup(STDOUT_FILENO), "a");
}
else {
fd_ = fopen(e, "a");
}
}
}
EnvironmentLog::~EnvironmentLog()
{
if (fd_)
fclose(fd_);
}
void
EnvironmentLog::print(const char* format, ...)
{
va_list a;
va_start(a, format);
if (fd_) {
vfprintf(fd_, format, a);
fflush(fd_);
}
va_end(a);
}
} // namespace mozilla

Просмотреть файл

@ -67,4 +67,22 @@ class DebugUtil {
#endif // defined(OS_MACOSX)
};
namespace mozilla {
class EnvironmentLog
{
public:
EnvironmentLog(const char* varname);
~EnvironmentLog();
void print(const char* format, ...);
private:
FILE* fd_;
DISALLOW_EVIL_CONSTRUCTORS(EnvironmentLog);
};
} // namespace mozilla
#endif // BASE_DEBUG_UTIL_H_

Просмотреть файл

@ -12,6 +12,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include "base/debug_util.h"
#include "base/eintr_wrapper.h"
#include "base/file_util.h"
#include "base/logging.h"
@ -25,6 +26,8 @@ enum ParsingState {
KEY_VALUE
};
static mozilla::EnvironmentLog gProcessLog("MOZ_PROCESS_LOG");
} // namespace
namespace base {
@ -55,17 +58,12 @@ bool LaunchApp(const std::vector<std::string>& argv,
execvp(argv_cstr[0], argv_cstr.get());
#if defined(CHROMIUM_MOZILLA_BUILD)
// if we get here, we're in serious trouble and should complain loudly
fprintf(stderr,
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n"
"----> FAILED TO exec() CHILD PROCESS <---\n"
"----> path: %s\n"
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
argv_cstr[0]);
DLOG(ERROR) << "FAILED TO exec() CHILD PROCESS, path: " << argv_cstr[0];
#endif
exit(127);
} else {
printf("==> process %d launched child process %d\n",
GetCurrentProcId(), pid);
gProcessLog.print("==> process %d launched child process %d\n",
GetCurrentProcId(), pid);
if (wait)
HANDLE_EINTR(waitpid(pid, 0, 0));

Просмотреть файл

@ -8,6 +8,7 @@
#include <winternl.h>
#include <psapi.h>
#include "base/debug_util.h"
#include "base/histogram.h"
#include "base/logging.h"
#include "base/scoped_handle_win.h"
@ -21,6 +22,8 @@ const int PAGESIZE_KB = 4;
// HeapSetInformation function pointer.
typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T);
static mozilla::EnvironmentLog gProcessLog("MOZ_PROCESS_LOG");
} // namespace
namespace base {
@ -162,9 +165,9 @@ bool LaunchApp(const std::wstring& cmdline,
&startup_info, &process_info))
return false;
printf("==> process %d launched child process %d\n",
GetCurrentProcId(),
process_info.dwProcessId);
gProcessLog.print("==> process %d launched child process %d\n",
GetCurrentProcId(),
process_info.dwProcessId);
// Handles must be closed or they will leak
CloseHandle(process_info.hThread);