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