User:Victor Grousset - tuxayo/Debugging

From Koha Wiki
Jump to navigation Jump to search

Perl: General

Dump variables or string literals in the logs

use Data::Dumper; $Data::Dumper::Maxdepth = 2;
warn Dumper('##### 1 #######################################################line: ' . __LINE__);
warn Dumper($foobar);
warn Dumper('##### end1 #######################################################');

Log or alter stuff during only a part of a test set

To not be flooded when inspecting code called a lot. Or to not break other previous tests when interfering with the code.

Enable a global flag in the test.

some_test_not_interesting();

$ENV{'LOG_NOW'} = 'foobar'; # whatever env var name you want with whatever value
test_that_has_an_issue();
delete $ENV{'LOG_NOW'}; # or use die; to have the database in the state after the interesting test

some_test_not_interesting();

Use it in the app code.

sub my_function_called_often() {
[...]
    if ($ENV{'LOG_NOW'}) {
        warn "logging some stuff";
    }
[...]
}

print a stack backtrace

use Carp qw<longmess>; use Data::Dumper; warn Dumper( longmess() );


Get a stack backtrace for warns and dies

Even without using Plack.

http://search.cpan.org/dist/Carp-Always/lib/Carp/Always.pm

Add this in any code ran before the part you want to debug.

use Carp::Always;

Even the call to libraries are affected

Install with apt install libcarp-always-perl

TODO: maybe try env var PERL5OPT='-MCarp::Always' instead of use;

Check if a file compiles

perl -cw /home/koha/src/acqui/histsearch.pl

It can actually execute code: https://stackoverflow.com/questions/12908416/how-to-check-if-a-perl-script-doesnt-have-any-compilation-errors#12908487

Database: Log SQL query of DBIx and also the direct queries

Install DBIx::QueryLog

cpanm DBIx::QueryLog

Enable it just before the code where you want to see the queries.

use DBIx::QueryLog;

Perl: Koha specific

dump one variable in a template

[% USE Dumper %][% Dumper.dump(myvar) %]

Dump all variable in a template

sysprefs:
DumpTemplateVarsIntranet
DumpTemplateVarsOpac

Filter some of the noise of the logs

tail -f var/log/koha-error_log | cut -d " " -f4- | sed -e 's/\[cgi:error.*AH01215://g' | sed -e 's/\\t/    /'
# get the new logs written in the file | remove date | remove pid, ip, other static stuff | Carp::Always creates "\t"'s so we convert it to spaces to actually see the indentation

Where does it ends up in Koha ?

Without Plack: koha-error_log for staff and koha-opac-error_log for opac.

DevBox: how to use a debugger

warning: not tried yet https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=18964#c5

TODO

Data::Printer

pour dump les variables use ddp; p $myVar;

Ressources

https://metacpan.org/pod/DDP https://coderwall.com/p/l5azkg/i-finally-switched-to-data-printer

See also