1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff#ifdef __WIN__
37219820Sjeff#pragma warning(disable : 4996)
38219820Sjeff#endif
39219820Sjeff
40219820Sjeff#if HAVE_CONFIG_H
41219820Sjeff#  include <config.h>
42219820Sjeff#endif				/* HAVE_CONFIG_H */
43219820Sjeff
44219820Sjeff#include <complib/cl_log.h>
45219820Sjeff#include <complib/cl_debug.h>
46219820Sjeff#include <syslog.h>
47219820Sjeff
48219820Sjeff/* Maximum number of bytes that can be logged. */
49219820Sjeff#define CL_MAX_LOG_DATA		(256)
50219820Sjeff
51219820Sjeff/*
52219820Sjeff * Size of the character buffer to allow logging the above
53219820Sjeff * number of bytes.  A space is added after every DWORD, and
54219820Sjeff * a new line is added after 8 DWORDS (for a line length less than 80).
55219820Sjeff */
56219820Sjeff#define CL_LOG_DATA_SIZE	(CL_MAX_LOG_DATA + (CL_MAX_LOG_DATA/4))
57219820Sjeff
58219820Sjeffvoid
59219820Sjeffcl_log_event(IN const char *const name,
60219820Sjeff	     IN const cl_log_type_t type,
61219820Sjeff	     IN const char *const message,
62219820Sjeff	     IN const void *const p_data OPTIONAL, IN const uint32_t data_len)
63219820Sjeff{
64219820Sjeff	int priority, i;
65219820Sjeff	char data[CL_LOG_DATA_SIZE];
66219820Sjeff	char *p_buf;
67219820Sjeff	uint8_t *p_int_data = (uint8_t *) p_data;
68219820Sjeff
69219820Sjeff	CL_ASSERT(name);
70219820Sjeff	CL_ASSERT(message);
71219820Sjeff
72219820Sjeff	openlog(name, LOG_NDELAY | LOG_PID, LOG_USER);
73219820Sjeff	switch (type) {
74219820Sjeff	case CL_LOG_ERROR:
75219820Sjeff		priority = LOG_ERR;
76219820Sjeff		break;
77219820Sjeff
78219820Sjeff	case CL_LOG_WARN:
79219820Sjeff		priority = LOG_WARNING;
80219820Sjeff		break;
81219820Sjeff
82219820Sjeff	case CL_LOG_INFO:
83219820Sjeff	default:
84219820Sjeff		priority = LOG_INFO;
85219820Sjeff		break;
86219820Sjeff	}
87219820Sjeff
88219820Sjeff	if (p_data) {
89219820Sjeff		CL_ASSERT(data_len);
90219820Sjeff		if (data_len < CL_MAX_LOG_DATA) {
91219820Sjeff			p_buf = data;
92219820Sjeff			/* Format the data into ASCII. */
93219820Sjeff			for (i = 0; i < data_len; i++) {
94219820Sjeff				sprintf(p_buf, "%02x", *p_int_data++);
95219820Sjeff				p_buf += 2;
96219820Sjeff
97219820Sjeff				/* Add line break after 8 DWORDS. */
98219820Sjeff				if (i % 32) {
99219820Sjeff					sprintf(p_buf++, "\n");
100219820Sjeff					continue;
101219820Sjeff				}
102219820Sjeff
103219820Sjeff				/* Add a space between DWORDS. */
104219820Sjeff				if (i % 4)
105219820Sjeff					sprintf(p_buf++, " ");
106219820Sjeff			}
107219820Sjeff			syslog(priority, "%s data:\n%s\n", message, p_buf);
108219820Sjeff		} else {
109219820Sjeff			/* The data portion is too large to log. */
110219820Sjeff			cl_msg_out
111219820Sjeff			    ("cl_log() - WARNING: data too large to log.\n");
112219820Sjeff			syslog(priority, "%s\n", message);
113219820Sjeff		}
114219820Sjeff	} else {
115219820Sjeff		syslog(priority, "%s\n", message);
116219820Sjeff	}
117219820Sjeff	closelog();
118219820Sjeff}
119