log_utils.cc 2.43 KB
Newer Older
1
2
3
4
5
6
7
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <testutils/log_utils.h>
8
#include <iostream>
9
10
11
12
13

namespace isc {
namespace dhcp {
namespace test {

14
15
LogContentTest::LogContentTest()
    :verbose_(false) {
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
    // Get rid of any old files
    remFile();

    // Set up the logger for use in checking the debug statements.
    // We send the debug statements to a file which we can
    // check after the evaluations have completed.  We also
    // set the log severity and debug levels so that the log
    // statements are executed.
    LoggerSpecification spec(getRootLoggerName(),
                             keaLoggerSeverity(isc::log::DEBUG),
                             keaLoggerDbglevel(isc::log::MAX_DEBUG_LEVEL));
    OutputOption option;
    option.destination = OutputOption::DEST_FILE;
    option.filename = string(LogContentTest::LOG_FILE);
    spec.addOutputOption(option);
    LoggerManager manager;
    manager.process(spec);
}

LogContentTest:: ~LogContentTest() {
    remFile();
}

bool LogContentTest::checkFile() {
    ifstream file(LOG_FILE);
    EXPECT_TRUE(file.is_open());
    string line, exp_line;
    int i = 0;
    bool found = true;

46
47
    using namespace std;

48
49
    while (getline(file, line) && (i != exp_strings_.size())) {
        exp_line = exp_strings_[i];
50
51
52
53
        if (verbose_) {
            cout << "Read line  :" << line << endl;
            cout << "Looking for:" << exp_line << endl;
        }
54
55
        i++;
        if (string::npos == line.find(exp_line)) {
56
57
58
            if (verbose_) {
                cout << "Verdict    : not found" << endl;
            }
59
60
61
62
63
64
            found = false;
        }
    }

    file.close();

65
66
67
68
    if ((i != exp_strings_.size()) || (found == false)) {
        if (verbose_) {
            cout << "Final verdict: false" << endl;
        }
69
        return (false);
70
    }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    return (true);
}

void LogContentTest::remFile() {
    static_cast<void>(remove(LOG_FILE));
}

void LogContentTest::addString(const string& new_string) {
    exp_strings_.push_back(new_string);
}

// Set up the name of the LOG_FILE for use in checking
// the debug statements
const char *LogContentTest::LOG_FILE = "test.log";

}; // end of isc::dhcp::test namespace
}; // end of isc::dhcp namespace
}; // end of isc namespace