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