summaryrefslogtreecommitdiff
path: root/jstests/libs/log.js
blob: d0ad6f48c36d7a695ff4d74c50a4c68202cdcc2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

// Yields every logline that contains the specified fields. The regex escape function used here is
// drawn from the following:
// https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
// https://github.com/ljharb/regexp.escape
function* findMatchingLogLines(logLines, fields, ignoreFields) {
    ignoreFields = ignoreFields || [];
    function escapeRegex(input) {
        return (typeof input === "string" ? input.replace(/[\^\$\\\.\*\+\?\(\)\[\]\{\}]/g, '\\$&')
                                          : input);
    }

    function lineMatches(line, fields, ignoreFields) {
        const fieldNames =
            Object.keys(fields).filter((fieldName) => !ignoreFields.includes(fieldName));
        return fieldNames.every((fieldName) => {
            const fieldValue = fields[fieldName];
            let regex;
            const booleanFields = [
                'cursorExhausted',
                'upsert',
                'hasSortStage',
                'usedDisk',
                'cursorExhausted',
                'cursorExhausted'
            ];

            // Command is a special case since it is the first arg of the message, not a
            // separate field
            if (fieldName === "command") {
                let commandName = fieldValue;

                // These commands can be sent camelCase or lower case but shell sends them lower
                // case
                if (fieldValue === "findAndModify" || fieldValue === "mapReduce") {
                    commandName = fieldValue.toLowerCase();
                }

                regex = `"command":{"${commandName}`;
            } else if (fieldName === "insert" && fieldValue.indexOf("|") != -1) {
                // Match new and legacy insert
                regex = `("insert","ns":"(${fieldValue})"|("insert":"(${fieldValue})"))`;
            } else if (booleanFields.find(f => f === fieldName) && fieldValue == 1) {
                regex = `"${fieldName}":true`;
            } else {
                regex = "\"" + escapeRegex(fieldName) + "\":(" +
                    escapeRegex(checkLog.formatAsJsonLogLine(fieldValue)) + "|" +
                    escapeRegex(checkLog.formatAsJsonLogLine(fieldValue, true)) + ")";
            }
            const match = line.match(regex);
            return match && match[0];
        });
    }

    for (const line of logLines) {
        if (lineMatches(line, fields, ignoreFields)) {
            yield line;
        }
    }
}
// Finds and returns a logline containing all the specified fields, or null if no such logline
// was found.
function findMatchingLogLine(logLines, fields, ignoreFields) {
    for (const line of findMatchingLogLines(logLines, fields, ignoreFields)) {
        return line;
    }
    return null;
}