summaryrefslogtreecommitdiffstats
path: root/vespalog/src/vespa/log/mknm.pl
blob: 9aa2f6310ce69b64d8f642a6d182d33ed9af836b (plain) (blame)
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
#!/usr/bin/perl
# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

while (<>) {
  if ( s/.*\benum\s+LogLevel\s*\{// ) {
    chomp;
    $t = $_;
    while (<>) {
      if ( s/\}.*// ) {
        $t .= $_;
        $t =~ s/,/ /g;
        @t = split(" ", $t);
        if ( $t[$#t] ne "NUM_LOGLEVELS" ) {
          die "expected NUM_LOGLEVELS got '$t[$#t]'\n";
        }
	pop @t;
	makecpp();
      }
      $t .= $_;
    }
  }
}
die "did not find enum\n";

sub makecpp
{
        print "#include <string.h>\n";
        print '#include <vespa/log/log.h>';
        print "\n\n" . "namespace ns_log {" . "\n\n";

        print "enum Logger::LogLevel\n";
	print "Logger::parseLevel(const char *lname)\n{\n";
	foreach $l ( @t ) {
		print "    if (strcmp(lname, \"$l\") == 0) return $l;\n";
	}
	print "    // bad level name signaled by NUM_LOGLEVELS\n";
	print "    return NUM_LOGLEVELS;\n";
	print "}\n\n";

        print "const char *Logger::logLevelNames[] = {" . "\n    ";
	foreach $l ( @t ) { $l = "\"$l\""; }
	push @t, "0 // converting NUM_LOGLEVELS gives null pointer\n";
        print join(",\n    ", @t);
        print "};\n\n} // namespace\n";
	exit(0);
}