blob: b99c4d31f55d91908a54f939bdf9eaa3f9715ae6 [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
Patrick Venturef18bf832018-10-26 18:14:00 -070019#include <systemd/sd-journal.h>
20
Patrick Venture59a6b1f2018-08-29 11:41:01 -070021#include <phosphor-logging/sdjournal.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070022#include <sdbusplus/server/transaction.hpp>
Patrick Williams2544b412022-10-04 08:41:06 -050023
Adriana Kobylak91da4532016-07-20 12:42:55 -050024#include <tuple>
Joseph Reynolds8f6f7fc2018-05-04 10:11:00 -050025#include <type_traits>
Adriana Kobylak91da4532016-07-20 12:42:55 -050026
27namespace phosphor
28{
29
30namespace logging
31{
32
33/** @enum level
34 * @brief Enum for priority level
35 */
36enum class level
37{
Patrick Venturef18bf832018-10-26 18:14:00 -070038 EMERG = LOG_EMERG,
39 ALERT = LOG_ALERT,
40 CRIT = LOG_CRIT,
41 ERR = LOG_ERR,
Adriana Kobylak91da4532016-07-20 12:42:55 -050042 WARNING = LOG_WARNING,
Patrick Venturef18bf832018-10-26 18:14:00 -070043 NOTICE = LOG_NOTICE,
44 INFO = LOG_INFO,
45 DEBUG = LOG_DEBUG,
Adriana Kobylak91da4532016-07-20 12:42:55 -050046};
47
Adriana Kobylak91da4532016-07-20 12:42:55 -050048namespace details
49{
50
51/** @fn prio()
52 * @brief Prepend PRIORITY= to the input priority string.
53 * This is required by sd_journal_send().
54 * @tparam L - Priority level
55 */
56template <level L>
57constexpr auto prio()
58{
Patrick Venturef18bf832018-10-26 18:14:00 -070059 constexpr const char* prio_str = "PRIORITY=%d";
Adriana Kobylak91da4532016-07-20 12:42:55 -050060 constexpr const auto prio_tuple = std::make_tuple(prio_str, L);
61 return prio_tuple;
62}
63
64/** @fn helper_log()
65 * @brief Helper function for details::log(). Log request to journal.
66 * @tparam T - Type of tuple
67 * @tparam I - std::integer_sequence of indexes (0..N) for each tuple element
68 * @param[in] e - Tuple containing the data to be logged
69 * @param[unnamed] - std::integer_sequence of tuple's index values
70 */
Patrick Venturef18bf832018-10-26 18:14:00 -070071template <typename T, size_t... I>
Adriana Kobylak91da4532016-07-20 12:42:55 -050072void helper_log(T&& e, std::integer_sequence<size_t, I...>)
73{
Andrew Geissler328889d2016-10-10 12:43:48 -050074 // https://www.freedesktop.org/software/systemd/man/sd_journal_print.html
Patrick Venture3f9cb092019-01-03 14:31:09 -080075 // TODO: Re-enable call through interface for testing (or move the code
76 // into the body of the object).
77 sd_journal_send(std::get<I>(std::forward<T>(e))..., NULL);
Adriana Kobylak91da4532016-07-20 12:42:55 -050078}
79
80/** @fn details::log()
81 * @brief Implementation of logging::log() function.
82 * Send request msg and size to helper funct to log it to the journal.
83 * @tparam T - Type of tuple
84 * @param[in] e - Tuple containing the data to be logged
85 */
86template <typename T>
87void log(T&& e)
88{
89 constexpr auto e_size = std::tuple_size<std::decay_t<T>>::value;
90 helper_log(std::forward<T>(e), std::make_index_sequence<e_size>{});
91}
92
93} // namespace details
94
William A. Kennington III3b818462019-07-12 16:04:59 -070095template <class T>
Patrick Williams2544b412022-10-04 08:41:06 -050096struct is_char_ptr_argtype :
97 std::integral_constant<
98 bool,
99 (std::is_pointer<typename std::decay<T>::type>::value &&
100 std::is_same<typename std::remove_cv<typename std::remove_pointer<
101 typename std::decay<T>::type>::type>::type,
102 char>::value)>
103{};
William A. Kennington III3b818462019-07-12 16:04:59 -0700104
105template <class T>
Patrick Williams2544b412022-10-04 08:41:06 -0500106struct is_printf_argtype :
107 std::integral_constant<
108 bool,
109 (std::is_integral<typename std::remove_reference<T>::type>::value ||
110 std::is_enum<typename std::remove_reference<T>::type>::value ||
111 std::is_floating_point<
112 typename std::remove_reference<T>::type>::value ||
113 std::is_pointer<typename std::decay<T>::type>::value)>
114{};
William A. Kennington III3b818462019-07-12 16:04:59 -0700115
116template <bool...>
117struct bool_pack;
118
119template <bool... bs>
120using all_true = std::is_same<bool_pack<bs..., true>, bool_pack<true, bs...>>;
121
122/** @fn entry()
123 * @brief Pack each format string entry as a tuple to be able to validate
124 * the string and parameters when multiple entries are passed to be logged.
125 * @tparam Arg - Types of first argument
126 * @tparam Args - Types of remaining arguments
127 * @param[in] arg - First metadata string of form VAR=value where
128 * VAR is the variable name in uppercase and
129 * value is of any size and format
130 * @param[in] args - Remaining metadata strings
131 */
132template <typename Arg, typename... Args>
133constexpr auto entry(Arg&& arg, Args&&... args)
134{
135 static_assert(is_char_ptr_argtype<Arg>::value,
136 "bad argument type: use char*");
137 static_assert(all_true<is_printf_argtype<Args>::value...>::value,
138 "bad argument type: use string.c_str() if needed");
139 return std::make_tuple(std::forward<Arg>(arg), std::forward<Args>(args)...);
140}
141
142/** @fn log()
143 * @brief Log message to systemd journal
144 * @tparam L - Priority level
145 * @param[in] msg - Message to be logged in C-string format
146 * @param[in] entry - Metadata fields to be added to the message
147 * @details Usage: log<level::XX>(const char*, entry(*format), entry()...);
148 * @example log<level::DEBUG>(
149 * "Simple Example");
150 * char msg_str[] = "File not found";
151 * log<level::DEBUG>(
152 * msg_str,
153 * entry("MY_METADATA=%s_%x, name, number));
154 */
Patrick Venturef18bf832018-10-26 18:14:00 -0700155template <level L, typename Msg, typename... Entry>
Adriana Kobylak7d0692a2016-12-06 15:13:23 -0600156void log(Msg msg, Entry... e)
Adriana Kobylak91da4532016-07-20 12:42:55 -0500157{
158 static_assert((std::is_same<const char*, std::decay_t<Msg>>::value ||
159 std::is_same<char*, std::decay_t<Msg>>::value),
160 "First parameter must be a C-string.");
161
Patrick Venturef18bf832018-10-26 18:14:00 -0700162 constexpr const char* msg_str = "MESSAGE=%s";
Adriana Kobylak91da4532016-07-20 12:42:55 -0500163 const auto msg_tuple = std::make_tuple(msg_str, std::forward<Msg>(msg));
164
Stanley Chu31d78f72022-02-08 13:45:32 +0800165 constexpr auto transactionStr = "TRANSACTION_ID=%llu";
Adriana Kobylak7d0692a2016-12-06 15:13:23 -0600166 auto transactionId = sdbusplus::server::transaction::get_id();
167
Patrick Venturef18bf832018-10-26 18:14:00 -0700168 auto log_tuple = std::tuple_cat(details::prio<L>(), msg_tuple,
Adriana Kobylak7d0692a2016-12-06 15:13:23 -0600169 entry(transactionStr, transactionId),
170 std::forward<Entry>(e)...);
Adriana Kobylak91da4532016-07-20 12:42:55 -0500171 details::log(log_tuple);
172}
173
Adriana Kobylak91da4532016-07-20 12:42:55 -0500174} // namespace logging
175
176} // namespace phosphor