aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/vespa/log/bufferedlogger.cpp
blob: e28e0ae022bb1a1f590f9619623a0586bbab148c (plain) (blame)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "bufferedlogger.h"
#include "internal.h"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>

#include <iomanip>
#include <sstream>
#include <vector>
#include <cstdarg>
#include <mutex>

using namespace std::literals::chrono_literals;

namespace ns_log {

// implementation details for BufferedLogger
class BackingBuffer {
    BackingBuffer(const BackingBuffer & rhs);
    BackingBuffer & operator = (const BackingBuffer & rhs);
public:
    std::unique_ptr<Timer> _timer;
    /** Lock needed to access cache. */
    mutable std::mutex _mutex;

    static duration _countFactor;

    /** Struct keeping information about log message. */
    struct Entry {
        Logger::LogLevel _level;
        std::string _file;
        int _line;
        std::string _token;
        std::string _message;
        uint32_t _count;
        system_time _timestamp;
        Logger* _logger;

        Entry(const Entry &);
        Entry & operator=(const Entry &);
        Entry(Entry &&) noexcept;
        Entry & operator=(Entry &&) noexcept;
        Entry(Logger::LogLevel level, const char* file, int line,
              const std::string& token, const std::string& message,
              system_time timestamp, Logger&);
        ~Entry();

        bool operator==(const Entry& entry) const;
        bool operator<(const Entry& entry) const;

        system_time getAgeFactor() const;

        std::string toString() const;
    };

    typedef boost::multi_index_container<
        Entry,
        boost::multi_index::indexed_by<
            boost::multi_index::sequenced<>, // Timestamp sorted
            boost::multi_index::ordered_unique<
                boost::multi_index::identity<Entry>
            >
        >
    > LogCacheFront;
    typedef boost::multi_index_container<
        Entry,
        boost::multi_index::indexed_by<
            boost::multi_index::sequenced<>, // Timestamp sorted
            boost::multi_index::ordered_unique<
                boost::multi_index::identity<Entry>
            >,
            boost::multi_index::ordered_non_unique<
                boost::multi_index::const_mem_fun<
                    Entry, system_time, &Entry::getAgeFactor
                >
            >
        >
    > LogCacheBack;

    /** Entry container indexes on insert order and token. */
    LogCacheFront _cacheFront;
    /** Entry container indexed on insert order, token and age function. */
    LogCacheBack _cacheBack;

    uint32_t _maxCacheSize;
    duration _maxEntryAge;

    /** Empty buffer and write all log entries in it. */
    void flush();

    /** Gives all current content of log buffer. Useful for debugging. */
    std::string toString() const;

    /**
     * Flush parts of cache, so we're below max size and only have messages of
     * acceptable age. Calling this, _mutex should already be locked.
     */
    void trimCache(system_time currentTime);

    /**
     * Trim the cache up to current time. Used externally to check if we
     * need to empty buffer before new log messages arive.
     */
    void trimCache() {
        std::lock_guard<std::mutex> guard(_mutex);
        trimCache(_timer->getTimestamp());
    }

    /**
     * Log a given entry to underlying logger. Used when removing from cache.
     * Calling this, _mutex should already be locked.
     */
    void log(const Entry& e) const;

    BackingBuffer();
    ~BackingBuffer();

    void logImpl(Logger& l, Logger::LogLevel level,
                 const char *file, int line,
                 const std::string& token,
                 const std::string& message);

};

// Let each hit count for 5 seconds
duration BackingBuffer::_countFactor = VESPA_LOG_COUNTAGEFACTOR * 1s;

BackingBuffer::Entry::Entry(Logger::LogLevel level, const char* file, int line,
                             const std::string& token, const std::string& msg,
                             system_time timestamp, Logger& l)
    : _level(level),
      _file(file),
      _line(line),
      _token(token),
      _message(msg),
      _count(1),
      _timestamp(timestamp),
      _logger(&l)
{
}

BackingBuffer::Entry::Entry(const Entry &) = default;
BackingBuffer::Entry & BackingBuffer::Entry::operator =(const Entry &) = default;
BackingBuffer::Entry::Entry(Entry &&) noexcept = default;
BackingBuffer::Entry & BackingBuffer::Entry::operator=(Entry &&) noexcept = default;
BackingBuffer::Entry::~Entry() = default;

bool
BackingBuffer::Entry::operator==(const Entry& entry) const
{
    return (_token == entry._token);
}

bool
BackingBuffer::Entry::operator<(const Entry& entry) const
{
        // Don't let tokens from different loggers match each other
    if (_logger != entry._logger) {
        return _logger < entry._logger;
    }
        // If in the same logger, you should have full control. Overlapping
        // tokens if you want is a feature.
    return (_token < entry._token);
}

std::string
BackingBuffer::Entry::toString() const
{
    std::ostringstream ost;
    ost << "Entry(" << _level << ", " << _file << ":" << _line << ": "
        << _message << " [" << _token << "], count " << _count
        << ", timestamp " << count_us(_timestamp.time_since_epoch()) << ")";
    return ost.str();
}

system_time
BackingBuffer::Entry::getAgeFactor() const
{
    return _timestamp + _countFactor * _count;
}

BackingBuffer::BackingBuffer()
    : _timer(new Timer),
      _mutex(),
      _cacheFront(),
      _cacheBack(),
      _maxCacheSize(VESPA_LOG_LOGBUFFERSIZE),
      _maxEntryAge(VESPA_LOG_LOGENTRYMAXAGE * 1000 * 1000)
{
}

BackingBuffer::~BackingBuffer() = default;

BufferedLogger::BufferedLogger()
{
    _backing = new BackingBuffer();
}

BufferedLogger::~BufferedLogger()
{
    delete _backing; _backing = NULL;
}

namespace {

typedef boost::multi_index::nth_index<
        BackingBuffer::LogCacheFront, 0>::type LogCacheFrontTimestamp;
typedef boost::multi_index::nth_index<
        BackingBuffer::LogCacheFront, 1>::type LogCacheFrontToken;
typedef boost::multi_index::nth_index<
        BackingBuffer::LogCacheBack, 0>::type LogCacheBackTimestamp;
typedef boost::multi_index::nth_index<
        BackingBuffer::LogCacheBack, 1>::type LogCacheBackToken;
typedef boost::multi_index::nth_index<
        BackingBuffer::LogCacheBack, 2>::type LogCacheBackAge;

struct TimeStampWrapper : public Timer {
    TimeStampWrapper(system_time timeStamp) : _timeStamp(timeStamp) {}
    system_time getTimestamp() const noexcept override { return _timeStamp; }

    system_time _timeStamp;
};

}

void
BufferedLogger::doLog(Logger& l, Logger::LogLevel level,
                      const char *file, int line,
                      const std::string& mytoken, const char *fmt, ...)
{
    std::string token(mytoken);
    va_list args;
    va_start(args, fmt);

    const size_t sizeofPayload(4000);
    std::vector<char> buffer(sizeofPayload);
    vsnprintf(&buffer[0], buffer.capacity(), fmt, args);
    std::string message(&buffer[0]);
    // Empty token means to use message itself as token
    if (token.empty()) token = message;

    _backing->logImpl(l, level, file, line, token, message);
}

void
BackingBuffer::logImpl(Logger& l, Logger::LogLevel level,
                       const char *file, int line,
                       const std::string& token,
                       const std::string& message)
{
    Entry entry(level, file, line, token, message, _timer->getTimestamp(), l);

    std::lock_guard<std::mutex> guard(_mutex);
    LogCacheFrontToken::iterator it1 = _cacheFront.get<1>().find(entry);
    LogCacheBackToken::iterator it2 = _cacheBack.get<1>().find(entry);
    if (it1 != _cacheFront.get<1>().end()) {
        Entry copy(*it1);
        ++copy._count;
        _cacheFront.get<1>().replace(it1, copy);
    } else if (it2 != _cacheBack.get<1>().end()) {
        Entry copy(*it2);
        ++copy._count;
        _cacheBack.get<1>().replace(it2, copy);
    } else {
            // If entry didn't already exist, add it to the cache and log it
        l.doLogCore(TimeStampWrapper(entry._timestamp), level, file, line, message.c_str(), message.size());
        _cacheFront.push_back(entry);
    }
    trimCache(entry._timestamp);
}

void
BackingBuffer::flush()
{
    std::lock_guard<std::mutex> guard(_mutex);
    for (const auto & entry : _cacheBack) {
        log(entry);
    }
    _cacheBack.clear();
    for (const auto & entry : _cacheFront) {
        log(entry);
    }
    _cacheFront.clear();
}

void
BufferedLogger::flush() {
    _backing->flush();
}

void
BackingBuffer::trimCache(system_time currentTime)
{
        // Remove entries that have been in here too long.
    while (!_cacheBack.empty() &&
           _cacheBack.front()._timestamp + _maxEntryAge < currentTime)
    {
        log(_cacheBack.front());
        _cacheBack.pop_front();
    }
    while (!_cacheFront.empty() &&
           _cacheFront.front()._timestamp + _maxEntryAge < currentTime)
    {
        log(_cacheFront.front());
        _cacheFront.pop_front();
    }
        // If cache front is larger than half max size, move to back.
    for (uint32_t i = _cacheFront.size(); i > _maxCacheSize / 2; --i) {
        Entry e(_cacheFront.front());
        _cacheFront.pop_front();
        _cacheBack.push_back(e);
    }
        // Remove entries from back based on count modified age.
    for (uint32_t i = _cacheFront.size() + _cacheBack.size(); i > _maxCacheSize; --i) {
        log(*_cacheBack.get<2>().begin());
        _cacheBack.get<2>().erase(_cacheBack.get<2>().begin());
    }
}

void
BufferedLogger::trimCache()
{
    _backing->trimCache();
}

void
BackingBuffer::log(const Entry& e) const
{
    if (e._count > 1) {
        std::ostringstream ost;
        ost << e._message << " (Repeated " << (e._count - 1)
            << " times since " << count_s(e._timestamp.time_since_epoch()) << "."
            << std::setw(6) << std::setfill('0') << (count_us(e._timestamp.time_since_epoch()) % 1000000)
            << ")";
        e._logger->doLogCore(*_timer, e._level, e._file.c_str(),
                             e._line, ost.str().c_str(), ost.str().size());
    }
}

std::string
BackingBuffer::toString() const
{
    std::ostringstream ost;
    ost << "Front log cache content:\n";
    std::lock_guard<std::mutex> guard(_mutex);
    for (const auto & entry : _cacheFront) {
        ost << "  " << entry.toString() << "\n";
    }
    ost << "Back log cache content:\n";
    for (const auto & entry : _cacheBack) {
        ost << "  " << entry.toString() << "\n";
    }
    return ost.str();
}


void
BufferedLogger::setMaxCacheSize(uint32_t size) {
    _backing->_maxCacheSize = size;
}

void
BufferedLogger::setMaxEntryAge(uint64_t seconds) {
    _backing->_maxEntryAge = std::chrono::seconds(seconds);
}

void
BufferedLogger::setCountFactor(uint64_t seconds) {
    _backing->_countFactor = std::chrono::seconds(seconds);
}

/** Set a fake timer to use for log messages. Used in unit testing. */
void
BufferedLogger::setTimer(std::unique_ptr<Timer> timer)
{
    _backing->_timer = std::move(timer);
}

BufferedLogger&
BufferedLogger::instance()
{
    static BufferedLogger logger;
    return logger;
}

} // ns_log