blob: cba6d37928444b94d7445ce452f27f9e9eb79fbf [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{
98 sd_journal_send(std::get<I>(std::forward<T>(e))..., NULL);
99}
100
101/** @fn details::log()
102 * @brief Implementation of logging::log() function.
103 * Send request msg and size to helper funct to log it to the journal.
104 * @tparam T - Type of tuple
105 * @param[in] e - Tuple containing the data to be logged
106 */
107template <typename T>
108void log(T&& e)
109{
110 constexpr auto e_size = std::tuple_size<std::decay_t<T>>::value;
111 helper_log(std::forward<T>(e), std::make_index_sequence<e_size>{});
112}
113
114} // namespace details
115
116template <level L, typename Msg, typename ...Entry>
117void log(Msg msg, Entry... entry)
118{
119 static_assert((std::is_same<const char*, std::decay_t<Msg>>::value ||
120 std::is_same<char*, std::decay_t<Msg>>::value),
121 "First parameter must be a C-string.");
122
123 constexpr const char *msg_str = "MESSAGE=%s";
124 const auto msg_tuple = std::make_tuple(msg_str, std::forward<Msg>(msg));
125
126 auto log_tuple = std::tuple_cat(details::prio<L>(),
127 msg_tuple,
128 std::forward<Entry>(entry)...);
129 details::log(log_tuple);
130}
131
132template <typename Arg, typename ...Args>
133constexpr auto entry(Arg&& arg, Args&&... args)
134{
135 const auto entry_tuple = std::make_tuple(std::forward<Arg>(arg),
136 std::forward<Args>(args)...);
137 return entry_tuple;
138}
139
140} // namespace logging
141
142} // namespace phosphor
143