xref: /freebsd/sys/dev/isci/scil/sci_base_logger.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23  * The full GNU General Public License is included in this distribution
24  * in the file called LICENSE.GPL.
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
35  *   * Redistributions of source code must retain the above copyright
36  *     notice, this list of conditions and the following disclaimer.
37  *   * Redistributions in binary form must reproduce the above copyright
38  *     notice, this list of conditions and the following disclaimer in
39  *     the documentation and/or other materials provided with the
40  *     distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  */
54 
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57 
58 /**
59  * @file
60  *
61  * @brief This file contains the implementation for the base logger object.
62  *        It provides the functionality necessary to enabling, disabling,
63  *        constructing, etc. logger objects.
64  */
65 
66 
67 #include <dev/isci/scil/sci_base_logger.h>
68 
69 
70 #ifdef SCI_LOGGING
71 
72 //******************************************************************************
73 //* P R I V A T E   M E T H O D S
74 //******************************************************************************
75 
76 #define SCI_LOGGER_GET_OBJECT_MASK(logger, verbosity_level) \
77    (((SCI_BASE_LOGGER_T *)(logger))->object_mask[(verbosity_level)])
78 
79 /**
80  * @brief This method is private to this object.  It attempts to enable the
81  *        supplied log objects for the supplied verbosity levels.
82  *
83  * @param[in]  logger This parameter specifies the logger object for which
84  *             to attempt to enable log object and verbosity levels.
85  * @param[in]  log_object_mask This parameter specifies the log objects to
86  *             attempt to enable.
87  * @param[in]  verbosity_mask This parameter specifies the verbosity levels
88  *             that are allowed to be enabled.
89  * @param[in]  verbosity This parameter specifies the specific verbosity level
90  *             to attempt to enable.
91  *
92  * @return none
93  */
94 static
95 void sci_base_logger_enable_log_object(
96    SCI_LOGGER_HANDLE_T  logger,
97    U32                  log_object_mask,
98    U8                   verbosity_mask,
99    U8                   verbosity
100 )
101 {
102    // Enable the log objects for the error verbosity if errs are enabled.
103    if ( (1<<verbosity) & verbosity_mask)
104    {
105       SCI_LOGGER_GET_OBJECT_MASK(logger, verbosity) |= log_object_mask;
106       (((SCI_BASE_LOGGER_T *)(logger))->verbosity_mask |= (1<<verbosity) );
107    }
108 }
109 
110 /**
111  * @brief This method is private to this object.  It attempts to disable the
112  *        supplied log objects for the supplied verbosity levels.
113  *
114  * @param[in]  logger This parameter specifies the logger object for which
115  *             to attempt to disable log object and verbosity levels.
116  * @param[in]  log_object_mask This parameter specifies the log objects to
117  *             attempt to disable.
118  * @param[in]  verbosity_mask This parameter specifies the verbosity levels
119  *             that are allowed to be disabled.
120  * @param[in]  verbosity This parameter specifies the specific verbosity level
121  *             to attempt to disable.
122  *
123  * @return none
124  */
125 static
126 void sci_base_logger_disable_log_object(
127    SCI_LOGGER_HANDLE_T  logger,
128    U32                  log_object_mask,
129    U8                   verbosity_mask,
130    U8                   verbosity
131 )
132 {
133    if ( (1<<verbosity) & verbosity_mask)
134    {
135       SCI_LOGGER_GET_OBJECT_MASK(logger, verbosity) &= ~log_object_mask;
136 
137       // If all of the objects in the object mask are disabled for this
138       // verbosity, then disable the verbosity as well.
139       if (SCI_LOGGER_GET_OBJECT_MASK(logger, verbosity) == 0)
140          (((SCI_BASE_LOGGER_T *)(logger))->verbosity_mask &= ~(1<<verbosity) );
141    }
142 }
143 
144 //******************************************************************************
145 //* P U B L I C   M E T H O D S
146 //******************************************************************************
147 
148 U8 sci_logger_get_verbosity_mask(
149    SCI_LOGGER_HANDLE_T  logger,
150    U32                  log_object
151 )
152 {
153    U8 verbosity_mask = 0;
154    SCI_BASE_LOGGER_T * base_logger = (SCI_BASE_LOGGER_T *)logger;
155 
156    if ( base_logger->object_mask[SCI_LOG_VERBOSITY_ERROR] & log_object )
157       verbosity_mask |= 1<<SCI_LOG_VERBOSITY_ERROR;
158 
159    if ( base_logger->object_mask[SCI_LOG_VERBOSITY_WARNING] & log_object )
160       verbosity_mask |= 1<<SCI_LOG_VERBOSITY_WARNING;
161 
162    if ( base_logger->object_mask[SCI_LOG_VERBOSITY_INFO] & log_object )
163       verbosity_mask |= 1<<SCI_LOG_VERBOSITY_INFO;
164 
165    if ( base_logger->object_mask[SCI_LOG_VERBOSITY_TRACE] & log_object )
166       verbosity_mask |= 1<<SCI_LOG_VERBOSITY_TRACE;
167 
168    if ( base_logger->object_mask[SCI_LOG_VERBOSITY_STATES] & log_object )
169       verbosity_mask |= 1<<SCI_LOG_VERBOSITY_TRACE;
170 
171    return verbosity_mask;
172 }
173 
174 // ---------------------------------------------------------------------------
175 
176 U32 sci_logger_get_object_mask(
177    SCI_LOGGER_HANDLE_T  logger,
178    U8                   verbosity
179 )
180 {
181    // Ensure that a supported verbosity level was supplied.
182    if (  (SCI_LOG_VERBOSITY_ERROR == verbosity)
183       || (SCI_LOG_VERBOSITY_WARNING == verbosity)
184       || (SCI_LOG_VERBOSITY_INFO == verbosity)
185       || (SCI_LOG_VERBOSITY_TRACE == verbosity)
186       || (SCI_LOG_VERBOSITY_STATES == verbosity) )
187    {
188       return SCI_LOGGER_GET_OBJECT_MASK(logger, verbosity);
189    }
190 
191    // An unsupported verbosity level was supplied.  Simply return an empty
192    // log object mask.
193    return 0;
194 }
195 
196 // ---------------------------------------------------------------------------
197 
198 void sci_logger_enable(
199    SCI_LOGGER_HANDLE_T  logger,
200    U32                  log_object_mask,
201    U8                   verbosity_mask
202 )
203 {
204    sci_base_logger_enable_log_object(
205       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_ERROR
206    );
207 
208    sci_base_logger_enable_log_object(
209       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_WARNING
210    );
211 
212    sci_base_logger_enable_log_object(
213       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_INFO
214    );
215 
216    sci_base_logger_enable_log_object(
217       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_TRACE
218    );
219 
220    sci_base_logger_enable_log_object(
221       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_STATES
222    );
223 }
224 
225 // ---------------------------------------------------------------------------
226 
227 void sci_logger_disable(
228    SCI_LOGGER_HANDLE_T  logger,
229    U32                  log_object_mask,
230    U8                   verbosity_mask
231 )
232 {
233    sci_base_logger_disable_log_object(
234       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_ERROR
235    );
236 
237    sci_base_logger_disable_log_object(
238       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_WARNING
239    );
240 
241    sci_base_logger_disable_log_object(
242       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_INFO
243    );
244 
245    sci_base_logger_disable_log_object(
246       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_TRACE
247    );
248 
249    sci_base_logger_disable_log_object(
250       logger, log_object_mask, verbosity_mask, SCI_LOG_VERBOSITY_STATES
251    );
252 }
253 
254 // ---------------------------------------------------------------------------
255 
256 BOOL sci_logger_is_enabled(
257    SCI_LOGGER_HANDLE_T  logger,
258    U32                  log_object_mask,
259    U8                   verbosity_level
260 )
261 {
262    SCI_BASE_LOGGER_T * base_logger = (SCI_BASE_LOGGER_T*) logger;
263 
264    if (  (base_logger->verbosity_mask & (1<<verbosity_level))
265       && (base_logger->object_mask[verbosity_level] & log_object_mask) )
266       return TRUE;
267 
268    return FALSE;
269 }
270 
271 //******************************************************************************
272 //* P R O T E C T E D   M E T H O D S
273 //******************************************************************************
274 
275 void sci_base_logger_construct(
276    SCI_BASE_LOGGER_T *this_logger
277 )
278 {
279    int index;
280 
281    sci_base_object_construct(
282       &this_logger->parent, this_logger
283    );
284 
285    this_logger->verbosity_mask = 0;
286 
287    for (index = 0; index < SCI_BASE_LOGGER_MAX_VERBOSITY_LEVELS; index++)
288    {
289       this_logger->object_mask[index] = 0;
290    }
291 }
292 
293 #endif // SCI_LOGGING
294