summaryrefslogtreecommitdiff
path: root/indra/llcommon/tests/wrapllerrs.h
blob: 9cd2c69b1157e0b1435548a95fbec7401c0d8c7e (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
 * @file   wrapllerrs.h
 * @author Nat Goodspeed
 * @date   2009-03-11
 * @brief  Define a class useful for unit tests that engage llerrs (LL_ERRS) functionality
 *
 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#if ! defined(LL_WRAPLLERRS_H)
#define LL_WRAPLLERRS_H

#if LL_WINDOWS
#pragma warning (disable : 4355) // 'this' used in initializer list: yes, intentionally
#endif

#include <tut/tut.hpp>
#include "llerrorcontrol.h"
#include "llexception.h"
#include "stringize.h"
#include "../test/catch_and_store_what_in.h"
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <list>
#include <string>

struct WrapLLErrs
{
    WrapLLErrs():
        // Resetting Settings discards the default Recorder that writes to
        // stderr. Otherwise, expected llerrs (LL_ERRS) messages clutter the
        // console output of successful tests, potentially confusing things.
        mPriorErrorSettings(LLError::saveAndResetSettings()),
        // Save shutdown function called by LL_ERRS
        mPriorFatal(LLError::getFatalFunction())
    {
        // Make LL_ERRS call our own operator() method
        LLError::setFatalFunction(
            [this](const std::string& message){ (*this)(message); });
    }

    ~WrapLLErrs()
    {
        LLError::setFatalFunction(mPriorFatal);
        LLError::restoreSettings(mPriorErrorSettings);
    }

    struct FatalException: public LLException
    {
        FatalException(const std::string& what): LLException(what) {}
    };

    void operator()(const std::string& message)
    {
        // Save message for later in case consumer wants to sense the result directly
        error = message;
        // Also throw an appropriate exception since calling code is likely to
        // assume that control won't continue beyond LL_ERRS.
        LLTHROW(FatalException(message));
    }

    /// Convenience wrapper for catch_what<FatalException>()
    //
    // The implementation makes it clear that this function need not be a
    // member; it could easily be a free function. It is a member because it
    // makes no sense to attempt to catch FatalException unless there is a
    // WrapLLErrs instance in scope. Without a live WrapLLErrs instance, any
    // LL_ERRS() reached by code within 'func' would terminate the test
    // program instead of throwing FatalException.
    //
    // We were tempted to introduce a free function, likewise accepting
    // arbitrary 'func', that would instantiate WrapLLErrs and then call
    // catch_llerrs() on that instance. We decided against it, for this
    // reason: on extending a test function containing a single call to that
    // free function, a maintainer would most likely make additional calls to
    // that free function, instead of switching to an explicit WrapLLErrs
    // declaration with several calls to its catch_llerrs() member function.
    // Even a construct such as WrapLLErrs().catch_llerrs(...) would make the
    // object declaration more visible; it's not unreasonable to expect a
    // maintainer to extend that by naming and reusing the WrapLLErrs instance.
    template <typename FUNC>
    std::string catch_llerrs(FUNC func)
    {
        return catch_what<FatalException>(func);
    }

    std::string error;
    LLError::SettingsStoragePtr mPriorErrorSettings;
    LLError::FatalFunction mPriorFatal;
};

/**
 * Capture log messages. This is adapted (simplified) from the one in
 * llerror_test.cpp.
 */
class CaptureLogRecorder : public LLError::Recorder, public boost::noncopyable
{
public:
    CaptureLogRecorder()
        : LLError::Recorder(),
        boost::noncopyable(),
        mMessages()
    {
    }

    virtual ~CaptureLogRecorder()
    {
    }

    virtual void recordMessage(LLError::ELevel level, const std::string& message)
    {
        mMessages.push_back(message);
    }

    friend inline
    std::ostream& operator<<(std::ostream& out, const CaptureLogRecorder& log)
    {
        return log.streamto(out);
    }

    /// Don't assume the message we want is necessarily the LAST log message
    /// emitted by the underlying code; search backwards through all messages
    /// for the sought string.
    std::string messageWith(const std::string& search, bool required)
    {
        for (MessageList::const_reverse_iterator rmi(mMessages.rbegin()), rmend(mMessages.rend());
             rmi != rmend; ++rmi)
        {
            if (rmi->find(search) != std::string::npos)
                return *rmi;
        }
        // failed to find any such message
        if (! required)
            return std::string();

        throw tut::failure(STRINGIZE("failed to find '" << search
                                     << "' in captured log messages:\n"
                                     << *this));
    }

    std::ostream& streamto(std::ostream& out) const
    {
        MessageList::const_iterator mi(mMessages.begin()), mend(mMessages.end());
        if (mi != mend)
        {
            // handle first message separately: it doesn't get a newline
            out << *mi++;
            for ( ; mi != mend; ++mi)
            {
                // every subsequent message gets a newline
                out << '\n' << *mi;
            }
        }
        return out;
    }

private:
    typedef std::list<std::string> MessageList;
    MessageList mMessages;
};

/**
 * Capture log messages. This is adapted (simplified) from the one in
 * llerror_test.cpp.
 */
class CaptureLog : public boost::noncopyable
{
public:
    CaptureLog(LLError::ELevel level=LLError::LEVEL_DEBUG)
        // Mostly what we're trying to accomplish by saving and resetting
        // LLError::Settings is to bypass the default RecordToStderr and
        // RecordToWinDebug Recorders. As these are visible only inside
        // llerror.cpp, we can't just call LLError::removeRecorder() with
        // each. For certain tests we need to produce, capture and examine
        // DEBUG log messages -- but we don't want to spam the user's console
        // with that output. If it turns out that saveAndResetSettings() has
        // some bad effect, give up and just let the DEBUG level log messages
        // display.
        : boost::noncopyable(),
        mFatalFunction(LLError::getFatalFunction()),
        mOldSettings(LLError::saveAndResetSettings()),
        mRecorder(new CaptureLogRecorder())
    {
        // reinstate the FatalFunction we just reset
        LLError::setFatalFunction(mFatalFunction);
        LLError::setDefaultLevel(level);
        LLError::addRecorder(mRecorder);
    }

    ~CaptureLog()
    {
        LLError::removeRecorder(mRecorder);
        LLError::restoreSettings(mOldSettings);
    }

    /// Don't assume the message we want is necessarily the LAST log message
    /// emitted by the underlying code; search backwards through all messages
    /// for the sought string.
    std::string messageWith(const std::string& search, bool required=true)
    {
        return std::dynamic_pointer_cast<CaptureLogRecorder>(mRecorder)->messageWith(search, required);
    }

    std::ostream& streamto(std::ostream& out) const
    {
        return std::dynamic_pointer_cast<CaptureLogRecorder>(mRecorder)->streamto(out);
    }

    friend inline std::ostream& operator<<(std::ostream& out, const CaptureLog& self)
    {
        return self.streamto(out);
    }

private:
    LLError::FatalFunction mFatalFunction;
    LLError::SettingsStoragePtr mOldSettings;
    LLError::RecorderPtr mRecorder;
};

#endif /* ! defined(LL_WRAPLLERRS_H) */