imgtools/imglib/filesystem/source/messageimplementation.cpp
changeset 0 044383f39525
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 /*
       
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * This class is used get the message strings and provides operations 
       
    16 * to log them into log file
       
    17 * @internalComponent
       
    18 * @released
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 
       
    24 #include "messageimplementation.h"
       
    25 #include "errorhandler.h"
       
    26 
       
    27 using std::endl;
       
    28 using std::cout;
       
    29 typedef std::string String;
       
    30 
       
    31 char *errorMssgPrefix="FileSystem : Error:";
       
    32 char *warnMssgPrefix="FileSystem : Warning:";
       
    33 char *infoMssgPrefix="FileSystem : Information:";
       
    34 char *Space=" ";
       
    35 
       
    36 enum MessageArraySize{MAX=16};
       
    37 
       
    38 //Messages stored required for the program
       
    39 struct EnglishMessage MessageArray[MAX]=
       
    40 {
       
    41 	{FILEOPENERROR,"%s:%d: Could not open file : %s."},
       
    42 	{FILEREADERROR,"%s:%d: Could not read file : %s."},
       
    43 	{FILEWRITEERROR,"%s:%d: Could not write input image file."},
       
    44 	{MEMORYALLOCATIONERROR,"%s:%d: Memory allocation failure."},
       
    45 	{ENTRYCREATEMSG,"Creating the entry : %s."},
       
    46 	{BOOTSECTORERROR,"%s:%d: Boot Sector Error: %s."},
       
    47 	{BOOTSECTORCREATEMSG,"Creating bootsector : %s."},
       
    48 	{BOOTSECTORWRITEMSG,"Writing bootsector : %s"},
       
    49 	{FATTABLEWRITEMSG, "Writing FAT table for : %s"},
       
    50 	{IMAGESIZETOOBIG,"%s:%d: Current image size is greater than the given partition size: %s"},
       
    51 	{NOENTRIESFOUND,"%s:%d: No entries found under root."},
       
    52 	{EMPTYFILENAME,"%s:%d: Empty name received."},
       
    53 	{EMPTYSHORTNAMEERROR,"%s:%d: Empty short name."},
       
    54 	{CLUSTERERROR,"%s:%d: Cluster Instance error."},
       
    55 	{ROOTNOTFOUND,"%s:%d: None of the entries received."},
       
    56 	{UNKNOWNERROR,"%s:%d: Unknown exception occured."}
       
    57 };
       
    58 
       
    59 /**
       
    60 Constructor to reset the logging option flag.
       
    61 
       
    62 @internalComponent
       
    63 @released
       
    64 */
       
    65 MessageImplementation::MessageImplementation()
       
    66 {
       
    67     iLogging = false;
       
    68 }
       
    69 
       
    70 /**
       
    71 Destructor to close log file if logging is enabled and to clear the messaged.
       
    72 
       
    73 @internalComponent
       
    74 @released
       
    75 */
       
    76 MessageImplementation::~MessageImplementation()
       
    77 {
       
    78     if(iLogging)
       
    79     {
       
    80 		fclose(iLogPtr);
       
    81     }
       
    82 	iMessage.clear();
       
    83 }
       
    84 
       
    85 /**
       
    86 Function to Get Message stored in map.
       
    87 
       
    88 @internalComponent
       
    89 @released
       
    90 
       
    91 @param aMessageIndex - Index of the Message to be displayed
       
    92 @return Message string to be displayed
       
    93 */
       
    94 char * MessageImplementation::GetMessageString(int aMessageIndex)
       
    95 {
       
    96 	Map::iterator p = iMessage.find(aMessageIndex);
       
    97 	if(p != iMessage.end())
       
    98 	{
       
    99 		return p->second;
       
   100 	}
       
   101 	else
       
   102 	{
       
   103 		if(aMessageIndex <= MAX)
       
   104 		{
       
   105 			return MessageArray[aMessageIndex-1].message;
       
   106 		}
       
   107 		else
       
   108 		{
       
   109 			return NULL;
       
   110 		}
       
   111 	}
       
   112 }
       
   113 
       
   114 /**
       
   115 Function to log message in log file if logging is enable.
       
   116 
       
   117 @internalComponent
       
   118 @released
       
   119 
       
   120 @param aString - Message to be displayed
       
   121 */
       
   122 void MessageImplementation::LogOutput(const char *aString)
       
   123 {
       
   124     if (iLogging)
       
   125     {
       
   126 		fputs(aString,iLogPtr);
       
   127 		fputs("\n",iLogPtr);
       
   128     }
       
   129 }
       
   130 
       
   131 
       
   132 /**
       
   133 Function to display output and log message in log file if logging is enable.
       
   134 
       
   135 @internalComponent
       
   136 @released
       
   137 
       
   138 @param aString - Message to be displayed
       
   139 */
       
   140 void MessageImplementation::Output(const char *aString)
       
   141 {
       
   142 
       
   143     if (iLogging)
       
   144     {
       
   145 		fputs(aString,iLogPtr);
       
   146 		fputs("\n",iLogPtr);
       
   147     }
       
   148 	cout << aString << endl;
       
   149 }
       
   150 
       
   151 /**
       
   152 Function to Get Message stored in map and to display the Message
       
   153 
       
   154 @internalComponent
       
   155 @released
       
   156 
       
   157 @param aMessageType - The type of the message, whether it is Error or Warning or Information.
       
   158 @param aMsgIndex - The index of the information and the corresponding arguments.
       
   159 */
       
   160 void MessageImplementation::ReportMessage(int aMessageType, int aMsgIndex,...)
       
   161 {
       
   162 	String reportMessage;
       
   163 	char* ptr;
       
   164 
       
   165 	va_list ap;
       
   166 	va_start(ap,aMsgIndex);
       
   167 	
       
   168 	ptr = GetMessageString(aMsgIndex);
       
   169 	
       
   170 	if(ptr)
       
   171 	{
       
   172 		switch (aMessageType)
       
   173 		{
       
   174 			case ERROR:
       
   175 				reportMessage += errorMssgPrefix;
       
   176 				break;
       
   177 			case WARNING:
       
   178 				reportMessage += warnMssgPrefix;
       
   179 				break;
       
   180 			case INFORMATION:
       
   181 				reportMessage += infoMssgPrefix;
       
   182 				break;
       
   183 			default:
       
   184 				break;
       
   185 		}
       
   186 		reportMessage += Space;
       
   187 		reportMessage.append(ptr);
       
   188 		int location = reportMessage.find('%',0);
       
   189 		//Erase the string from % to the end, because it is no where required.
       
   190 		reportMessage.erase(location);
       
   191 		reportMessage += va_arg(ap, char *);
       
   192 
       
   193 		LogOutput(reportMessage.c_str());
       
   194 	}
       
   195 }
       
   196 
       
   197 /**
       
   198 Function to start logging.
       
   199 
       
   200 @internalComponent
       
   201 @released
       
   202 
       
   203 @param aFileName - Name of the Log file
       
   204 */
       
   205 void MessageImplementation::StartLogging(char *aFileName)
       
   206 {
       
   207 	char logFile[1024];
       
   208 	FILE *fptr;
       
   209 
       
   210 	strcpy(logFile,aFileName);
       
   211 
       
   212 	// open file for log etc.
       
   213 	if((fptr=fopen(logFile,"a"))==NULL)
       
   214 	{
       
   215 		ReportMessage(WARNING, FILEOPENERROR,aFileName);
       
   216 	}
       
   217 	else
       
   218 	{
       
   219 	    iLogging = true;
       
   220 		iLogPtr=fptr;
       
   221 	}
       
   222 }
       
   223 
       
   224 /**
       
   225 Function to put Message string in map which is stored in message file.
       
   226 If file is not available the put message in map from Message Array structure.
       
   227 
       
   228 @internalComponent
       
   229 @released
       
   230 
       
   231 @param aFileName - Name of the Message file passed in
       
   232 */
       
   233 void MessageImplementation::InitializeMessages()
       
   234 {
       
   235 	char *errStr;
       
   236 	int i;
       
   237 
       
   238 	for(i=0;i<MAX;i++)
       
   239 	{
       
   240 		errStr = new char[strlen(MessageArray[i].message) + 1];
       
   241 		strcpy(errStr, MessageArray[i].message);
       
   242 		iMessage.insert(std::pair<int,char*>(MessageArray[i].index,errStr));
       
   243 	}
       
   244 }