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