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