/* Monster - an advanced game scripting language Copyright (C) 2007-2009 Nicolay Korslund Email: WWW: http://monster.snaptoad.com/ This file (dbg.d) is part of the Monster script language package. Monster is distributed as free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License version 3 along with this program. If not, see http://www.gnu.org/licenses/ . */ module monster.vm.dbg; import monster.vm.thread; import std.stream; import std.string; import std.cstream; import monster.vm.fstack; import monster.options; /* This file is used for runtime debugging, stack tracing, etc. */ /* Create a trace for your function that automatically pops itself of the fstack when it goes out of scope. Usage: auto scope _t = new MTrace("funcName"); This is one of the few places where C++ is actually easier to use... An alternative way to do the same thing: dbg.trace("funcName"); scope(exit) dbg.untrace(); */ scope class MTrace { this(char[] str) { dbg.trace(str); } ~this() { dbg.untrace(); } } Dbg dbg; struct Dbg { Stream dbgOut = null; // Add indentation for each function stack level (only works if // logFStack is true in options.d) char[] logLevelString = "| "; // Called at startup void init() { static if(defaultLogToStdout) dbgOut = dout; } void log(char[] msg) { if(dbgOut !is null) { int logLevel = getFStackLevel(); int extLevel = getExtLevel(); int trdIndex = getThreadIndex(); char[] str = format("(trd=%s,ext=%s,lev=%s)", trdIndex, extLevel, logLevel); str = format("%-24s", str); dbgOut.writeString(str); // If we're logging function stack activity, put in some fancy // indentation as well. static if(logFStack) { for(int i;i