Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add the log categories to the documentation
[simgrid.git] / docs / bin / extract_logs_hierarchy.pl
1 #!/usr/bin/env perl
2
3 # Copyright (c) 2008-2021. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 use strict;
10 use warnings;
11
12 my $debug = 0;
13
14 print ".. Generated file, do not edit \n\n";
15 print ".. _logging_categories:\n\n";
16 print "Existing categories\n";
17 print "===================\n\n";
18
19 # Search for calls to macros defining new channels, and prepare the tree representation
20 my %ancestor;
21 my %desc;
22 # $ancestor{"toto"} is the ancestor of the toto channel as declared by XBT_LOG_NEW_SUBCATEGORY and
23 # XBT_LOG_NEW_DEFAULT_SUBCATEGORY ie, when the channel toto is initialized (does not work under windows)
24
25 # $desc{"toto"} is its description
26
27 sub cleanup_ctn {
28     my $ctn = shift;        # cleanup the content of a macro call
29     $ctn =~ s/^\s*(.*)\s*$/$1/gs;
30     my @elms;
31     print STDERR "ctn=$ctn\n" if $debug > 1;
32     if ($ctn =~ m/^(\w+)\s*,\s*(\w+)\s*,\s*"?([^"]*)"?$/s) {
33         # Perfect, we got 0->name; 1->anc; 2->desc
34         $elms[0] = $1;
35         $elms[1] = $2;
36         $elms[2] = $3;
37     } elsif ($ctn =~ m/^(\w+)\s*,\s*"?([^"]*)"?$/s) {
38         # Mmm. got no ancestor. Add the default one.
39         $elms[0] = $1;
40         $elms[1] = "XBT_LOG_ROOT_CAT";
41         $elms[2] = $2;
42     } else {
43         die "Unparsable content: $ctn\n";
44     }
45     $elms[2] =~ s/\\\\/\\/gs;
46     return @elms;
47 }
48
49 sub parse_file {
50     my $filename = shift;
51
52     my $data = "";
53
54     print STDERR "Parse $filename\n" if $debug;
55     open IN, "$filename" || die "Cannot read $filename: $!\n";
56     while (<IN>) {
57         $data .= $_;
58     }
59     close IN;
60
61     # Purge $data from C and C++ comments
62     $data =~ s|/\*.*?\*/||sg;
63     $data =~ s|//.*$||mg;
64
65     while ($data =~ s/^.*?XBT_LOG_NEW(_DEFAULT)?_(SUB)?CATEGORY\(//s) {
66         $data =~ s/([^"]*"[^"]*")\)//s || die "unparsable macro: $data";
67
68         my ($name,$anc,$desc) = cleanup_ctn($1);
69
70         # build the tree, checking for name conflict
71         die "ERROR: Category name conflict: $name used several times (in $ancestor{$name} and $anc, last time in $filename)\n"
72            if defined ($ancestor{$name}) && $ancestor{$name} ne $anc && defined ($desc{$name}) && $desc{$name} ne $desc;
73        $ancestor{$name}=$anc;
74        $desc{$name}=$desc;
75
76        print STDERR " $name -> $anc\n" if $debug;
77    }
78 }
79 # Retrieve all the file names
80 my $path = $ARGV[0] // "..";
81 open FILES, "find $path/src/ $path/tools/ $path/include/ -name '*.c' -o -name '*.cpp' |" || die "Cannot search for the source file names: $!\n";
82 while (my $file=<FILES>) {
83     chomp $file;
84     parse_file($file);  
85 }
86 parse_file("$path/include/xbt/sysdep.h");
87 close FILES;
88
89 # Display the tree, looking for disconnected elems
90 my %used;
91
92 sub display_subtree {
93     my $name=shift;
94     my $indent=shift;
95     
96     $used{$name} = 1;
97     unless ($name eq "XBT_LOG_ROOT_CAT") { # do not display the root
98         print "$indent - $name: ".($desc{$name}|| "(undocumented)")."\n";
99     }
100
101     my $state = 0; # 0: before the sublist; 1, within; 2: after
102     foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
103         if ($state == 0) {
104             $state = 1;
105             print "\n";
106         }
107         display_subtree($cat, $name eq "XBT_LOG_ROOT_CAT" ? $indent: "$indent  ");
108     }
109     if ($state != 0) {
110         print "\n";
111     }
112 }
113
114 display_subtree("XBT_LOG_ROOT_CAT","");
115
116 map {
117     warn "Category $_ does not seem to be connected to the root (anc=$ancestor{$_})\n";
118 } grep {!defined $used{$_}} sort keys %ancestor;
119
120 print "\n";