2 * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
19 * bfa_log.c BFA log library
22 #include <bfa_os_inc.h>
23 #include <cs/bfa_log.h>
26 * global log info structure
28 struct bfa_log_info_s
{
29 u32 start_idx
; /* start index for a module */
30 u32 total_count
; /* total count for a module */
31 enum bfa_log_severity level
; /* global log level */
32 bfa_log_cb_t cbfn
; /* callback function */
35 static struct bfa_log_info_s bfa_log_info
[BFA_LOG_MODULE_ID_MAX
+ 1];
36 static u32 bfa_log_msg_total_count
;
37 static int bfa_log_initialized
;
39 static char *bfa_log_severity
[] =
40 { "[none]", "[critical]", "[error]", "[warn]", "[info]", "" };
43 * BFA log library initialization
45 * The log library initialization includes the following,
46 * - set log instance name and callback function
47 * - read the message array generated from xml files
48 * - calculate start index for each module
49 * - calculate message count for each module
50 * - perform error checking
52 * @param[in] log_mod - log module info
53 * @param[in] instance_name - instance name
54 * @param[in] cbfn - callback function
56 * It return 0 on success, or -1 on failure
59 bfa_log_init(struct bfa_log_mod_s
*log_mod
, char *instance_name
,
62 struct bfa_log_msgdef_s
*msg
;
65 u32 i
, pre_idx
, idx
, msg_id
;
71 strncpy(log_mod
->instance_info
, instance_name
,
72 sizeof(log_mod
->instance_info
));
74 for (i
= 0; i
<= BFA_LOG_MODULE_ID_MAX
; i
++)
75 log_mod
->log_level
[i
] = BFA_LOG_WARNING
;
78 if (bfa_log_initialized
)
81 for (i
= 0; i
<= BFA_LOG_MODULE_ID_MAX
; i
++) {
82 bfa_log_info
[i
].start_idx
= 0;
83 bfa_log_info
[i
].total_count
= 0;
84 bfa_log_info
[i
].level
= BFA_LOG_WARNING
;
85 bfa_log_info
[i
].cbfn
= cbfn
;
90 msg
= bfa_log_msg_array
;
91 msg_id
= BFA_LOG_GET_MSG_ID(msg
);
92 pre_mod_id
= BFA_LOG_GET_MOD_ID(msg_id
);
94 cur_mod_id
= BFA_LOG_GET_MOD_ID(msg_id
);
96 if (cur_mod_id
> BFA_LOG_MODULE_ID_MAX
) {
98 "%s%s log: module id %u out of range\n",
100 bfa_log_severity
[BFA_LOG_ERROR
],
105 if (pre_mod_id
> BFA_LOG_MODULE_ID_MAX
) {
106 cbfn(log_mod
, msg_id
,
107 "%s%s log: module id %u out of range\n",
109 bfa_log_severity
[BFA_LOG_ERROR
],
114 if (cur_mod_id
!= pre_mod_id
) {
115 bfa_log_info
[pre_mod_id
].start_idx
= pre_idx
;
116 bfa_log_info
[pre_mod_id
].total_count
= idx
- pre_idx
;
117 pre_mod_id
= cur_mod_id
;
123 msg_id
= BFA_LOG_GET_MSG_ID(msg
);
126 bfa_log_info
[cur_mod_id
].start_idx
= pre_idx
;
127 bfa_log_info
[cur_mod_id
].total_count
= idx
- pre_idx
;
128 bfa_log_msg_total_count
= idx
;
130 cbfn(log_mod
, msg_id
, "%s%s log: init OK, msg total count %u\n",
132 bfa_log_severity
[BFA_LOG_INFO
], bfa_log_msg_total_count
);
134 bfa_log_initialized
= 1;
140 * BFA log set log level for a module
142 * @param[in] log_mod - log module info
143 * @param[in] mod_id - module id
144 * @param[in] log_level - log severity level
146 * It return BFA_STATUS_OK on success, or > 0 on failure
149 bfa_log_set_level(struct bfa_log_mod_s
*log_mod
, int mod_id
,
150 enum bfa_log_severity log_level
)
152 if (mod_id
<= BFA_LOG_UNUSED_ID
|| mod_id
> BFA_LOG_MODULE_ID_MAX
)
153 return BFA_STATUS_EINVAL
;
155 if (log_level
<= BFA_LOG_INVALID
|| log_level
> BFA_LOG_LEVEL_MAX
)
156 return BFA_STATUS_EINVAL
;
159 log_mod
->log_level
[mod_id
] = log_level
;
161 bfa_log_info
[mod_id
].level
= log_level
;
163 return BFA_STATUS_OK
;
167 * BFA log set log level for all modules
169 * @param[in] log_mod - log module info
170 * @param[in] log_level - log severity level
172 * It return BFA_STATUS_OK on success, or > 0 on failure
175 bfa_log_set_level_all(struct bfa_log_mod_s
*log_mod
,
176 enum bfa_log_severity log_level
)
178 int mod_id
= BFA_LOG_UNUSED_ID
+ 1;
180 if (log_level
<= BFA_LOG_INVALID
|| log_level
> BFA_LOG_LEVEL_MAX
)
181 return BFA_STATUS_EINVAL
;
184 for (; mod_id
<= BFA_LOG_MODULE_ID_MAX
; mod_id
++)
185 log_mod
->log_level
[mod_id
] = log_level
;
187 for (; mod_id
<= BFA_LOG_MODULE_ID_MAX
; mod_id
++)
188 bfa_log_info
[mod_id
].level
= log_level
;
191 return BFA_STATUS_OK
;
195 * BFA log set log level for all aen sub-modules
197 * @param[in] log_mod - log module info
198 * @param[in] log_level - log severity level
200 * It return BFA_STATUS_OK on success, or > 0 on failure
203 bfa_log_set_level_aen(struct bfa_log_mod_s
*log_mod
,
204 enum bfa_log_severity log_level
)
206 int mod_id
= BFA_LOG_AEN_MIN
+ 1;
209 for (; mod_id
<= BFA_LOG_AEN_MAX
; mod_id
++)
210 log_mod
->log_level
[mod_id
] = log_level
;
212 for (; mod_id
<= BFA_LOG_AEN_MAX
; mod_id
++)
213 bfa_log_info
[mod_id
].level
= log_level
;
216 return BFA_STATUS_OK
;
220 * BFA log get log level for a module
222 * @param[in] log_mod - log module info
223 * @param[in] mod_id - module id
225 * It returns log level or -1 on error
227 enum bfa_log_severity
228 bfa_log_get_level(struct bfa_log_mod_s
*log_mod
, int mod_id
)
230 if (mod_id
<= BFA_LOG_UNUSED_ID
|| mod_id
> BFA_LOG_MODULE_ID_MAX
)
231 return BFA_LOG_INVALID
;
234 return (log_mod
->log_level
[mod_id
]);
236 return (bfa_log_info
[mod_id
].level
);
239 enum bfa_log_severity
240 bfa_log_get_msg_level(struct bfa_log_mod_s
*log_mod
, u32 msg_id
)
242 struct bfa_log_msgdef_s
*msg
;
243 u32 mod
= BFA_LOG_GET_MOD_ID(msg_id
);
244 u32 idx
= BFA_LOG_GET_MSG_IDX(msg_id
) - 1;
246 if (!bfa_log_initialized
)
247 return BFA_LOG_INVALID
;
249 if (mod
> BFA_LOG_MODULE_ID_MAX
)
250 return BFA_LOG_INVALID
;
252 if (idx
>= bfa_log_info
[mod
].total_count
) {
253 bfa_log_info
[mod
].cbfn(log_mod
, msg_id
,
254 "%s%s log: inconsistent idx %u vs. total count %u\n",
255 BFA_LOG_CAT_NAME
, bfa_log_severity
[BFA_LOG_ERROR
], idx
,
256 bfa_log_info
[mod
].total_count
);
257 return BFA_LOG_INVALID
;
260 msg
= bfa_log_msg_array
+ bfa_log_info
[mod
].start_idx
+ idx
;
261 if (msg_id
!= BFA_LOG_GET_MSG_ID(msg
)) {
262 bfa_log_info
[mod
].cbfn(log_mod
, msg_id
,
263 "%s%s log: inconsistent msg id %u array msg id %u\n",
264 BFA_LOG_CAT_NAME
, bfa_log_severity
[BFA_LOG_ERROR
],
265 msg_id
, BFA_LOG_GET_MSG_ID(msg
));
266 return BFA_LOG_INVALID
;
269 return BFA_LOG_GET_SEVERITY(msg
);
273 * BFA log message handling
275 * BFA log message handling finds the message based on message id and prints
276 * out the message based on its format and arguments. It also does prefix
279 * @param[in] log_mod - log module info
280 * @param[in] msg_id - message id
281 * @param[in] ... - message arguments
283 * It return 0 on success, or -1 on errors
286 bfa_log(struct bfa_log_mod_s
*log_mod
, u32 msg_id
, ...)
290 struct bfa_log_msgdef_s
*msg
;
292 u32 mod
= BFA_LOG_GET_MOD_ID(msg_id
);
293 u32 idx
= BFA_LOG_GET_MSG_IDX(msg_id
) - 1;
295 if (!bfa_log_initialized
)
298 if (mod
> BFA_LOG_MODULE_ID_MAX
)
301 if (idx
>= bfa_log_info
[mod
].total_count
) {
305 "%s%s log: inconsistent idx %u vs. total count %u\n",
306 BFA_LOG_CAT_NAME
, bfa_log_severity
[BFA_LOG_ERROR
], idx
,
307 bfa_log_info
[mod
].total_count
);
311 msg
= bfa_log_msg_array
+ bfa_log_info
[mod
].start_idx
+ idx
;
312 if (msg_id
!= BFA_LOG_GET_MSG_ID(msg
)) {
316 "%s%s log: inconsistent msg id %u array msg id %u\n",
317 BFA_LOG_CAT_NAME
, bfa_log_severity
[BFA_LOG_ERROR
],
318 msg_id
, BFA_LOG_GET_MSG_ID(msg
));
322 log_level
= log_mod
? log_mod
->log_level
[mod
] : bfa_log_info
[mod
].level
;
323 if ((BFA_LOG_GET_SEVERITY(msg
) > log_level
) &&
324 (msg
->attributes
!= BFA_LOG_ATTR_NONE
))
327 va_start(ap
, msg_id
);
328 bfa_os_vsprintf(buf
, BFA_LOG_GET_MSG_FMT_STRING(msg
), ap
);
332 log_mod
->cbfn(log_mod
, msg_id
, "%s[%s]%s%s %s: %s\n",
333 BFA_LOG_CAT_NAME
, log_mod
->instance_info
,
334 bfa_log_severity
[BFA_LOG_GET_SEVERITY(msg
)],
335 (msg
->attributes
& BFA_LOG_ATTR_AUDIT
)
336 ? " (audit) " : "", msg
->msg_value
, buf
);
338 bfa_log_info
[mod
].cbfn(log_mod
, msg_id
, "%s%s%s %s: %s\n",
340 bfa_log_severity
[BFA_LOG_GET_SEVERITY(msg
)],
341 (msg
->attributes
& BFA_LOG_ATTR_AUDIT
) ?
342 " (audit) " : "", msg
->msg_value
, buf
);