在这篇文章中,我们将带领您了解PHP从记录事件中获取行号的全貌,包括php从记录事件中获取行号的方法的相关情况。同时,我们还将为您介绍有关.net–WP7的记录事件信息?、c#–如何从网格视图更新事件
在这篇文章中,我们将带领您了解PHP从记录事件中获取行号的全貌,包括php从记录事件中获取行号的方法的相关情况。同时,我们还将为您介绍有关.net – WP7的记录事件信息?、c# – 如何从网格视图更新事件中的gridview编辑事件中获取保存在字符串中的值?、java – 如何在ANTLR3树解析器@init动作中获取行号、java – 是否可以从.class文件中获取注释的行号?的知识,以帮助您更好地理解这个主题。
本文目录一览:- PHP从记录事件中获取行号(php从记录事件中获取行号的方法)
- .net – WP7的记录事件信息?
- c# – 如何从网格视图更新事件中的gridview编辑事件中获取保存在字符串中的值?
- java – 如何在ANTLR3树解析器@init动作中获取行号
- java – 是否可以从.class文件中获取注释的行号?
PHP从记录事件中获取行号(php从记录事件中获取行号的方法)
我见过__Line__但是这给了我这行所在行的行号.
例:
a.PHP只会
$log = new Logger(); $log->debug('hello'); // Say this is line #20
现在在debug()的Logger.PHP类中,我使用了__Line__ Magic Constant,例如第300行.当我运行脚本时,我希望日志条目读取’在第20行’,但它在’第300行’上读取.除了将行号传递给函数之外还有其他方法可以做到这一点吗?
示例调试类函数
public function debug($message) { if(DEBUG) { $this->calling_script = $this->getScriptBaseName(); $this->log_file = LOG_FILE_DIRECTORY."/".$this->calling_script.".log"; $this->fh = fopen($this->log_file,'a') or die("Can't open log file: ".$this->log_file); if($this->first_run) { $this->log_entry = "\n[" . date("Y-m-d H:i:s",mktime()) . "][debug][line:".__LINE__."]:\t".$message."\n"; } else { $this->log_entry = "[" . date("Y-m-d H:i:s",mktime()) . "][debug][line:".__LINE__."]:\t".$message."\n"; } fwrite($this->fh,$this->log_entry); fclose($this->fh); $this->first_run = false; } }
编辑:debug_backtrace()工作得很好!!!在下面工作
public function debug($message) { if(DEBUG) { $debug_arr = debug_backtrace(); $this->calling_script = $this->getScriptBaseName(); $this->log_file = LOG_FILE_DIRECTORY."/".$this->calling_script.".log"; $this->fh = fopen($this->log_file,mktime()) . "][debug]:\t".$message." [line:".$debug_arr[0]['line']."]\n"; } else { $this->log_entry = "[" . date("Y-m-d H:i:s",mktime()) . "][debug]:\t".$message." [line:".$debug_arr[0]['line']."]\n"; } fwrite($this->fh,$this->log_entry); fclose($this->fh); $this->first_run = false; } }
debug_backtrace
,否则始终将该行(使用__LINE__)传递给该函数.
.net – WP7的记录事件信息?
解决方法
http://tonychampion.net/blog/index.php/2011/02/logging-in-silverlight-and-wp7-with-mvvm-light/
http://silverlightlogging.codeplex.com/
http://nlog-project.org/2011/01/09/nlog-for-windows-phone-7.html
http://phoney.codeplex.com/wikipage?title=PhoneLogger%20class
http://wp7logging.codeplex.com/
c# – 如何从网格视图更新事件中的gridview编辑事件中获取保存在字符串中的值?
string name = GridView1.Rows[e.NewEditIndex].Cells[1].Text; string subname = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
我想从行更新事件gridview中的变量中获取这些值,但我不知道如何访问它们.
谢谢
解决方法
RowIndex
属性
protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; string name = row.Cells[1].Text; string subname = row.Cells[2].Text; }
要获取更新行的旧值/新值,还可以使用GridViewUpdateEventArgs.OldValues
和GridViewUpdateEventArgs.NewValues
词典.
protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e) { string oldName = e.OldValues["Name"]; string oldSubname = e.OldValues["SubName"]; string newName = e.NewValues["Name"]; string newSubname = e.NewValues["SubName"]; }
仅检测更改的值(未测试):
var changed = new Dictionary<Object,Object>(); foreach (DictionaryEntry entry in e.NewValues) { if (e.OldValues[entry.Key] != entry.Value) { changed.Add(entry.Key,entry.Value); } }
或者使用LINQ:
changed = e.NewValues.Cast<DictionaryEntry>() .Where(entry => entry.Value != e.OldValues[entry.Key]) .ToDictionary(entry => entry.Key,entry => entry.Value);
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx
java – 如何在ANTLR3树解析器@init动作中获取行号
例如,在下面的@init操作中,我想将行号与句子文本一起推送.
sentence @init { myNodeVisitor.pushScriptContext( new MyScriptContext( $sentence.text )); } : assignCommand | actionCommand; finally { m_nodeVisitor.popScriptContext(); }
我需要在执行与规则中的符号相关联的操作之前推送上下文.
有些事情不起作用:
>使用$sentence.line – 它没有定义,即使$sentence.text是.
>将释义推送到规则操作中.放置在规则之前,规则中没有令牌可用.放置在规则之后,操作发生在与规则符号关联的操作之后.
>在@init动作中使用此表达式,该动作编译但返回值0:getTreeNodeStream().getTreeAdaptor().getToken($sentence.start).getLine().编辑:实际上,这确实有效,如果$sentence.start要么是真正的令牌,要么带有参考的虚构 – 请参阅下面的Bart Kiers答案.
似乎我可以很容易地在@init规则中获得匹配的文本和第一个匹配的令牌,因此应该有一种简单的方法来获取行号.
解决方法
每个CommonTree(ANTLR中的默认Tree实现)都有一个getToken()方法,该方法返回与此树关联的Token.并且每个令牌都有一个getLine()方法,毫不奇怪,它返回此令牌的行号.
因此,如果您执行以下操作:
sentence @init { CommonTree ahead = (CommonTree)input.LT(1); int line = ahead.getToken().getLine(); System.out.println("line=" + line); } : assignCommand | actionCommand ;
您应该能够看到正在打印的正确行号.我说一些,因为在所有情况下都不会按计划进行.让我演示使用一个简单的示例语法:
grammar ASTDemo; options { output=AST; } tokens { ROOT; ACTION; } parse : sentence+ EOF -> ^(ROOT sentence+) ; sentence : assignCommand | actionCommand ; assignCommand : ID ASSIGN NUMBER -> ^(ASSIGN ID NUMBER) ; actionCommand : action ID -> ^(ACTION action ID) ; action : START | STOP ; ASSIGN : '='; START : 'start'; STOP : 'stop'; ID : ('a'..'z' | 'A'..'Z')+; NUMBER : '0'..'9'+; SPACE : (' ' | '\t' | '\r' | '\n')+ {skip();};
其树语法如下:
tree grammar ASTDemoWalker; options { output=AST; tokenVocab=ASTDemo; ASTLabelType=CommonTree; } walk : ^(ROOT sentence+) ; sentence @init { CommonTree ahead = (CommonTree)input.LT(1); int line = ahead.getToken().getLine(); System.out.println("line=" + line); } : assignCommand | actionCommand ; assignCommand : ^(ASSIGN ID NUMBER) ; actionCommand : ^(ACTION action ID) ; action : START | STOP ;
如果您运行以下测试类:
import org.antlr.runtime.*; import org.antlr.runtime.tree.*; public class Main { public static void main(String[] args) throws Exception { String src = "\n\n\nABC = 123\n\nstart ABC"; ASTDemoLexer lexer = new ASTDemoLexer(new ANTlrstringStream(src)); ASTDemoParser parser = new ASTDemoParser(new CommonTokenStream(lexer)); CommonTree root = (CommonTree)parser.parse().getTree(); ASTDemoWalker walker = new ASTDemoWalker(new CommonTreeNodeStream(root)); walker.walk(); } }
你会看到以下内容被打印出来:
line=4 line=0
如您所见,“ABC = 123”产生预期输出(第4行),但“启动ABC”没有产生(第0行).这是因为操作规则的根是一个ACTION标记,并且该标记永远不会在词法分析器中定义,只能在标记{…}块中定义.并且因为输入中并不存在,所以默认情况下会将0行附加到输入中.如果要更改行号,则需要提供一个“引用”标记作为此所谓的虚构ACTION标记的参数,该标记用于将属性复制到自身中.
因此,如果您将组合语法中的actionCommand规则更改为:
actionCommand : ref=action ID -> ^(ACTION[$ref.start] action ID) ;
行号将如预期的那样(第6行).
请注意,每个解析器规则都有一个start和end属性,分别是对第一个和最后一个令牌的引用.如果action是lexer规则(比如FOO),那么你可以省略它的.start:
actionCommand : ref=FOO ID -> ^(ACTION[$ref] action ID) ;
现在,ACTION令牌已经复制了$ref指向的所有属性,除了令牌的类型,当然是int ACTION.但这也意味着它复制了text属性,因此在我的例子中,由ref = action ID创建的AST – > ^(ACTION [$ref.start]动作ID)可能如下所示:
[text=START,type=ACTION] / \ / \ / \ [text=START,type=START] [text=ABC,type=ID]
当然,它是一个合适的AST,因为节点的类型是唯一的,但它使调试混乱,因为ACTION和START共享相同的.text属性.
您可以通过提供第二个字符串参数将所有属性复制到除.text和.type之外的虚构标记,如下所示:
actionCommand : ref=action ID -> ^(ACTION[$ref.start,"Action"] action ID) ;
如果您现在再次运行相同的测试类,您将看到以下内容:
line=4 line=6
如果你检查生成的树,它将如下所示:
[type=ROOT,text='ROOT'] [type=ASSIGN,text='='] [type=ID,text='ABC'] [type=NUMBER,text='123'] [type=ACTION,text='Action'] [type=START,text='start'] [type=ID,text='ABC']
java – 是否可以从.class文件中获取注释的行号?
我试图从.class文件中获取注释的行号,但我只能得到注释列表,而不是行.是否有可能做到这一点?
由于行号文件中不存在行号信息,因此无法从类文件中提取它.
行号仅适用于代码段:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.12
关于PHP从记录事件中获取行号和php从记录事件中获取行号的方法的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于.net – WP7的记录事件信息?、c# – 如何从网格视图更新事件中的gridview编辑事件中获取保存在字符串中的值?、java – 如何在ANTLR3树解析器@init动作中获取行号、java – 是否可以从.class文件中获取注释的行号?的相关信息,请在本站寻找。
本文标签: