1###########################################################################
2# A module for logging
3#
4# Copyright (C) 2015-2018 Andrey Ponomarenko's ABI Laboratory
5#
6# Written by Andrey Ponomarenko
7#
8# This library is free software; you can redistribute it and/or
9# modify it under the terms of the GNU Lesser General Public
10# License as published by the Free Software Foundation; either
11# version 2.1 of the License, or (at your option) any later version.
12#
13# This library is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16# Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public
19# License along with this library; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21# MA  02110-1301 USA
22###########################################################################
23use strict;
24
25my (%LOG_PATH, %DEBUG_DIR);
26
27my %ERROR_CODE = (
28    # Compatible verdict
29    "Compatible"=>0,
30    "Success"=>0,
31    # Incompatible verdict
32    "Incompatible"=>1,
33    # Undifferentiated error code
34    "Error"=>2,
35    # System command is not found
36    "Not_Found"=>3,
37    # Cannot access input files
38    "Access_Error"=>4,
39    # Cannot compile header files
40    "Cannot_Compile"=>5,
41    # Header compiled with errors
42    "Compile_Error"=>6,
43    # Invalid input ABI dump
44    "Invalid_Dump"=>7,
45    # Incompatible version of ABI dump
46    "Dump_Version"=>8,
47    # Cannot find a module
48    "Module_Error"=>9,
49    # Empty intersection between
50    # headers and shared objects
51    "Empty_Intersection"=>10,
52    # Empty set of symbols in headers
53    "Empty_Set"=>11
54);
55
56sub exitStatus($$)
57{
58    my ($Code, $Msg) = @_;
59    printMsg("ERROR", $Msg);
60    exit($ERROR_CODE{$Code});
61}
62
63sub getErrorCode($) {
64    return $ERROR_CODE{$_[0]};
65}
66
67sub getCodeError($)
68{
69    my %CODE_ERROR = reverse(%ERROR_CODE);
70    return $CODE_ERROR{$_[0]};
71}
72
73sub printMsg($$)
74{
75    my ($Type, $Msg) = @_;
76    if($Type!~/\AINFO/) {
77        $Msg = $Type.": ".$Msg;
78    }
79    if($Type!~/_C\Z/) {
80        $Msg .= "\n";
81    }
82    if($In::Opt{"Quiet"})
83    { # --quiet option
84        appendFile($In::Opt{"DefaultLog"}, $Msg);
85    }
86    else
87    {
88        if($Type eq "ERROR") {
89            print STDERR $Msg;
90        }
91        else {
92            print $Msg;
93        }
94    }
95}
96
97sub initLogging($)
98{
99    my $LVer = $_[0];
100
101    # create log directory
102    my ($LogDir, $LogFile) = ("logs/".$In::Opt{"TargetLib"}."/".$In::Desc{$LVer}{"Version"}, "log.txt");
103    if(my $LogPath = $In::Desc{$LVer}{"OutputLogPath"})
104    { # user-defined by -log-path option
105        ($LogDir, $LogFile) = sepPath($LogPath);
106    }
107    if($In::Opt{"LogMode"} ne "n") {
108        mkpath($LogDir);
109    }
110    $LOG_PATH{$LVer} = join_P(getAbsPath($LogDir), $LogFile);
111    if($In::Opt{"Debug"}) {
112        initDebugging($LVer);
113    }
114
115    resetLogging($LVer);
116    resetDebugging($LVer);
117}
118
119sub initDebugging($)
120{
121    my $LVer = $_[0];
122
123    # debug directory
124    $DEBUG_DIR{$LVer} = "debug/".$In::Opt{"TargetLib"}."/".$In::Desc{$LVer}{"Version"};
125}
126
127sub getDebugDir($) {
128    return $DEBUG_DIR{$_[0]};
129}
130
131sub getExtraDir($) {
132    return $DEBUG_DIR{$_[0]}."/extra-info";
133}
134
135sub writeLog($$)
136{
137    my ($LVer, $Msg) = @_;
138    if($In::Opt{"LogMode"} ne "n") {
139        appendFile($LOG_PATH{$LVer}, $Msg);
140    }
141}
142
143sub resetLogging($)
144{
145    my $LVer = $_[0];
146    if($In::Opt{"LogMode"}!~/a|n/)
147    { # remove old log
148        unlink($LOG_PATH{$LVer});
149    }
150}
151
152sub resetDebugging($)
153{
154    my $LVer = $_[0];
155    if($In::Opt{"Debug"})
156    {
157        if(-d $DEBUG_DIR{$LVer})
158        {
159            rmtree($DEBUG_DIR{$LVer});
160        }
161
162        mkpath($DEBUG_DIR{$LVer});
163    }
164}
165
166sub printErrorLog($)
167{
168    my $LVer = $_[0];
169    if($In::Opt{"LogMode"} ne "n") {
170        printMsg("ERROR", "see log for details:\n  ".$LOG_PATH{$LVer}."\n");
171    }
172}
173
174return 1;
175