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