java log ,c /c++ log add function

本文介绍了一个用于在Java源代码中自动注入日志记录宏的Perl脚本。该工具能够智能地读取Java文件,自动识别类定义及方法,并添加相应的日志记录语句。此外,它还能检查文件的有效性,确保正确地导入了Android的日志包。

------java--------

#dd_traces.pl (C) Marko Kivij?rvi 2006
# Dummy checks
die "Specify an input file!/n" if $ARGV[0] eq "";
die "File not found!/n" unless -e $ARGV[0];
die "Incorrect file extension for a C/C++ file!\n"
    if ( $ARGV[0] !~ /(.*)\.(java)$/ );


# Constants
my $IMPORT_LOG_PACKAG = "\nimport android.util.Log;";
my $TAG="";
my $LOG;


# Parse output filename from the input filename
my $file = $ARGV[0];


my $origFile = $1."-orig.".$2;
system( "cp $file $origFile" );


# Reset the input record separator (newline) so the entire file is read at once
undef $/;




# Read the input file
$_ = <>;                                 # All there


s/
    String
    \s+
    TAG
    \s*
    =
    \s*
    "
    (\w+)
    "
/
    TagFind($&,$1)    # Print the match and add the macro
/gxe;                         # g = repeat, x = split regex to multiple lines, e = evaluate substitution


s/
    import 
    \s+
    android\.util\.Log;
/
    LogFind($&)    # Print the match and add the macro
/gxe;
# Adds a tracer macro after each function definition
s/
    package #package
    \s+
    [^{;\/(\s]*\.[^{;\/(\s]*        # package name
    ;
/
    AddLogImport($&)    # Print the match and add the macro
/gxe;                         # g = repeat, x = split regex to multiple lines, e = evaluate substitution


s/
    (public|private|protected)?   #api property type
    (\s+)?
    (abstract)?
    (\s+)?
    class                                     # Possible function return type
    \s+                                       # One or more empty spaces
    (\b\w+?)                               # class name 
    (\b|\<\w+|\<\w+\>)?
    \s+
    ([^{;\/]*)?                            # extends or implements
    \{                                          # Opening curly brace
/
    AddTAG($&,$1,$2,$3,$4,$5,$6,$7) # Print the match and add the macro
/gxe;                      # g = repeat, x = split regex to multiple lines, e = evaluate substitution


s/
    (public|private|protected) #api property type
    \s+
    (\b\w*?.?\w*?\b)? # Possible function return type
    \s+ # One or more empty spaces
    (\b\w+?\b) # Function name
    \s*? # Possible empty space
    \( # Opening brace
    ([^)]*?) # Possible function parameters
    \) # Closing brace
    [^={!();#"\*\/\\\r\n]*?
    \r?
    \n?
    \{ # Opening curly brace
/


    Parse($&,$1,$2,$3,$4) # Print the match and add the macro


/gxe;            # g = repeat, x = split regex to multiple lines, e = evaluate substitution


 


open OUT, ">$file" or die "Cannot open file $file $!\n";
print OUT;
close OUT;


exit 0;


sub TagFind {
    my $match = shift;
    $TAG = shift;


    return $match;
}


sub LogFind {
    my $match = shift;


    $LOG = 1;
    return $match;
}


sub AddLogImport {
    my $match = shift;


    if ($LOG eq "1" ) {
        return $match;
    }
    my $debug = $match."\n";
    $debug .= $IMPORT_LOG_PACKAG;


    return $debug;
}


sub AddTAG {
    my $match = shift;
    if ( $TAG eq "") {
        my $s0 = shift;
        my $property = shift;
        my $abstract = shift;
        my $s1 = shift;
        # my $class = shift; 
        my $classnam = shift;
        my $s2 = shift;
        my $extends_implements = shift;


        my $debug = $match;
        $debug .= "\n private static final String  TAG = \"$classnam\";" ;


        return $debug;
    }
    return $match;
}


sub Parse {
    my $match = shift;


    my $property = shift;


    my $ret = shift;
    my $func = shift;
    my $param = shift;


    foreach ( $property, $ret, $func, $param ) {
        s/^\s+|\s+$//g;
        s/\n//g;
    }


    my $debug = $match."\n ";
    $debug .= "Log.d(TAG,\"$func\");";


    return $debug;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值