|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# use QgsDebugMsg instead of iostream for debugging output |
| 4 | + |
| 5 | +# EXAMPLE: |
| 6 | +# |
| 7 | +# #include <iostream> |
| 8 | +# |
| 9 | +# #ifdef QGISDEBUG |
| 10 | +# std::cout << "A " << a << " B " << b << " A " << a << std::endl; |
| 11 | +# // std::cout << "commented out" << std::endl; |
| 12 | +# #endif |
| 13 | +# |
| 14 | +# becomes |
| 15 | +# |
| 16 | +# #include "qgslogger.h" |
| 17 | +# |
| 18 | +# QgsDebugMsg(QString("A %1 B %2 A %1").arg(a).arg(b)); |
| 19 | +# QgsDebugMsgLevel("commented out", 3); |
| 20 | +# |
| 21 | +# |
| 22 | +# append // OK to keep it as it is. |
| 23 | + |
| 24 | +use strict; |
| 25 | +use warnings; |
| 26 | + |
| 27 | +for my $file (@ARGV) { |
| 28 | + my $output; |
| 29 | + my $F; |
| 30 | + my @file; |
| 31 | + |
| 32 | + open $F, $file; |
| 33 | + |
| 34 | + my $externc = 0; |
| 35 | + my $ifdef = 0; |
| 36 | + my $loggerseen=0; |
| 37 | + my $lastinclude=0; |
| 38 | + my $modified = 0; |
| 39 | + my $i=0; |
| 40 | + my $le; |
| 41 | + while(<$F>) { |
| 42 | + $i++; |
| 43 | + |
| 44 | + ($le) = /([\r\n]+)$/ unless defined $le; |
| 45 | + |
| 46 | + if(/\s*#\s*include\s*<iostream>/) { |
| 47 | + next; |
| 48 | + } |
| 49 | + |
| 50 | + if(/\s*#\s*include\s*qgslogger\.h/) { |
| 51 | + $loggerseen=1; |
| 52 | + } |
| 53 | + |
| 54 | + $externc=1 if /extern\s+\"C\"\s*{/; |
| 55 | + $externc=0 if $externc && /^\s*}\s*$/; |
| 56 | + $ifdef++ if /^\s*#\s*if/; |
| 57 | + $ifdef-- if /^\s*#\s*endif/; |
| 58 | + |
| 59 | + if($externc==0 && $ifdef==0 && /\s*#\s*include/) { |
| 60 | + $lastinclude = scalar(@file)+1; |
| 61 | + } |
| 62 | + |
| 63 | + if(/std::(cout|cerr)/) { |
| 64 | + die "nested? [$file]" if defined $output; |
| 65 | + $output = ""; |
| 66 | + } |
| 67 | + |
| 68 | + if(defined $output) { |
| 69 | + $output .= $_; |
| 70 | + if(/;/) { |
| 71 | + ($le) = ($output =~ /([\r\n]+)$/); |
| 72 | + $output =~ s/$le/\n/g; |
| 73 | + |
| 74 | + my $level = 0; |
| 75 | + if($output =~ /^\s*\/\/\s*(std::(cout|cerr))/) { |
| 76 | + $level = 3; |
| 77 | + $output =~ s/^\s*\/\///; |
| 78 | + $output =~ s/\n\s*\/\//\n /g; |
| 79 | + } |
| 80 | + |
| 81 | + my @arr = split /\s*<<\s*/, $output; |
| 82 | + my ($indent) = ($arr[0] =~ /^(\s*)/); |
| 83 | + $arr[0] =~ s/^\s+//; |
| 84 | +
|
| 85 | + if($arr[0] =~ /^\/\// || $arr[-1] =~ /\/\/ OK$/) { |
| 86 | + # commented out |
| 87 | + push @file, "$output\n"; |
| 88 | + undef $output; |
| 89 | + next; |
| 90 | + } |
| 91 | +
|
| 92 | + unless( $arr[0] =~ /^std::(cout|cerr)$/ ) { |
| 93 | + die "std::(cerr|cout) expected [$file]: |" . $arr[0] . "|"; |
| 94 | + } |
| 95 | +
|
| 96 | + $arr[-1] =~ s/\s*;\s*$/;/; |
| 97 | +
|
| 98 | + if( $arr[-1] =~ /\\n";$/) { |
| 99 | + $arr[-1] =~ s/\\n";/"/; |
| 100 | + push @arr, "std::endl;"; |
| 101 | + } elsif( $arr[-1] =~ /'\\n';$/) { |
| 102 | + $arr[-1] = "std::endl;"; |
| 103 | + } |
| 104 | +
|
| 105 | + if( $arr[-1] =~ /^std::flush;$/ && |
| 106 | + $arr[-2] =~ /^std::endl$/ ) { |
| 107 | + pop @arr; |
| 108 | + pop @arr; |
| 109 | + $arr[-1] = "std::endl;"; |
| 110 | + } |
| 111 | +
|
| 112 | + unless( $arr[-1] =~ /^std::endl;$/ ) { |
| 113 | + die "std::endl; expected [$file]: |" . $arr[-1] . "|"; |
| 114 | + } |
| 115 | +
|
| 116 | + shift @arr; |
| 117 | + pop @arr; |
| 118 | +
|
| 119 | + my $str; |
| 120 | + my %args; |
| 121 | + my @args; |
| 122 | + my $fmt = ""; |
| 123 | + foreach(@arr) { |
| 124 | + if(/^"(.*)"$/) { |
| 125 | + $fmt .= $1; |
| 126 | + } else { |
| 127 | + if(/^QString::number\s*\(\s*([^,]*)\s*\)$/) { |
| 128 | + $_ = $1; |
| 129 | + } |
| 130 | + |
| 131 | + s/\.toLocal8Bit\(\).data\(\)$//; |
| 132 | + s/\.toUtf8\(\).data\(\)$//; |
| 133 | + s/\.ascii\(\)$//; |
| 134 | +
|
| 135 | + if(exists $args{$_}) { |
| 136 | + my $n = $args{$_}; |
| 137 | + $fmt .= "%$n"; |
| 138 | + } else { |
| 139 | + push @args, $_; |
| 140 | + $args{$_} = scalar(@args); |
| 141 | + $fmt .= "%" . scalar(@args); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +
|
| 146 | + if(@args>0) { |
| 147 | + if(@args==1 && $fmt eq "%1") { |
| 148 | + $str = $args[0]; |
| 149 | + } else { |
| 150 | + $str = "QString(\"$fmt\").arg(" . join(").arg(", @args) . ")"; |
| 151 | + } |
| 152 | + } else { |
| 153 | + $str = "\"$fmt\""; |
| 154 | + } |
| 155 | +
|
| 156 | + if($level == 3) { |
| 157 | +# push @file, $indent . "QgsDebugMsgLevel($str, 3);$le"; |
| 158 | + push @file, $indent . "// QgsDebugMsg($str);$le"; |
| 159 | + } else { |
| 160 | + push @file, $indent . "QgsDebugMsg($str);$le"; |
| 161 | + } |
| 162 | +
|
| 163 | + $modified=1; |
| 164 | +
|
| 165 | + undef $output; |
| 166 | + } |
| 167 | + } else { |
| 168 | + push @file, $_; |
| 169 | + } |
| 170 | + } |
| 171 | + close $F; |
| 172 | +
|
| 173 | +
|
| 174 | + if($modified) { |
| 175 | + if(!$loggerseen) { |
| 176 | + die "no includes? [$file]" unless defined $lastinclude; |
| 177 | + splice @file, $lastinclude, 0, "#include \"qgslogger.h\"$le"; |
| 178 | + } |
| 179 | +
|
| 180 | + #print "MODIFIED: $file\n"; |
| 181 | +
|
| 182 | + my @filtered; |
| 183 | + my @output; |
| 184 | + my $ifdef_seen=0; |
| 185 | +
|
| 186 | + foreach(@file) { |
| 187 | + if($ifdef_seen) { |
| 188 | + if(/^\s*#\s*if/) { |
| 189 | + die "nested #if? [$file]"; |
| 190 | + } elsif(/^\s*QgsDebugMsg/) { |
| 191 | + push @output, $_; |
| 192 | + } elsif(/^\s*#\s*endif/) { |
| 193 | + push @filtered, $_ foreach @output; |
| 194 | + undef @output; |
| 195 | + $ifdef_seen=0; |
| 196 | + } else { |
| 197 | + push @filtered, "#ifdef QGISDEBUG$le"; |
| 198 | + push @filtered, $_ foreach @output; |
| 199 | + push @filtered, $_; |
| 200 | + undef @output; |
| 201 | + $ifdef_seen=0; |
| 202 | + } |
| 203 | + } elsif(/^\s*#\s*ifdef\s+QGISDEBUG\s*$/) { |
| 204 | + die "output pending" if @output; |
| 205 | + $ifdef_seen=1; |
| 206 | + } else { |
| 207 | + push @filtered, $_; |
| 208 | + } |
| 209 | + } |
| 210 | +
|
| 211 | + die "output pending" if @output; |
| 212 | +
|
| 213 | + link $file, "$file.iostream" unless -f "$file.iostream"; |
| 214 | + unlink $file; |
| 215 | +
|
| 216 | + open $F, ">$file"; |
| 217 | + foreach (@filtered) { |
| 218 | + print $F $_; |
| 219 | + } |
| 220 | + close $F; |
| 221 | + } |
| 222 | +} |
| 223 | +
|
| 224 | +# vim: set ts=8 noet: |
0 commit comments