Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bring --enable-logging functionality in line with Chromium #25089

Merged
merged 62 commits into from Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
c6bce7d
feat: support ELECTRON_LOG_FILE environment vars.
ckerr Aug 21, 2020
412168d
docs: document ELECTRON_LOG_FILE envvar
ckerr Aug 21, 2020
e7922b8
chore: make lint happy
ckerr Aug 22, 2020
2716c2f
feat: add --log-file --log-level switches.
ckerr Aug 22, 2020
5d46b3f
chore: fix inaccurate code comment
ckerr Aug 22, 2020
612de3d
fix: make cli switches take priority over envvars.
ckerr Aug 22, 2020
db7c7fa
chore: remove unused '#ifdef DEBUG' code branches
ckerr Aug 24, 2020
266846b
chore: set logging_dest only if logging is enabled
ckerr Aug 24, 2020
4957164
refactor: apply API WG's feedback to logging patch
ckerr Aug 25, 2020
5097130
merge 'master' into 'refactor-logging'
ckerr Oct 1, 2020
76f0ba0
Merge branch 'master' into refactor-logging
ckerr Oct 1, 2020
36b7477
Merge remote-tracking branch 'origin/main' into refactor-logging
nornagon Jun 2, 2021
22fbcdc
fix build
nornagon Jun 3, 2021
8fc2a61
fix sandboxed children not being able to write to log dir
nornagon Jun 5, 2021
7184b05
default to stderr
nornagon Jun 5, 2021
0736367
lint
nornagon Jun 5, 2021
063980e
Merge branch 'main' into refactor-logging
nornagon Jun 5, 2021
8c5b84e
fix build
nornagon Jun 7, 2021
0ea0cf8
more fix build
nornagon Jun 7, 2021
312da81
more includes
nornagon Jun 7, 2021
aba4922
base::DIR_APP_DATA is not defined on linux apparently
nornagon Jun 7, 2021
86817d0
fix build on windows
nornagon Jun 7, 2021
4c61513
stop ELECTRON_ENABLE_LOGGING from modifying the command line
nornagon Jun 7, 2021
799fb68
remove debug logs
nornagon Jun 7, 2021
fc87ba5
fiddle with namespacing
nornagon Jun 7, 2021
45addaf
put the electron path provider first
nornagon Jun 8, 2021
be4c340
preinit / reinit
nornagon Jun 9, 2021
f9e6800
update docs now that it is possible to set enable-logging from js
nornagon Jun 9, 2021
5cad579
fix win build
nornagon Jun 9, 2021
9277439
clarify docs
nornagon Jun 9, 2021
9199d35
move DIR_APP_DATA on Linux to path provider
nornagon Jun 9, 2021
9a63924
remove _setDefaultAppPaths
nornagon Jun 9, 2021
ff258a3
fix build
nornagon Jun 9, 2021
6bfdd41
fix linux build
nornagon Jun 9, 2021
6223aad
fix switch syntax
nornagon Jun 9, 2021
cd6be93
add tests
nornagon Jun 10, 2021
7da965b
re-reverse the order of path providers
nornagon Jun 10, 2021
da13a8b
improve assertions in weirdly failing session spec?
nornagon Jun 10, 2021
0e9b727
add some diagnostics for failing windows "do not log by default" test
nornagon Jun 10, 2021
1bfbe17
Merge branch 'main' into refactor-logging
nornagon Jun 11, 2021
8a8f6f4
update docs
nornagon Jun 10, 2021
b8e5b64
remove ELECTRON_ENABLE_LOGGING from the env for appveyor
nornagon Jun 10, 2021
ec91f65
create DIR_USER_DATA and DIR_APP_DICTIONARIES when accessed
nornagon Jun 11, 2021
5436fd7
zygote doesn't need --user-data-dir
nornagon Jun 11, 2021
5f20c17
assert hostname in session spec
nornagon Jun 11, 2021
7dff87f
dedup DIR_CRASH_DUMPS logic, create caches dir
nornagon Jun 11, 2021
8f28930
refactor: use PathProvider for user-data-dir and others
nornagon Jun 11, 2021
3106957
consolidate logic for DIR_RECENT and DIR_APP_LOGS into path provider
nornagon Jun 11, 2021
c215230
fix bad include
nornagon Jun 11, 2021
ceef070
remove base::debug::StackTrace
nornagon Jun 12, 2021
d9f9b31
Revert "remove base::debug::StackTrace"
nornagon Jun 12, 2021
0b09ee0
remove debugging code
nornagon Jun 14, 2021
e42bc4b
fix build on mac
nornagon Jun 14, 2021
8fa30ab
fix build on win
nornagon Jun 14, 2021
92a9e6e
no logging to file in zygotes
nornagon Jun 14, 2021
972d6b4
create app logs dir on both mac and non-mac
nornagon Jun 14, 2021
36d965e
Merge branch 'refactor-paths' into refactor-logging
nornagon Jun 14, 2021
f961c28
Merge remote-tracking branch 'origin/main' into refactor-logging
nornagon Jun 15, 2021
d1f49ba
test that early log messages are not deleted when logging to a given …
nornagon Jun 16, 2021
e2a34d3
lint
nornagon Jun 16, 2021
e82ae30
don't delete log file on reinit when passing --log-file
nornagon Jun 17, 2021
7be93ba
lint
nornagon Jun 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/api/environment-variables.md
Expand Up @@ -126,6 +126,17 @@ When Electron reads from an ASAR file, log the read offset and file path to
the system `tmpdir`. The resulting file can be provided to the ASAR module
to optimize file ordering.

### `ELECTRON_LOG_FILE`

Print Chrome's internal logging to the specified file.
When this is set, `ELECTRON_ENABLE_LOGGING` is treated as true.

### `ELECTRON_LOG_LEVEL`

Set the verbosity level for Chrome's internal logging.
This value should be a number, one of [Chrome's LogSeverities](https://source.chromium.org/chromium/chromium/src/+/master:base/logging.h?q=logging::LogSeverity&ss=chromium).
When this is set, `ELECTRON_ENABLE_LOGGING` is treated as true.

### `ELECTRON_ENABLE_STACK_DUMPING`

Prints the stack trace to the console when Electron crashes.
Expand Down
107 changes: 78 additions & 29 deletions shell/app/electron_main_delegate.cc
Expand Up @@ -12,6 +12,7 @@
#include <glib.h> // for g_setenv()
#endif

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/debug/stack_trace.h"
#include "base/environment.h"
Expand Down Expand Up @@ -73,6 +74,16 @@ namespace {

const char* kRelauncherProcess = "relauncher";

constexpr base::StringPiece kElectronDisableSandbox("ELECTRON_DISABLE_SANDBOX");
constexpr base::StringPiece kElectronEnableLogging("ELECTRON_ENABLE_LOGGING");
constexpr base::StringPiece kElectronEnableStackDumping(
"ELECTRON_ENABLE_STACK_DUMPING");

constexpr base::StringPiece kLogFileEnv("ELECTRON_LOG_FILE");
constexpr base::StringPiece kLogLevelEnv(
"ELECTRON_LOG_LEVEL"); // logging::LogSeverity
constexpr base::StringPiece kLogLevelSwitch("log-level");

bool IsBrowserProcess(base::CommandLine* cmd) {
std::string process_type = cmd->GetSwitchValueASCII(::switches::kProcessType);
return process_type.empty();
Expand Down Expand Up @@ -112,6 +123,70 @@ void InvalidParameterHandler(const wchar_t*,
}
#endif

void InitLogging(base::Environment* env, const base::CommandLine* cmd) {
logging::LoggingSettings settings;
bool logging_requested = false;

#if defined(OS_WIN) && defined(DEBUG)
// Print logging to debug.log on Windows
base::FilePath log_filename;
base::PathService::Get(base::DIR_EXE, &log_filename);
static constexpr base::StringPiece debug_log("debug.log");
log_filename = log_filename.AppendASCII(debug_log);

settings.delete_old = logging::DELETE_OLD_LOG_FILE;
settings.lock_log = logging::LOCK_LOG_FILE;
settings.log_file_path = log_filename.value().c_str();
settings.logging_dest = logging::LOG_TO_ALL;
logging_requested = true;
#endif

// '--log-level' switch and 'ELECTRON_LOG_LEVEL' env
std::string arg;
if (!env->GetVar(kLogLevelEnv, &arg)) {
arg = cmd->GetSwitchValueASCII(kLogLevelSwitch);
}
if (!arg.empty()) {
int level;
if (base::StringToInt(arg, &level)) {
auto const clamped =
base::ClampToRange(level, 0, logging::LOG_NUM_SEVERITIES);
logging::SetMinLogLevel(clamped);
ckerr marked this conversation as resolved.
Show resolved Hide resolved
logging_requested = true;
}
}

// '--log-file' switch and 'ELECTRON_LOG_FILE' env
base::FilePath path;
if (env->GetVar(kLogFileEnv, &arg)) {
#if defined(OS_WIN)
path = base::FilePath(base::UTF8ToWide(arg));
#else
path = base::FilePath(arg);
#endif
} else {
path = cmd->GetSwitchValuePath(::switches::kLogFile);
}
if (!path.empty()) {
settings.log_file_path = path.value().c_str();
settings.logging_dest |= logging::LOG_TO_FILE;
logging_requested = true;
}

logging_requested = logging_requested ||
env->HasVar(kElectronEnableLogging) ||
cmd->HasSwitch(::switches::kEnableLogging);

// don't log unless someone asked for it
if (!logging_requested) {
logging::SetMinLogLevel(logging::LOG_NUM_SEVERITIES);
settings.logging_dest = logging::LOG_NONE;
}

logging::InitLogging(settings);
logging::SetLogItems(true /* pid */, false, true /* timestamp */, false);
}

} // namespace

// TODO(nornagon): move path provider overriding to its own file in
Expand Down Expand Up @@ -183,51 +258,25 @@ const size_t ElectronMainDelegate::kNonWildcardDomainNonPortSchemesSize =
bool ElectronMainDelegate::BasicStartupComplete(int* exit_code) {
auto* command_line = base::CommandLine::ForCurrentProcess();

logging::LoggingSettings settings;
#if defined(OS_WIN)
v8_crashpad_support::SetUp();

// On Windows the terminal returns immediately, so we add a new line to
// prevent output in the same line as the prompt.
if (IsBrowserProcess(command_line))
std::wcout << std::endl;
#if defined(DEBUG)
// Print logging to debug.log on Windows
settings.logging_dest = logging::LOG_TO_ALL;
base::FilePath log_filename;
base::PathService::Get(base::DIR_EXE, &log_filename);
log_filename = log_filename.AppendASCII("debug.log");
settings.log_file_path = log_filename.value().c_str();
settings.lock_log = logging::LOCK_LOG_FILE;
settings.delete_old = logging::DELETE_OLD_LOG_FILE;
#else
settings.logging_dest =
logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
#endif // defined(DEBUG)
#else // defined(OS_WIN)
settings.logging_dest =
logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
#endif // !defined(OS_WIN)

// Only enable logging when --enable-logging is specified.
auto env = base::Environment::Create();
if (!command_line->HasSwitch(::switches::kEnableLogging) &&
!env->HasVar("ELECTRON_ENABLE_LOGGING")) {
settings.logging_dest = logging::LOG_NONE;
logging::SetMinLogLevel(logging::LOG_NUM_SEVERITIES);
}

logging::InitLogging(settings);

// Logging with pid and timestamp.
logging::SetLogItems(true, false, true, false);
InitLogging(env.get(), command_line);

// Enable convient stack printing. This is enabled by default in non-official
// builds.
if (env->HasVar("ELECTRON_ENABLE_STACK_DUMPING"))
if (env->HasVar(kElectronEnableStackDumping))
base::debug::EnableInProcessStackDumping();

if (env->HasVar("ELECTRON_DISABLE_SANDBOX"))
if (env->HasVar(kElectronDisableSandbox))
command_line->AppendSwitch(sandbox::policy::switches::kNoSandbox);

tracing_sampler_profiler_ =
Expand Down