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 "NSPRLogModulesParser.h"
9 #include "mozilla/Tokenizer.h"
11 const char kDelimiters
[] = ", ";
12 const char kAdditionalWordChars
[] = "_-.*";
16 void NSPRLogModulesParser(
17 const char* aLogModules
,
18 const std::function
<void(const char*, LogLevel
, int32_t)>& aCallback
) {
23 Tokenizer
parser(aLogModules
, kDelimiters
, kAdditionalWordChars
);
24 nsAutoCString moduleName
;
26 Tokenizer::Token rustModSep
=
27 parser
.AddCustomToken("::", Tokenizer::CASE_SENSITIVE
);
29 auto readModuleName
= [&](nsAutoCString
& moduleName
) -> bool {
30 moduleName
.Truncate();
31 nsDependentCSubstring sub
;
33 // If the name doesn't include at least one word, we've reached the end.
34 if (!parser
.ReadWord(sub
)) {
37 // We will exit this loop if when any of the condition fails
38 while (parser
.Check(rustModSep
) && parser
.ReadWord(sub
)) {
40 // Claim will include characters of the last sucessfully read item
41 parser
.Claim(moduleName
, Tokenizer::INCLUDE_LAST
);
45 // Format: LOG_MODULES="Foo:2,Bar, Baz:5,rust_crate::mod::file:5"
46 while (readModuleName(moduleName
)) {
47 // Next should be :<level>, default to Error if not provided.
48 LogLevel logLevel
= LogLevel::Error
;
49 int32_t levelValue
= 0;
50 if (parser
.CheckChar(':')) {
51 // NB: If a level isn't provided after the ':' we assume the default
52 // Error level is desired. This differs from NSPR which will stop
53 // processing the log module string in this case.
54 if (parser
.ReadSignedInteger(&levelValue
)) {
55 logLevel
= ToLogLevel(levelValue
);
59 aCallback(moduleName
.get(), logLevel
, levelValue
);
61 // Skip ahead to the next token.
66 } // namespace mozilla