summaryrefslogtreecommitdiffstats
path: root/src/lib/testutils/log_utils.h
blob: 7191e0eedeebf9f0bd8e064f229c5ea2321505f3 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2016-2022 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/.

#ifndef TEST_LOG_UTILS_H
#define TEST_LOG_UTILS_H

#include <string>
#include <fstream>

//#include <config.h>

#include <log/logger_manager.h>
#include <log/logger_name.h>
#include <log/logger_support.h>

//#include <boost/shared_ptr.hpp>
//#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>

using namespace std;
using namespace isc::log;

namespace isc {
namespace dhcp {
namespace test {

/// @brief Test class for testing things that emit log entries
///
/// This class provides a convenient method for testing out
/// things that emit log entries.  The class sets up the logging
/// to log everything into a file and provides a routine to
/// check the expected strings vs the actual log entries.
/// The user needs to call the addString function for each of
/// the strings they expect in the output in the order they
/// will be emitted.

class LogContentTest : public ::testing::Test {
public:

    /// @brief Initializes the logger setup for using
    /// in checking log statements
    ///
    /// @todo add support to adjust the severity and debug level
    /// to allow for better control over the statements that
    /// get logged.
    LogContentTest();

    virtual ~LogContentTest();

    /// @brief check that the requested strings are in the
    /// test log file in the requested order.
    ///
    /// This routine expects that the caller has properly
    /// set up the vector of expected strings by calling
    /// addString() with the necessary strings.
    ///
    /// @return true if all of the strings match
    bool checkFile();

    /// @brief check that the requested string is in the
    /// test log file.
    ///
    /// @param exp_string the string to be searched
    /// @return count of matching lines
    size_t countFile(const string& exp_string);

    /// @brief remove the test log file
    void remFile();

    /// @brief Enables or disables verbose mode.
    ///
    /// See @ref verbose_ for details.
    ///
    /// @param talk_a_lot (true - as the name says, false - shut up)
    void logCheckVerbose(bool talk_a_lot) {
        verbose_ = talk_a_lot;
    }

    /// @brief Add a string to the vector of expected strings
    ///
    /// @param new_string the string to add to the end of the vector
    void addString(const string& new_string);

    vector<string> exp_strings_;
    static const char* LOG_FILE;
    static const char* KEA_LOG_CHECK_VERBOSE;

    /// @brief controls whether the checkFile() should print more details.
    ///
    /// If set to true, checkFile() will print each expected line, each
    /// logged line and will print out a failure message if those two do
    /// not match. Also, a final verdict is printed. Everything is printed
    /// on stdout.
    /// The default is false but can be overwritten by setting the
    /// KEA_LOG_CHECK_VERBOSE environment variable.
    bool verbose_;
};

} // end of isc::dhcp::test namespace
} // end of isc::dhcp namespace
} // end of isc namespace
#endif // TEST_LOG_UTILS_H