Backed out changeset bd51857879db (bug 1862957) for causing WakeLock related failures...
[gecko.git] / xpcom / base / LogCommandLineHandler.cpp
blob26e78670e9911e3838d7d356b8242c7ebc036111
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "LogCommandLineHandler.h"
9 #include "mozilla/Tokenizer.h"
10 #include "nsDebug.h"
12 namespace mozilla {
14 void LoggingHandleCommandLineArgs(
15 int argc, char const* const* argv,
16 std::function<void(nsACString const&)> const& consumer) {
17 // Keeps the name of a pending env var (MOZ_LOG or MOZ_LOG_FILE) that
18 // we expect to get a value for in the next iterated arg.
19 // Used for the `-MOZ_LOG <modules>` form of argument.
20 nsAutoCString env;
22 auto const names = {"MOZ_LOG"_ns, "MOZ_LOG_FILE"_ns};
24 for (int arg = 1; arg < argc; ++arg) {
25 Tokenizer p(argv[arg]);
27 if (!env.IsEmpty() && p.CheckChar('-')) {
28 NS_WARNING(
29 "Expects value after -MOZ_LOG(_FILE) argument, but another argument "
30 "found");
32 // We only expect values for the pending env var, start over
33 p.Rollback();
34 env.Truncate();
37 if (env.IsEmpty()) {
38 if (!p.CheckChar('-')) {
39 continue;
41 // We accept `-MOZ_LOG` as well as `--MOZ_LOG`.
42 Unused << p.CheckChar('-');
44 for (auto const& name : names) {
45 if (!p.CheckWord(name)) {
46 continue;
49 env.Assign(name);
50 env.Append('=');
51 break;
54 if (env.IsEmpty()) {
55 // An unknonwn argument, ignore.
56 continue;
59 // We accept `-MOZ_LOG <modules>` as well as `-MOZ_LOG=<modules>`.
61 if (p.CheckEOF()) {
62 // We have a lone `-MOZ_LOG` arg, the next arg is expected to be
63 // the value, |env| is now prepared as `MOZ_LOG=`.
64 continue;
67 if (!p.CheckChar('=')) {
68 // There is a character after the arg name and it's not '=',
69 // ignore this argument.
70 NS_WARNING("-MOZ_LOG(_FILE) argument not in a proper form");
72 env.Truncate();
73 continue;
77 // This can be non-empty from previous iteration or in this iteration.
78 if (!env.IsEmpty()) {
79 nsDependentCSubstring value;
80 Unused << p.ReadUntil(Tokenizer::Token::EndOfFile(), value);
81 env.Append(value);
83 consumer(env);
85 env.Truncate();
90 } // namespace mozilla