File: extract_logs_hierarchy.pl

package info (click to toggle)
simgrid 4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,236 kB
  • sloc: cpp: 124,918; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (122 lines) | stat: -rwxr-xr-x 3,812 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env perl

# Copyright (c) 2008-2025. The SimGrid Team. All rights reserved.

# This program is free software; you can redistribute it and/or modify it
# under the terms of the license (GNU LGPL) which comes with this package.

use strict;
use warnings;

my $debug = 0;

print ".. Generated file, do not edit \n\n";
print ".. _logging_categories:\n\n";
print "Existing categories\n";
print "===================\n\n";
print "This is the list of all categories existing in the SimGrid implementation. "
  ."Some of them only exist with specific compile-time options, while your implementation may add new ones. "
  ."Please add \`\`--help-log-categories\`\` to the command-line of a SimGrid simulator to see the exact list of categories usable with it.\n\n";

# Search for calls to macros defining new channels, and prepare the tree representation
my %ancestor;
my %desc;
# $ancestor{"toto"} is the ancestor of the toto channel as declared by XBT_LOG_NEW_SUBCATEGORY and
# XBT_LOG_NEW_DEFAULT_SUBCATEGORY ie, when the channel toto is initialized

# $desc{"toto"} is its description

sub cleanup_ctn {
    my $ctn = shift;        # cleanup the content of a macro call
    $ctn =~ s/^\s*(.*)\s*$/$1/gs;
    my @elms;
    print STDERR "ctn=$ctn\n" if $debug > 1;
    if ($ctn =~ m/^(\w+)\s*,\s*(\w+)\s*,\s*"?([^"]*)"?$/s) {
	    # Perfect, we got 0->name; 1->anc; 2->desc
	    $elms[0] = $1;
	    $elms[1] = $2;
	    $elms[2] = $3;
    } elsif ($ctn =~ m/^(\w+)\s*,\s*"?([^"]*)"?$/s) {
	    # Mmm. got no ancestor. Add the default one.
	    $elms[0] = $1;
	    $elms[1] = "XBT_LOG_ROOT_CAT";
	    $elms[2] = $2;
    } else {
	    die "Unparsable content: $ctn\n";
    }
    $elms[2] =~ s/\\\\/\\/gs;
    return @elms;
}

sub parse_file {
    my $filename = shift;

    my $data = "";

    print STDERR "Parse $filename\n" if $debug;
    open IN, "$filename" || die "Cannot read $filename: $!\n";
        while (<IN>) {
	    $data .= $_;
    }
    close IN;

    # Purge $data from C and C++ comments
    $data =~ s|/\*.*?\*/||sg;
    $data =~ s|//.*$||mg;

    while ($data =~ s/^.*?XBT_LOG_NEW(_DEFAULT)?_(SUB)?CATEGORY\(//s) {
	    $data =~ s/([^"]*"[^"]*")\)//s || die "unparsable macro: $data";

        my ($name,$anc,$desc) = cleanup_ctn($1);

        # build the tree, checking for name conflict
        die "ERROR: Category name conflict: $name used several times (in $ancestor{$name} and $anc, last time in $filename)\n"
	        if defined ($ancestor{$name}) && $ancestor{$name} ne $anc && defined ($desc{$name}) && $desc{$name} ne $desc;
        $ancestor{$name}=$anc;
        $desc{$name}=$desc;

        print STDERR " $name -> $anc\n" if $debug;
   }
}
# Retrieve all the file names
my $path = $ARGV[0] // "..";
open FILES, "find $path/src/ $path/tools/ $path/include/ -name '*.c' -o -name '*.cpp' |" || die "Cannot search for the source file names: $!\n";
while (my $file=<FILES>) {
    chomp $file;
    parse_file($file);
}
parse_file("$path/include/xbt/sysdep.h");
close FILES;

# Display the tree, looking for disconnected elems
my %used;

sub display_subtree {
    my $name=shift;
    my $indent=shift;

    $used{$name} = 1;
    unless ($name eq "XBT_LOG_ROOT_CAT") { # do not display the root
	print "$indent - $name: ".($desc{$name}|| "(undocumented)")."\n";
    }

    my $state = 0; # 0: before the sublist; 1, within; 2: after
    foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
	if ($state == 0) {
	    $state = 1;
	    print "\n";
	}
	display_subtree($cat, $name eq "XBT_LOG_ROOT_CAT" ? $indent: "$indent  ");
    }
    if ($state != 0) {
	print "\n";
    }
}

display_subtree("XBT_LOG_ROOT_CAT","");

map {
    warn "Category $_ does not seem to be connected to the root (anc=$ancestor{$_})\n";
} grep {!defined $used{$_}} sort keys %ancestor;

print "\n";