fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef _TRACE_H
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define _TRACE_H
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <cstdarg>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <vector>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stack>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <pthread.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef MAX_MSG_LEN
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAX_MSG_LEN 2048
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Tracing, Logging, and debugging facility
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @field ONE_FIELD_DESCRIPTION
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @doc The Trace class provides stack tracing, and basic
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * logging/debugging facilities.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteclass Trace {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortepublic:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace(std::string myRoutine);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ~Trace();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte std::string label() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (routine);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void noMemory() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message(1, "Out of memory");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void debug(const char *fmt, ...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char msg[MAX_MSG_LEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_list ap;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_start(ap, fmt);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte vsnprintf(msg, sizeof (msg), fmt, ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message(LOG_DEBUG, msg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_end(ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void genericIOError(const char *fmt, ...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char msg[MAX_MSG_LEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_list ap;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_start(ap, fmt);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte vsnprintf(msg, sizeof (msg), fmt, ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message(IO_ERROR, msg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_end(ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void internalError(const char *fmt, ...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char msg[MAX_MSG_LEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_list ap;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_start(ap, fmt);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte vsnprintf(msg, sizeof (msg), fmt, ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message(INTERNAL_ERROR, msg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_end(ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void userError(const char *fmt, ...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char msg[MAX_MSG_LEN];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_list ap;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_start(ap, fmt);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte vsnprintf(msg, sizeof (msg), fmt, ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message(USER_ERROR, msg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_end(ap);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void stackTrace();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteprivate:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte std::string routine;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pthread_t tid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static const int INTERNAL_ERROR = 3;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static const int STACK_TRACE = 4;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static const int IO_ERROR = 5;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static const int USER_ERROR = 6;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static const int LOG_DEBUG = 7;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void message(int priority, const char *msg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static std::vector<std::vector<Trace *> > stacks;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static std::vector<std::string> indent;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte};
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* _TRACE_H */