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