blob: 1f1031c4bbe0369e39310c47e0212d2b286d2acf [file] [log] [blame]
Adriana Kobylak91da4532016-07-20 12:42:55 -05001/**
2 * Copyright © 2016 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <tuple>
20#include <systemd/sd-journal.h>
21
22namespace phosphor
23{
24
25namespace logging
26{
27
28/** @enum level
29 * @brief Enum for priority level
30 */
31enum class level
32{
33 EMERG = LOG_EMERG,
34 ALERT = LOG_ALERT,
35 CRIT = LOG_CRIT,
36 ERR = LOG_ERR,
37 WARNING = LOG_WARNING,
38 NOTICE = LOG_NOTICE,
39 INFO = LOG_INFO,
40 DEBUG = LOG_DEBUG,
41};
42
43/** @fn log()
44 * @brief Log message to systemd journal
45 * @tparam L - Priority level
46 * @param[in] msg - Message to be logged in C-string format
47 * @param[in] entry - Metadata fields to be added to the message
48 * @details Usage: log<level::XX>(const char*, entry(*format), entry()...);
49 * @example log<level::DEBUG>(
50 * "Simple Example");
51 * char msg_str[] = "File not found";
52 * log<level::DEBUG>(
53 * msg_str,
54 * entry("MY_METADATA=%s_%x, name, number));
55 */
56template <level L, typename Msg, typename ...Entry>
57void log(Msg msg, Entry... entry);
58
59/** @fn entry()
60 * @brief Pack each format string entry as a tuple to be able to validate
61 * the string and parameters when multiple entries are passed to be logged.
62 * @tparam Arg - Types of first argument
63 * @tparam Args - Types of remaining arguments
64 * @param[in] arg - First metadata string of form VAR=value where
65 * VAR is the variable name in uppercase and
66 * value is of any size and format
67 * @param[in] args - Remaining metadata strings
68 */
69template <typename Arg, typename ...Args>
70constexpr auto entry(Arg&& arg, Args&&... args);
71
72namespace details
73{
74
75/** @fn prio()
76 * @brief Prepend PRIORITY= to the input priority string.
77 * This is required by sd_journal_send().
78 * @tparam L - Priority level
79 */
80template <level L>
81constexpr auto prio()
82{
83 constexpr const char *prio_str = "PRIORITY=%d";
84 constexpr const auto prio_tuple = std::make_tuple(prio_str, L);
85 return prio_tuple;
86}
87
88/** @fn helper_log()
89 * @brief Helper function for details::log(). Log request to journal.
90 * @tparam T - Type of tuple
91 * @tparam I - std::integer_sequence of indexes (0..N) for each tuple element
92 * @param[in] e - Tuple containing the data to be logged
93 * @param[unnamed] - std::integer_sequence of tuple's index values
94 */
95template <typename T, size_t ...I>
96void helper_log(T&& e, std::integer_sequence<size_t, I...>)
97{
Andrew Geissler328889d2016-10-10 12:43:48 -050098 // https://www.freedesktop.org/software/systemd/man/sd_journal_print.html
Adriana Kobylak91da4532016-07-20 12:42:55 -050099 sd_journal_send(std::get<I>(std::forward<T>(e))..., NULL);
100}
101
102/** @fn details::log()
103 * @brief Implementation of logging::log() function.
104 * Send request msg and size to helper funct to log it to the journal.
105 * @tparam T - Type of tuple
106 * @param[in] e - Tuple containing the data to be logged
107 */
108template <typename T>
109void log(T&& e)
110{
111 constexpr auto e_size = std::tuple_size<std::decay_t<T>>::value;
112 helper_log(std::forward<T>(e), std::make_index_sequence<e_size>{});
113}
114
115} // namespace details
116
117template <level L, typename Msg, typename ...Entry>
118void log(Msg msg, Entry... entry)
119{
120 static_assert((std::is_same<const char*, std::decay_t<Msg>>::value ||
121 std::is_same<char*, std::decay_t<Msg>>::value),
122 "First parameter must be a C-string.");
123
124 constexpr const char *msg_str = "MESSAGE=%s";
125 const auto msg_tuple = std::make_tuple(msg_str, std::forward<Msg>(msg));
126
127 auto log_tuple = std::tuple_cat(details::prio<L>(),
128 msg_tuple,
129 std::forward<Entry>(entry)...);
130 details::log(log_tuple);
131}
132
133template <typename Arg, typename ...Args>
134constexpr auto entry(Arg&& arg, Args&&... args)
135{
136 const auto entry_tuple = std::make_tuple(std::forward<Arg>(arg),
137 std::forward<Args>(args)...);
138 return entry_tuple;
139}
140
141} // namespace logging
142
143} // namespace phosphor
144