xref: /onnv-gate/usr/src/cmd/fps/fptest/util.c (revision 7186:e728311aafb0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <sys/systeminfo.h>
31 #include <strings.h>
32 #include <netdb.h>
33 #include <stdarg.h>
34 #include <sys/time.h>
35 
36 #define	FPS_MAX_MSGLEN  4096 /* Max msg length including last null */
37 #define	FPS_TEST_NAME "fptest" /* Name of test app */
38 #define	FPS_VER_TEST    "1.0" /* Test Version */
39 
40 void fps_msg(int msg_enable, const char *fmt, ...);
41 static const char *msg_get_hostname();
42 
43 static const char *
msg_get_hostname(void)44 msg_get_hostname(void) {
45 	static char hname[MAXHOSTNAMELEN+1];
46 
47 	if (hname[0] == 0)
48 		(void) sysinfo(SI_HOSTNAME, hname, MAXHOSTNAMELEN);
49 
50 	return (hname);
51 }
52 
53 void
fps_msg(int msg_enable,const char * fmt,...)54 fps_msg(int msg_enable, const char *fmt, ...)
55 {
56 	char  msg_buf[FPS_MAX_MSGLEN];
57 	char  *msg_ptr;
58 	struct tm tms;
59 	time_t ts;
60 	va_list  ap;
61 
62 	va_start(ap, fmt);
63 
64 	if (!msg_enable) {
65 		va_end(ap);
66 		return;
67 	}
68 
69 	if (NULL == fmt) {
70 		va_end(ap);
71 		return;
72 	}
73 
74 	(void) time(&ts);
75 	(void) localtime_r(&ts, &tms);
76 
77 	msg_buf[0] = 0;
78 	(void) strftime(msg_buf, sizeof (msg_buf), "%x %X ", &tms);
79 
80 	msg_ptr = &msg_buf[strlen(msg_buf)];
81 	(void) snprintf(msg_ptr, sizeof (msg_buf) - strlen(msg_buf) - 1,
82 	    "%s %s(%s).%s: ",
83 	    msg_get_hostname(),
84 	    FPS_TEST_NAME, FPS_VER_TEST,
85 	    "verbose");
86 
87 	msg_ptr = &msg_buf[strlen(msg_buf)];
88 
89 	(void) vsnprintf(msg_ptr,
90 	    sizeof (msg_buf) - strlen(msg_buf) - 1, fmt, ap);
91 	if (msg_buf[strlen(msg_buf)-1] != '\n')
92 		(void) strcat(msg_buf, "\n");
93 
94 
95 	(void) fputs(msg_buf, stdout);
96 
97 	va_end(ap);
98 }
99