summaryrefslogtreecommitdiffstats
path: root/phosphor-logging/sdjournal.hpp
blob: 6aa2394098826c3a3e895e42c8e104ea276e2d12 (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
#pragma once

#include <systemd/sd-journal.h>

#include <cstdarg>

namespace phosphor
{
namespace logging
{

/**
 * Implementation that calls into real sd_journal methods.
 */
class SdJournalHandler
{
  public:
    SdJournalHandler() = default;
    virtual ~SdJournalHandler() = default;
    SdJournalHandler(const SdJournalHandler&) = default;
    SdJournalHandler& operator=(const SdJournalHandler&) = default;
    SdJournalHandler(SdJournalHandler&&) = default;
    SdJournalHandler& operator=(SdJournalHandler&&) = default;

    /**
     * Provide a fake method that's called by the real method so we can catch
     * the journal_send call in testing.
     *
     * @param[in] fmt - the format string passed into journal_send.
     * @return an int meant to be intepreted by the journal_send caller during
     * testing.
     */
    virtual int journal_send_call(const char* fmt)
    {
        return 0;
    };

    /**
     * Send the information to sd_journal_send.
     *
     * @param[in] fmt - c string format.
     * @param[in] ... - parameters.
     * @return value from sd_journal_send
     *
     * sentinel default makes sure the last parameter is null.
     */
    virtual int journal_send(const char* fmt, ...)
        __attribute__((format(printf, 2, 0))) __attribute__((sentinel))
    {
        va_list args;
        va_start(args, fmt);

        int rc = ::sd_journal_send(fmt, args, NULL);
        va_end(args);

        return rc;
    }
};

extern SdJournalHandler* sdjournal_ptr;

/**
 * Swap out the sdjournal_ptr used by log<> such that a test
 * won't need to hit the real sd_journal and fail.
 *
 * @param[in] with - pointer to your sdjournal_mock object.
 * @return pointer to the previously stored sdjournal_ptr.
 */
SdJournalHandler* SwapJouralImpl(SdJournalHandler* with);

} // namespace logging
} // namespace phosphor
OpenPOWER on IntegriCloud