xref: /onnv-gate/usr/src/cmd/fps/fptest/util.c (revision 7186:e728311aafb0)
16429Svs195195 /*
26429Svs195195  * CDDL HEADER START
36429Svs195195  *
46429Svs195195  * The contents of this file are subject to the terms of the
56429Svs195195  * Common Development and Distribution License (the "License").
66429Svs195195  * You may not use this file except in compliance with the License.
76429Svs195195  *
86429Svs195195  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96429Svs195195  * or http://www.opensolaris.org/os/licensing.
106429Svs195195  * See the License for the specific language governing permissions
116429Svs195195  * and limitations under the License.
126429Svs195195  *
136429Svs195195  * When distributing Covered Code, include this CDDL HEADER in each
146429Svs195195  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156429Svs195195  * If applicable, add the following below this CDDL HEADER, with the
166429Svs195195  * fields enclosed by brackets "[]" replaced with your own identifying
176429Svs195195  * information: Portions Copyright [yyyy] [name of copyright owner]
186429Svs195195  *
196429Svs195195  * CDDL HEADER END
206429Svs195195  */
216429Svs195195 
226429Svs195195 /*
236491Sia112686  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246429Svs195195  * Use is subject to license terms.
256429Svs195195  */
266429Svs195195 
276429Svs195195 #pragma ident	"%Z%%M%	%I%	%E% SMI"
286429Svs195195 
296429Svs195195 #include <stdio.h>
306429Svs195195 #include <sys/systeminfo.h>
316429Svs195195 #include <strings.h>
326429Svs195195 #include <netdb.h>
336429Svs195195 #include <stdarg.h>
346429Svs195195 #include <sys/time.h>
356429Svs195195 
366429Svs195195 #define	FPS_MAX_MSGLEN  4096 /* Max msg length including last null */
376429Svs195195 #define	FPS_TEST_NAME "fptest" /* Name of test app */
386429Svs195195 #define	FPS_VER_TEST    "1.0" /* Test Version */
396429Svs195195 
406429Svs195195 void fps_msg(int msg_enable, const char *fmt, ...);
416429Svs195195 static const char *msg_get_hostname();
426429Svs195195 
436429Svs195195 static const char *
msg_get_hostname(void)446429Svs195195 msg_get_hostname(void) {
456429Svs195195 	static char hname[MAXHOSTNAMELEN+1];
466429Svs195195 
476429Svs195195 	if (hname[0] == 0)
48*7186Skk158166 		(void) sysinfo(SI_HOSTNAME, hname, MAXHOSTNAMELEN);
496429Svs195195 
506429Svs195195 	return (hname);
516429Svs195195 }
526429Svs195195 
536429Svs195195 void
fps_msg(int msg_enable,const char * fmt,...)546429Svs195195 fps_msg(int msg_enable, const char *fmt, ...)
556429Svs195195 {
566429Svs195195 	char  msg_buf[FPS_MAX_MSGLEN];
576429Svs195195 	char  *msg_ptr;
586429Svs195195 	struct tm tms;
596429Svs195195 	time_t ts;
606429Svs195195 	va_list  ap;
616429Svs195195 
626429Svs195195 	va_start(ap, fmt);
636429Svs195195 
64*7186Skk158166 	if (!msg_enable) {
65*7186Skk158166 		va_end(ap);
666429Svs195195 		return;
67*7186Skk158166 	}
686429Svs195195 
69*7186Skk158166 	if (NULL == fmt) {
70*7186Skk158166 		va_end(ap);
716429Svs195195 		return;
72*7186Skk158166 	}
736429Svs195195 
74*7186Skk158166 	(void) time(&ts);
75*7186Skk158166 	(void) localtime_r(&ts, &tms);
766429Svs195195 
776429Svs195195 	msg_buf[0] = 0;
78*7186Skk158166 	(void) strftime(msg_buf, sizeof (msg_buf), "%x %X ", &tms);
796429Svs195195 
806429Svs195195 	msg_ptr = &msg_buf[strlen(msg_buf)];
81*7186Skk158166 	(void) snprintf(msg_ptr, sizeof (msg_buf) - strlen(msg_buf) - 1,
826429Svs195195 	    "%s %s(%s).%s: ",
836429Svs195195 	    msg_get_hostname(),
846429Svs195195 	    FPS_TEST_NAME, FPS_VER_TEST,
856429Svs195195 	    "verbose");
866429Svs195195 
876429Svs195195 	msg_ptr = &msg_buf[strlen(msg_buf)];
886429Svs195195 
89*7186Skk158166 	(void) vsnprintf(msg_ptr,
90*7186Skk158166 	    sizeof (msg_buf) - strlen(msg_buf) - 1, fmt, ap);
916429Svs195195 	if (msg_buf[strlen(msg_buf)-1] != '\n')
92*7186Skk158166 		(void) strcat(msg_buf, "\n");
936429Svs195195 
946429Svs195195 
956429Svs195195 	(void) fputs(msg_buf, stdout);
966429Svs195195 
976429Svs195195 	va_end(ap);
986429Svs195195 }
99