sysmodellibs/sysmodelgen/src/common/Logger.pm
changeset 6 5b32dc297d05
parent 3 e7e0ae78773e
child 7 3c36c452f013
equal deleted inserted replaced
3:e7e0ae78773e 6:5b32dc297d05
     1 # Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 #
       
    15 
       
    16 package Logger;
       
    17 
       
    18 use FindBin;
       
    19 use lib $FindBin::Bin;
       
    20 
       
    21 use DepConstants;
       
    22 use LogItem;
       
    23 
       
    24 # Global statics:
       
    25 
       
    26 # This is expected to be set by the client code using $Logger::LOGFILE
       
    27 # If it's not defined, the logging is done to stdout
       
    28 $LOGFILE = "";
       
    29 
       
    30 $SEVERITY = DepConstants::ERROR;
       
    31 
       
    32 # Forward declarations:
       
    33 sub Log($$$$);
       
    34 sub LogFatal($$$);
       
    35 sub LogError($$$);
       
    36 sub LogWarning($$$);
       
    37 sub LogInfo($$$);
       
    38 sub LogRaw($);
       
    39 
       
    40 #-------------------------------------------------------------------------------------------------
       
    41 # Subroutine:   Log
       
    42 # Purpose:      Logs to the screen
       
    43 # Input:        Messsage, Module Code, Severity
       
    44 # Output:       None
       
    45 #-------------------------------------------------------------------------------------------------
       
    46 sub Log($$$$)
       
    47 	{
       
    48 	my $message = $_[0];
       
    49 	my $callingModule = $_[1];
       
    50 	my $severity = $_[2] ? $_[2] : DepConstants::INFO;
       
    51 	my $depth = $_[3] ? $_[3] : 0;
       
    52 	
       
    53 	# log this only if its severity level is less than or equal to the user-defined level:
       
    54 	#  -w1: errors only (default)
       
    55 	#  -w2: warnings as well as errors
       
    56 	#  -w3: info messages, warnings and errors.
       
    57 	return if $severity > $SEVERITY;
       
    58 	
       
    59 	my $code = &DepConstants::ModuleErrorCodes($callingModule, $severity);
       
    60 	my $logItem = new LogItem(msg => $message, code => $code, severity => $severity, depth => $depth);
       
    61 	&WriteToFile($logItem->LogText());
       
    62 	}
       
    63 
       
    64 #-------------------------------------------------------------------------------------------------
       
    65 # Subroutine:   LogFatal
       
    66 # Purpose:      Logs to the screen
       
    67 # Input:        Message Module Code
       
    68 # Output:       None
       
    69 #-------------------------------------------------------------------------------------------------
       
    70 sub LogFatal($$$)
       
    71 	{
       
    72 	my $message = $_[0];
       
    73 	my $callingModule = $_[1];
       
    74 	my $depth = $_[2] ? $_[2] : 0;
       
    75 	my $exitCode = $_[3] ? $_[3] : DepConstants::KFailure;
       
    76 	&Log("Fatal! ".$message, $callingModule, DepConstants::ERROR, $depth);
       
    77 	exit $exitCode;
       
    78 	}
       
    79 
       
    80 #-------------------------------------------------------------------------------------------------
       
    81 # Subroutine:   LogError
       
    82 # Purpose:      Logs to the screen
       
    83 # Input:        Message Module Code
       
    84 # Output:       None
       
    85 #-------------------------------------------------------------------------------------------------
       
    86 sub LogError($$$)
       
    87 	{
       
    88 	my $message = $_[0];
       
    89 	my $callingModule = $_[1];
       
    90 	my $depth = $_[2] ? $_[2] : 0;
       
    91 	&Log($message, $callingModule, DepConstants::ERROR, $depth);
       
    92 	}
       
    93 
       
    94 #-------------------------------------------------------------------------------------------------
       
    95 # Subroutine:   LogWarning
       
    96 # Purpose:      Logs to the screen
       
    97 # Input:        Message Module Code
       
    98 # Output:       None
       
    99 #-------------------------------------------------------------------------------------------------
       
   100 sub LogWarning($$$)
       
   101 	{
       
   102 	# first check the severity level:
       
   103 	return if $SEVERITY < DepConstants::WARNING;
       
   104 	
       
   105 	my $message = $_[0];
       
   106 	my $callingModule = $_[1];
       
   107 	my $depth = $_[2] ? $_[2] : 0;
       
   108 	&Log($message, $callingModule, DepConstants::WARNING, $depth);
       
   109 	}
       
   110 
       
   111 #-------------------------------------------------------------------------------------------------
       
   112 # Subroutine:   LogInfo
       
   113 # Purpose:      Logs to the screen
       
   114 # Input:        Message Module Code
       
   115 # Output:       None
       
   116 #-------------------------------------------------------------------------------------------------
       
   117 sub LogInfo($$$)
       
   118 	{
       
   119 	# first check the severity level:
       
   120 	return if $SEVERITY < DepConstants::INFO;
       
   121 	
       
   122 	my $message = $_[0];
       
   123 	my $callingModule = $_[1];
       
   124 	my $depth = $_[2] ? $_[2] : 0;
       
   125 	&Log($message, $callingModule, DepConstants::INFO, $depth);
       
   126 	}
       
   127 
       
   128 #-------------------------------------------------------------------------------------------------
       
   129 # Subroutine:   LogRaw
       
   130 # Purpose:      Logs a piece of raw text to the screen
       
   131 # Input:        Messsage string
       
   132 # Output:       None
       
   133 #-------------------------------------------------------------------------------------------------
       
   134 sub LogRaw($)
       
   135 	{
       
   136 	# only log raw text if the warning level is on info - i.e. the most verbose:
       
   137 	return if $SEVERITY < DepConstants::INFO;
       
   138 	&WriteToFile($_[0]);
       
   139 	}
       
   140 
       
   141 #-------------------------------------------------------------------------------------------------
       
   142 # Subroutine:   LogList
       
   143 # Purpose:      Logs a list of log items
       
   144 # Input:        array of logs starting with ERROR, WARNING or Note
       
   145 # Output:       None
       
   146 #-------------------------------------------------------------------------------------------------
       
   147 sub LogList
       
   148 	{
       
   149 	foreach $log (@_) 
       
   150 		{
       
   151 		$log.="\n";
       
   152 		if($log=~s/^ERROR:\s*//)
       
   153 			{
       
   154 			&LogError($log,DepConstants::KUnknownModuleError,1);
       
   155 			}
       
   156 		elsif($log=~s/^WARNING:\s*//)
       
   157 			{
       
   158 			&LogWarning($log,DepConstants::KUnknownModuleError,1);
       
   159 			}
       
   160 		elsif($log=~s/^Note:\s*//)
       
   161 			{
       
   162 			&LogInfo($log,DepConstants::KUnknownModuleError,1);
       
   163 			}
       
   164 		else
       
   165 			{
       
   166 			&LogRaw($log);
       
   167 			}
       
   168 		}
       
   169 	}
       
   170 
       
   171 #-------------------------------------------------------------------------------------------------
       
   172 # Subroutine:   WriteToFile
       
   173 # Purpose:      
       
   174 # Input:        A message string
       
   175 # Output:       None
       
   176 #-------------------------------------------------------------------------------------------------
       
   177 sub WriteToFile()
       
   178 	{
       
   179 	my $message = shift;
       
   180 	if ($LOGFILE ne "")
       
   181 		{
       
   182 		open(LOGFILE, ">> $LOGFILE") or die "Can't open the log file '$LOGFILE': $!";
       
   183 		print LOGFILE $message;
       
   184 		}
       
   185 	else
       
   186 		{
       
   187 		print $message; # print to stdout
       
   188 		}
       
   189 	}
       
   190 
       
   191 1;