//
// System.Diagnostics.StackTrace.cs
//
// Author:
// Alexander Klyubin (klyubin@aqris.com)
//
// (C) 2001
//
using System;
using System.Reflection;
using System.Threading;
namespace System.Diagnostics {
///
/// Stack trace.
/// TODO: more information.
///
public class StackTrace {
///
/// Uses a constant to define the number of methods that are
/// to be omitted from the stack trace.
///
public const int METHODS_TO_SKIP = 0;
///
/// Frames. First frame is the last stack frame pushed.
///
private StackFrame[] frames;
///
/// Initializes a new instance of the StackTrace class.
///
public StackTrace() {
throw new NotImplementedException();
}
///
/// Initializes a new instance of the StackTrace class.
///
///
/// TODO:
///
public StackTrace(bool needFileInfo) : this() {}
///
/// Initializes a new instance of the StackTrace class.
///
///
/// TODO:
///
public StackTrace(Exception e) {
throw new NotImplementedException();
}
///
/// Initializes a new instance of the StackTrace class,
/// using the provided exception object. The resulting stack
/// trace describes the stack at the time of the exception.
///
///
/// TODO:
///
///
/// TODO:
///
public StackTrace(Exception e, bool needFileInfo) : this(e) {}
///
/// Initializes a new instance of the StackTrace class,
/// using the provided exception object. The resulting stack
/// trace describes the stack at the time of the exception.
///
///
/// Exception.
///
///
/// The number of frames up the stack to start the trace
/// from.
///
public StackTrace(Exception e, int skipFrames) {
throw new NotImplementedException();
}
///
/// Initializes a new instance of the StackTrace class,
/// using the provided exception object. The resulting stack
/// trace describes the stack at the time of the exception.
///
///
/// Exception.
///
///
/// The number of frames up the stack to start the trace
/// from.
///
///
/// TODO:
///
public StackTrace(Exception e, int skipFrames, bool needFileInfo)
: this(e, skipFrames) {}
///
/// Initializes a new instance of the StackTrace class
/// from the current location, in a caller's frame.
///
///
/// The number of frames up the stack to start the trace
/// from.
///
public StackTrace(int skipFrames) {
throw new NotImplementedException();
}
///
/// Initializes a new instance of the StackTrace class
/// containing a single frame.
///
///
/// The frame that the StackTrace object should contain.
///
public StackTrace(StackFrame frame) {
this.frames = new StackFrame[1];
this.frames[0] = frame;
}
///
/// Initializes a new instance of the StackTrace class
/// from the current location, in a caller's frame.
///
///
/// The number of frames up the stack to start the trace
/// from.
///
///
/// TODO:
///
public StackTrace(int skipFrames, bool needFileInfo)
: this(skipFrames) {}
///
/// Initializes a new instance of the StackTrace class.
///
///
/// TODO:
///
///
/// TODO:
///
public StackTrace(Thread targetThread, bool needFileInfo) {
throw new NotImplementedException();
}
///
/// Holds the number of frames in the stack trace.
///
public virtual int FrameCount {
get {
return (frames == null) ? 0 : frames.Length;
}
}
///
/// Gets the specified stack frame.
///
///
/// The index of the stack frame requested.
///
///
/// The specified stack frame. Returns null if
/// frame with specified index does not exist in this stack
/// trace.
///
///
/// Stack frames are numbered starting at zero, which is the
/// last stack frame pushed.
///
public virtual StackFrame GetFrame(int index) {
if ((index < 0) || (index >= FrameCount)) {
return null;
}
return frames[index];
}
///
/// Builds a readable representation of the stack trace.
///
///
/// A readable representation of the stack trace.
///
public override string ToString() {
string result = "";
for (int i = 0; i < FrameCount; i++) {
StackFrame frame = GetFrame(i);
result += "\n\tat " + FrameToString(frame);
}
return result;
}
public override bool Equals(Object obj) {
if ((obj == null) || (!(obj is StackTrace))) {
return false;
}
StackTrace rhs = (StackTrace) obj;
if (FrameCount != rhs.FrameCount) {
return false;
}
for (int i = 0; i < FrameCount; i++) {
if (!GetFrame(i).Equals(rhs.GetFrame(i))) {
return false;
}
}
return true;
}
public override int GetHashCode() {
return FrameCount;
}
///
/// Converts single stack frame to string to be used in
/// ToString method.
///
///
/// Frame to convert.
///
///
/// A readable representation of stack frame for using
/// ToString.
///
private static String FrameToString(StackFrame frame) {
string locationInfo;
if (frame.GetFileName() == null) {
// File name not available
locationInfo = "";
} else {
// File name available
locationInfo = frame.GetFileName();
if (frame.GetFileLineNumber() != 0) {
// Line number information available
locationInfo += ":line "
+ frame.GetFileLineNumber();
if (frame.GetFileColumnNumber() != 0) {
// Column number information available
locationInfo += ":column"
+ frame.GetFileColumnNumber();
}
}
}
MethodBase method = frame.GetMethod();
if (method != null) {
// Method information available
return method.DeclaringType.Name
+ "." + method.Name + "()"
+ ((locationInfo != null)
? " at " + locationInfo
: "");
} else {
// Method information not available
string methodInfo = "";
if ("".Equals(locationInfo)) {
// No location information available
return methodInfo;
}
// Location information available
return methodInfo + " at " + locationInfo;
}
}
}
}