Trace.cc revision f3aaec0a97c3584095582719a0149d5e94c06ea2
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
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 * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteusing namespace std;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Tracking for the stacks
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The indentation string for output
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Log a message
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param priority The priority of the message (see syslog man page)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param msg The message string
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @doc If the debug file is present, we will log everything to
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * that file. Otherwise, if the normal log file is present,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we'll log all non-debug messages to that file. Lastly,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * if neither file is present, the message will be
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * silently discarded.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid Trace::message(int priority, const char *msg) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char message[MAX_MSG_PREFIX_LEN + MAX_MSG_LEN + 2];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // char time[CTIME_LEN+1];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* If we can open the log file, write there, else use the cim log */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fd = open(DEBUG_FILE, O_WRONLY|O_APPEND); /* will only open if exists */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* No debug file, how about the log file? */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return; /* Ignore debug */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* We catch open failures later */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // now(time);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* First interpret the priority value */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Format the prefix string for the log file */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(prefix, MAX_MSG_PREFIX_LEN, "%d:%d:%s%s:%s",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Format the message string for the log file */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte snprintf(message, strlen(prefix) + MAX_MSG_LEN + 2, "%s:%s\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } /* Else discard the log message */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Create a new Trace instance and update stack.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param myRoutine The name of the routine
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @doc This class was developed to provide a Java
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * like stack trace capability, as C++ does not provide
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * a run-time stack lookup feature. Instances of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Trace class should be created on the stack so they
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * will be automatically freed when the stack is popped
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * when the function returns.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteTrace::Trace(std::string myRoutine) : routine(myRoutine) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Delete a trace instances and update the stack
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Print out the current stack trace information
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (vector<Trace *>::size_type i = stacks[tid].size() - 1; ; i--) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (i == 0) {