blob: 78820fba442862a706c78caf824ad935736b3a7e [file] [log] [blame]
Patrick Venture59a6b1f2018-08-29 11:41:01 -07001#pragma once
2
3#include <systemd/sd-journal.h>
4
5#include <cstdarg>
6
7namespace phosphor
8{
9namespace logging
10{
11
12/**
13 * Implementation that calls into real sd_journal methods.
14 */
15class SdJournalHandler
16{
17 public:
18 SdJournalHandler() = default;
19 virtual ~SdJournalHandler() = default;
20 SdJournalHandler(const SdJournalHandler&) = default;
21 SdJournalHandler& operator=(const SdJournalHandler&) = default;
22 SdJournalHandler(SdJournalHandler&&) = default;
23 SdJournalHandler& operator=(SdJournalHandler&&) = default;
24
25 /**
26 * Provide a fake method that's called by the real method so we can catch
27 * the journal_send call in testing.
28 *
29 * @param[in] fmt - the format string passed into journal_send.
30 * @return an int meant to be intepreted by the journal_send caller during
31 * testing.
32 */
33 virtual int journal_send_call(const char* fmt)
34 {
35 return 0;
36 };
37
38 /**
39 * Send the information to sd_journal_send.
40 *
41 * @param[in] fmt - c string format.
42 * @param[in] ... - parameters.
43 * @return value from sd_journal_send
44 *
45 * sentinel default makes sure the last parameter is null.
46 */
47 virtual int journal_send(const char* fmt, ...)
48 __attribute__((format(printf, 2, 0))) __attribute__((sentinel))
49 {
50 va_list args;
51 va_start(args, fmt);
52
53 int rc = ::sd_journal_send(fmt, args, NULL);
54 va_end(args);
55
56 return rc;
57 }
58};
59
60extern SdJournalHandler* sdjournal_ptr;
61
62/**
63 * Swap out the sdjournal_ptr used by log<> such that a test
64 * won't need to hit the real sd_journal and fail.
65 *
66 * @param[in] with - pointer to your sdjournal_mock object.
67 * @return pointer to the previously stored sdjournal_ptr.
68 */
Patrick Venture7bb7a772018-12-04 14:43:21 -080069SdJournalHandler* SwapJouralHandler(SdJournalHandler* with);
Patrick Venture59a6b1f2018-08-29 11:41:01 -070070
71} // namespace logging
72} // namespace phosphor