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
// Copyright (C) 2012-2026 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 <config.h>

#include <log/macros.h>
#include <log/logger_support.h>
#include <log/logger_manager.h>
#include <log/log_messages.h>
#include <log/interprocess/interprocess_sync.h>
#include <log/tests/log_test_messages.h>
#include <testutils/multi_threading_utils.h>

#include <mutex>
#include <iostream>

#include <boost/noncopyable.hpp>

using namespace std;
using namespace isc::log;
using namespace isc::test;

/// \brief RAII safe mutex checker.
class CheckMutex : boost::noncopyable {
public:
    /// \brief Exception thrown when the mutex is already locked.
    struct AlreadyLocked : public isc::InvalidParameter {
        AlreadyLocked(const char* file, size_t line, const char* what) :
            isc::InvalidParameter(file, line, what)
        {}
    };

    /// \brief Constructor.
    ///
    /// \throw AlreadyLocked if the mutex is already locked.
    CheckMutex(mutex& mutex) : mutex_(mutex) {
        if (!checkTryLock(mutex_)) {
            isc_throw(AlreadyLocked, "The mutex is already locked");
        }
    }

    /// \brief Destructor.
    ~CheckMutex() = default;

private:
    mutex& mutex_;
};

class MockLoggingSync : public isc::log::interprocess::InterprocessSync {
public:
    /// \brief Constructor
    MockLoggingSync(const std::string& component_name) :
        InterprocessSync(component_name)
    {}

protected:
    virtual bool lock() {<--- Function in derived class
        // We first check if the logger acquired a lock on the
        // LoggerManager mutex.
        try {
            CheckMutex check(LoggerManager::getMutex());
        } catch (const CheckMutex::AlreadyLocked&) {
            cout << "FIELD1 FIELD2 LOGGER_LOCK_TEST: MUTEXLOCK\n";
        }

        cout << "FIELD1 FIELD2 LOGGER_LOCK_TEST: LOCK\n";
        return (true);
    }

    virtual bool tryLock() {<--- Function in derived class
        cout << "FIELD1 FIELD2 LOGGER_LOCK_TEST: TRYLOCK\n";
        return (true);
    }

    virtual bool unlock() {<--- Function in derived class
        cout << "FIELD1 FIELD2 LOGGER_LOCK_TEST: UNLOCK\n";
        return (true);
    }
};

/// \brief Test logger lock sequence
///
/// A program used in testing the logger. It verifies that (1) an
/// interprocess sync lock is first acquired by the logger, (2) the
/// message is logged by the logger, and (3) the lock is released in
/// that sequence.
int
main(int, char**) {
    initLogger();
    Logger logger("log");
    logger.setInterprocessSync(new MockLoggingSync("log"));

    LOG_INFO(logger, LOG_LOCK_TEST_MESSAGE);

    return (0);
}