Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/base/LogCommandLineHandler.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#include "LogCommandLineHandler.h"
8
9
#include "mozilla/Tokenizer.h"
10
#include "nsDebug.h"
11
12
namespace mozilla {
13
14
void LoggingHandleCommandLineArgs(int argc, char const* const* argv,
15
                                  std::function<void(nsACString const&)> const& consumer)
16
3
{
17
3
  // Keeps the name of a pending env var (MOZ_LOG or MOZ_LOG_FILE) that
18
3
  // we expect to get a value for in the next iterated arg.
19
3
  // Used for the `-MOZ_LOG <modules>` form of argument.
20
3
  nsAutoCString env;
21
3
22
3
  auto const names = {
23
3
    NS_LITERAL_CSTRING("MOZ_LOG"),
24
3
    NS_LITERAL_CSTRING("MOZ_LOG_FILE")
25
3
  };
26
3
27
15
  for (int arg = 1; arg < argc; ++arg) {
28
12
    Tokenizer p(argv[arg]);
29
12
30
12
    if (!env.IsEmpty() && p.CheckChar('-')) {
31
0
      NS_WARNING("Expects value after -MOZ_LOG(_FILE) argument, but another argument found");
32
0
33
0
      // We only expect values for the pending env var, start over
34
0
      p.Rollback();
35
0
      env.Truncate();
36
0
    }
37
12
38
12
    if (env.IsEmpty()) {
39
12
      if (!p.CheckChar('-')) {
40
3
        continue;
41
3
      }
42
9
      // We accept `-MOZ_LOG` as well as `--MOZ_LOG`.
43
9
      Unused << p.CheckChar('-');
44
9
45
18
      for (auto const& name : names) {
46
18
        if (!p.CheckWord(name)) {
47
18
          continue;
48
18
        }
49
0
50
0
        env.Assign(name);
51
0
        env.Append('=');
52
0
        break;
53
0
      }
54
9
55
9
      if (env.IsEmpty()) {
56
9
        // An unknonwn argument, ignore.
57
9
        continue;
58
9
      }
59
0
60
0
      // We accept `-MOZ_LOG <modules>` as well as `-MOZ_LOG=<modules>`.
61
0
62
0
      if (p.CheckEOF()) {
63
0
        // We have a lone `-MOZ_LOG` arg, the next arg is expected to be
64
0
        // the value, |env| is now prepared as `MOZ_LOG=`.
65
0
        continue;
66
0
      }
67
0
68
0
      if (!p.CheckChar('=')) {
69
0
        // There is a character after the arg name and it's not '=',
70
0
        // ignore this argument.
71
0
        NS_WARNING("-MOZ_LOG(_FILE) argument not in a proper form");
72
0
73
0
        env.Truncate();
74
0
        continue;
75
0
      }
76
0
    }
77
0
78
0
    // This can be non-empty from previous iteration or in this iteration.
79
0
    if (!env.IsEmpty()) {
80
0
      nsDependentCSubstring value;
81
0
      Unused << p.ReadUntil(Tokenizer::Token::EndOfFile(), value);
82
0
      env.Append(value);
83
0
84
0
      consumer(env);
85
0
86
0
      env.Truncate();
87
0
    }
88
0
  }
89
3
}
90
91
} // mozilla