xref: /illumos-gate/usr/src/lib/sun_fc/common/Trace.h (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte /*
22*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*fcf3ce44SJohn Forte  * Use is subject to license terms.
24*fcf3ce44SJohn Forte  */
25*fcf3ce44SJohn Forte 
26*fcf3ce44SJohn Forte #ifndef	_TRACE_H
27*fcf3ce44SJohn Forte #define	_TRACE_H
28*fcf3ce44SJohn Forte 
29*fcf3ce44SJohn Forte 
30*fcf3ce44SJohn Forte 
31*fcf3ce44SJohn Forte #include <cstdarg>
32*fcf3ce44SJohn Forte #include <string>
33*fcf3ce44SJohn Forte #include <vector>
34*fcf3ce44SJohn Forte #include <stack>
35*fcf3ce44SJohn Forte #include <pthread.h>
36*fcf3ce44SJohn Forte 
37*fcf3ce44SJohn Forte #ifndef	MAX_MSG_LEN
38*fcf3ce44SJohn Forte #define	MAX_MSG_LEN 2048
39*fcf3ce44SJohn Forte #endif
40*fcf3ce44SJohn Forte 
41*fcf3ce44SJohn Forte /*
42*fcf3ce44SJohn Forte  * @memo	    Tracing, Logging, and debugging facility
43*fcf3ce44SJohn Forte  * @field	    ONE_FIELD_DESCRIPTION
44*fcf3ce44SJohn Forte  *
45*fcf3ce44SJohn Forte  * @doc		    The Trace class provides stack tracing, and basic
46*fcf3ce44SJohn Forte  *		    logging/debugging facilities.
47*fcf3ce44SJohn Forte  */
48*fcf3ce44SJohn Forte class Trace {
49*fcf3ce44SJohn Forte public:
50*fcf3ce44SJohn Forte 	Trace(std::string myRoutine);
51*fcf3ce44SJohn Forte 
52*fcf3ce44SJohn Forte 	~Trace();
53*fcf3ce44SJohn Forte 
54*fcf3ce44SJohn Forte 	std::string label() {
55*fcf3ce44SJohn Forte 	    return (routine);
56*fcf3ce44SJohn Forte 	}
57*fcf3ce44SJohn Forte 
58*fcf3ce44SJohn Forte 	void noMemory() {
59*fcf3ce44SJohn Forte 	    message(1, "Out of memory");
60*fcf3ce44SJohn Forte 	}
61*fcf3ce44SJohn Forte 
62*fcf3ce44SJohn Forte 	void debug(const char *fmt, ...) {
63*fcf3ce44SJohn Forte 	    char msg[MAX_MSG_LEN];
64*fcf3ce44SJohn Forte 	    va_list ap;
65*fcf3ce44SJohn Forte 	    va_start(ap, fmt);
66*fcf3ce44SJohn Forte 	    vsnprintf(msg, sizeof (msg), fmt, ap);
67*fcf3ce44SJohn Forte 	    message(LOG_DEBUG, msg);
68*fcf3ce44SJohn Forte 	    va_end(ap);
69*fcf3ce44SJohn Forte 	}
70*fcf3ce44SJohn Forte 
71*fcf3ce44SJohn Forte 	void genericIOError(const char *fmt, ...) {
72*fcf3ce44SJohn Forte 	    char msg[MAX_MSG_LEN];
73*fcf3ce44SJohn Forte 	    va_list ap;
74*fcf3ce44SJohn Forte 	    va_start(ap, fmt);
75*fcf3ce44SJohn Forte 	    vsnprintf(msg, sizeof (msg), fmt, ap);
76*fcf3ce44SJohn Forte 	    message(IO_ERROR, msg);
77*fcf3ce44SJohn Forte 	    va_end(ap);
78*fcf3ce44SJohn Forte 	}
79*fcf3ce44SJohn Forte 
80*fcf3ce44SJohn Forte 	void internalError(const char *fmt, ...) {
81*fcf3ce44SJohn Forte 	    char msg[MAX_MSG_LEN];
82*fcf3ce44SJohn Forte 	    va_list ap;
83*fcf3ce44SJohn Forte 	    va_start(ap, fmt);
84*fcf3ce44SJohn Forte 	    vsnprintf(msg, sizeof (msg), fmt, ap);
85*fcf3ce44SJohn Forte 	    message(INTERNAL_ERROR, msg);
86*fcf3ce44SJohn Forte 	    va_end(ap);
87*fcf3ce44SJohn Forte 	}
88*fcf3ce44SJohn Forte 
89*fcf3ce44SJohn Forte 	void userError(const char *fmt, ...) {
90*fcf3ce44SJohn Forte 	    char msg[MAX_MSG_LEN];
91*fcf3ce44SJohn Forte 	    va_list ap;
92*fcf3ce44SJohn Forte 	    va_start(ap, fmt);
93*fcf3ce44SJohn Forte 	    vsnprintf(msg, sizeof (msg), fmt, ap);
94*fcf3ce44SJohn Forte 	    message(USER_ERROR, msg);
95*fcf3ce44SJohn Forte 	    va_end(ap);
96*fcf3ce44SJohn Forte 	}
97*fcf3ce44SJohn Forte 
98*fcf3ce44SJohn Forte 	void stackTrace();
99*fcf3ce44SJohn Forte 
100*fcf3ce44SJohn Forte private:
101*fcf3ce44SJohn Forte 	std::string routine;
102*fcf3ce44SJohn Forte 	pthread_t	tid;
103*fcf3ce44SJohn Forte 	static const int INTERNAL_ERROR = 3;
104*fcf3ce44SJohn Forte 	static const int STACK_TRACE = 4;
105*fcf3ce44SJohn Forte 	static const int IO_ERROR = 5;
106*fcf3ce44SJohn Forte 	static const int USER_ERROR = 6;
107*fcf3ce44SJohn Forte 	static const int LOG_DEBUG = 7;
108*fcf3ce44SJohn Forte 	void message(int priority, const char *msg);
109*fcf3ce44SJohn Forte 	static std::vector<std::vector<Trace *> > stacks;
110*fcf3ce44SJohn Forte 	static std::vector<std::string> indent;
111*fcf3ce44SJohn Forte };
112*fcf3ce44SJohn Forte 
113*fcf3ce44SJohn Forte #endif /* _TRACE_H */
114