1 /* |
|
2 * Copyright (c) 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 "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 module contains the implementation of Util class member functions. |
|
16 * Util class provides static utility functions for logging and string |
|
17 * manipulation. |
|
18 */ |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include "util.h" |
|
22 |
|
23 // command line parameters |
|
24 extern map<string, string> g_parameters; |
|
25 |
|
26 //********************************************************************************** |
|
27 // Class Util |
|
28 // |
|
29 // Provides static utility functions for logging and string handling. |
|
30 //********************************************************************************** |
|
31 |
|
32 // The default verbose level for Util output is error |
|
33 Util::VerboseLevel Util::iVerbose(Util::error); |
|
34 |
|
35 // The default output is without timestamp |
|
36 int Util::iTimestamp(0); |
|
37 |
|
38 #ifdef ENABLE_LOG_SYNC |
|
39 Mutex Util::iCritical; |
|
40 #endif |
|
41 |
|
42 /*------------------------------------------------------------------------------- |
|
43 |
|
44 Class: Util |
|
45 |
|
46 Method: SetVerboseLevel |
|
47 |
|
48 Description: Sets new verbose level by string: |
|
49 |
|
50 "none" |
|
51 "result" |
|
52 "error" |
|
53 "info" |
|
54 "debug" |
|
55 |
|
56 Parameters: const string& aVerbose : in: Verbose level as string |
|
57 |
|
58 Return Values: None |
|
59 |
|
60 Errors/Exceptions: None |
|
61 |
|
62 -------------------------------------------------------------------------------*/ |
|
63 void Util::SetVerboseLevel(const string& aVerbose) |
|
64 { |
|
65 string verbose(aVerbose); |
|
66 ToLower(verbose); |
|
67 if (verbose.compare("result") == 0) |
|
68 { |
|
69 iVerbose = result; |
|
70 } |
|
71 else if (verbose.compare("error") == 0) |
|
72 { |
|
73 iVerbose = error; |
|
74 } |
|
75 else if (verbose.compare("info") == 0) |
|
76 { |
|
77 iVerbose = info; |
|
78 } |
|
79 else if (verbose.compare("debug") == 0) |
|
80 { |
|
81 iVerbose = debug; |
|
82 } |
|
83 else |
|
84 { |
|
85 iVerbose = none; |
|
86 } |
|
87 } |
|
88 |
|
89 /*------------------------------------------------------------------------------- |
|
90 |
|
91 Class: Util |
|
92 |
|
93 Method: SetVerboseLevel |
|
94 |
|
95 Description: Sets new verbose level by enumeration. |
|
96 |
|
97 Parameters: const VerboseLevel aVerbose : in: Verbose level |
|
98 |
|
99 Return Values: None |
|
100 |
|
101 Errors/Exceptions: None |
|
102 |
|
103 -------------------------------------------------------------------------------*/ |
|
104 void Util::SetVerboseLevel(const VerboseLevel aVerbose) |
|
105 { |
|
106 iVerbose = aVerbose; |
|
107 } |
|
108 |
|
109 /*------------------------------------------------------------------------------- |
|
110 |
|
111 Class: Util |
|
112 |
|
113 Method: GetVerboseLevel |
|
114 |
|
115 Description: Returns current verbose level. |
|
116 |
|
117 Parameters: None |
|
118 |
|
119 Return Values: VerboseLevel : Current verbose level |
|
120 |
|
121 Errors/Exceptions: None |
|
122 |
|
123 -------------------------------------------------------------------------------*/ |
|
124 Util::VerboseLevel Util::GetVerboseLevel() |
|
125 { |
|
126 return iVerbose; |
|
127 } |
|
128 |
|
129 /*------------------------------------------------------------------------------- |
|
130 |
|
131 Class: Util |
|
132 |
|
133 Method: SetTimestamp |
|
134 |
|
135 Description: Enables/disables timestamp from output by string "yes" or "no". |
|
136 |
|
137 Parameters: const string& aTimestamp : in : "yes" to enable timestamp, "no" to |
|
138 disable. |
|
139 |
|
140 Return Values: None |
|
141 |
|
142 Errors/Exceptions: None |
|
143 |
|
144 -------------------------------------------------------------------------------*/ |
|
145 void Util::SetTimestamp(const string& aTimestamp) |
|
146 { |
|
147 string timestamp(aTimestamp); |
|
148 ToLower(timestamp); |
|
149 if (timestamp.compare("true") == 0) |
|
150 { |
|
151 SetTimestamp(1); |
|
152 } |
|
153 else |
|
154 { |
|
155 SetTimestamp(0); |
|
156 } |
|
157 } |
|
158 |
|
159 /*------------------------------------------------------------------------------- |
|
160 |
|
161 Class: Util |
|
162 |
|
163 Method: SetTimestamp |
|
164 |
|
165 Description: Enables/disables timestamp from output. 0 is disabled. |
|
166 |
|
167 Parameters: const int aTimestamp : in : Enable/disable time stamp |
|
168 |
|
169 Return Values: None |
|
170 |
|
171 Errors/Exceptions: None |
|
172 |
|
173 -------------------------------------------------------------------------------*/ |
|
174 void Util::SetTimestamp(const int aTimestamp) |
|
175 { |
|
176 iTimestamp = aTimestamp; |
|
177 } |
|
178 |
|
179 /*------------------------------------------------------------------------------- |
|
180 |
|
181 Class: Util |
|
182 |
|
183 Method: ParseCmdLine |
|
184 |
|
185 Description: Parses command line parameters to map. The map which is passed |
|
186 as argument must contain all valid keys (command line swicthes). |
|
187 Function adds values to map to all keys found from command line. |
|
188 The command line switch is form <key>=<value>. Default values for |
|
189 keys can be specified. |
|
190 |
|
191 Parameters: const int aArgc : in : The number of arguments |
|
192 char **aArgs : in : Pointer array to arguments |
|
193 map<string, string>& aMap : in/out : Map containing possible command |
|
194 line switches (keys). Values of |
|
195 switches are returned as values. |
|
196 |
|
197 Return Values: None |
|
198 |
|
199 Errors/Exceptions: UtilError |
|
200 |
|
201 -------------------------------------------------------------------------------*/ |
|
202 void Util::ParseCmdLine(const int aArgc, |
|
203 char **aArgs, |
|
204 map<string, string>& aMap) throw (UtilError) |
|
205 { |
|
206 for (int i = 1; i < aArgc; i++) |
|
207 { |
|
208 string p(aArgs[i]); |
|
209 string v; |
|
210 int idx = p.find_first_of("="); |
|
211 if (idx != string::npos) |
|
212 { |
|
213 if ((idx + 1) == p.length()) |
|
214 { |
|
215 string err("Parameter '" + p.substr(0, idx) + "' without value"); |
|
216 throw UtilError(err, ERR_UTIL_NO_PARAM_VALUE); |
|
217 } |
|
218 v = &aArgs[i][idx + 1]; |
|
219 p = p.substr(0, idx); |
|
220 } |
|
221 else |
|
222 { |
|
223 v = ""; |
|
224 } |
|
225 |
|
226 // 28.03.06 Disabled this "check" to enable parameter passing to plug-ins also |
|
227 //string def; |
|
228 //GetValue(p, aMap, def); |
|
229 aMap[p] = v; |
|
230 } |
|
231 } |
|
232 |
|
233 /*------------------------------------------------------------------------------- |
|
234 |
|
235 Class: Util |
|
236 |
|
237 Method: GetValue |
|
238 |
|
239 Description: Returns value by key from a map. |
|
240 |
|
241 Parameters: const string& aKey : in : Key to value |
|
242 map<string, string>& aMap : in : Map containing valid keys |
|
243 string& aValue : out : Value of key |
|
244 |
|
245 Return Values: None |
|
246 |
|
247 Errors/Exceptions: UtilError |
|
248 |
|
249 -------------------------------------------------------------------------------*/ |
|
250 void Util::GetValue(const string& aKey, |
|
251 const map<string, |
|
252 string>& aMap, string& aValue) throw (UtilError) |
|
253 { |
|
254 map<string, string>::const_iterator i = aMap.find(aKey); |
|
255 if (i != aMap.end()) |
|
256 { |
|
257 aValue = i->second; |
|
258 } |
|
259 else |
|
260 { |
|
261 string err("Unknown parameter '" + aKey + "'"); |
|
262 throw UtilError(err, ERR_UTIL_UNKNOWN_PARAM); |
|
263 } |
|
264 } |
|
265 |
|
266 /*------------------------------------------------------------------------------- |
|
267 |
|
268 Class: Util |
|
269 |
|
270 Method: ReadProperties |
|
271 |
|
272 Description: Reads properties from a file to a map. Line beginning with |
|
273 hash character ('#') are considered as comments. The form |
|
274 of prooerties in file is <property> = <value>. |
|
275 |
|
276 Parameters: const string& aFilename : in : Filename where properties are read |
|
277 map<string, string>& aMap : out : Map where property values are put |
|
278 |
|
279 Return Values: None |
|
280 |
|
281 Errors/Exceptions: UtilError |
|
282 |
|
283 -------------------------------------------------------------------------------*/ |
|
284 void Util::ReadProperties(const string& aFilename, |
|
285 map<string, string>& aMap) throw (UtilError) |
|
286 { |
|
287 ifstream in(aFilename.c_str()); |
|
288 if (!in) |
|
289 { |
|
290 string err("Cannot open properties file '"); |
|
291 err += aFilename + "'."; |
|
292 throw UtilError(err, ERR_UTIL_PROPERTIES_NOT_FOUND); |
|
293 } |
|
294 while (in) |
|
295 { |
|
296 char tmp[256]; |
|
297 in.getline(tmp, 256); |
|
298 string line(tmp); |
|
299 if (line.length() == 0 || line.find_first_of("#") == 0) |
|
300 { |
|
301 continue; |
|
302 } |
|
303 int idx = line.find_first_of('='); |
|
304 string v, p; |
|
305 if (idx != string::npos) |
|
306 { |
|
307 p = line.substr(0, idx); |
|
308 int r = p.find(" "); |
|
309 while (r != string::npos) |
|
310 { |
|
311 p.replace(r, 1, ""); |
|
312 r = p.find(" "); |
|
313 } |
|
314 v = line.substr(idx + 1); |
|
315 string::iterator i = v.begin(); |
|
316 int spaces = 0; |
|
317 while (i != v.end() && *i == ' ') |
|
318 { |
|
319 ++spaces; |
|
320 ++i; |
|
321 } |
|
322 v.erase(0, spaces); |
|
323 if (v.length() == 0) |
|
324 { |
|
325 string err = "Value not specified for parameter '"; |
|
326 err += line.substr(0, idx) + "'."; |
|
327 throw UtilError(err, ERR_UTIL_NO_PROPERTY_VALUE); |
|
328 } |
|
329 } |
|
330 else |
|
331 { |
|
332 p = line; |
|
333 v = ""; |
|
334 } |
|
335 aMap[p] = v; |
|
336 } |
|
337 } |
|
338 |
|
339 /*------------------------------------------------------------------------------- |
|
340 |
|
341 Class: Util |
|
342 |
|
343 Method: Error |
|
344 |
|
345 Description: Prints message to output in error level. |
|
346 |
|
347 Parameters: const string& aMsg : in : Error message to output |
|
348 |
|
349 Return Values: None |
|
350 |
|
351 Errors/Exceptions: None |
|
352 |
|
353 -------------------------------------------------------------------------------*/ |
|
354 void Util::Error(const string& aMsg) |
|
355 { |
|
356 Print(cerr, aMsg, error); |
|
357 } |
|
358 |
|
359 /*------------------------------------------------------------------------------- |
|
360 |
|
361 Class: Util |
|
362 |
|
363 Method: Error |
|
364 |
|
365 Description: Prints message to output in error level with error code number. |
|
366 |
|
367 Parameters: const string& aMsg : in : Error message to output |
|
368 const long aCode : in : Error code to output |
|
369 |
|
370 Return Values: None |
|
371 |
|
372 Errors/Exceptions: None |
|
373 |
|
374 -------------------------------------------------------------------------------*/ |
|
375 void Util::Error(const string& aMsg, const long aCode) |
|
376 { |
|
377 Print(cerr, aMsg, error, aCode); |
|
378 } |
|
379 |
|
380 /*------------------------------------------------------------------------------- |
|
381 |
|
382 Class: Util |
|
383 |
|
384 Method: Log |
|
385 |
|
386 Description: Prints message to output in result level. |
|
387 |
|
388 Parameters: const string& aMsg : in : Result message to output |
|
389 |
|
390 Return Values: None |
|
391 |
|
392 Errors/Exceptions: None |
|
393 |
|
394 -------------------------------------------------------------------------------*/ |
|
395 void Util::Log(const string& aMsg) |
|
396 { |
|
397 Print(cout, aMsg, result); |
|
398 } |
|
399 |
|
400 /*------------------------------------------------------------------------------- |
|
401 |
|
402 Class: Util |
|
403 |
|
404 Method: Info |
|
405 |
|
406 Description: Prints message to output in info level. |
|
407 |
|
408 Parameters: const string& aMsg : in : Info message to output |
|
409 |
|
410 Return Values: None |
|
411 |
|
412 Errors/Exceptions: None |
|
413 |
|
414 -------------------------------------------------------------------------------*/ |
|
415 void Util::Info(const string& aMsg) |
|
416 { |
|
417 Print(cout, aMsg, info); |
|
418 } |
|
419 |
|
420 /*------------------------------------------------------------------------------- |
|
421 |
|
422 Class: Util |
|
423 |
|
424 Method: Debug |
|
425 |
|
426 Description: Prints message to output in debug level. |
|
427 |
|
428 Parameters: const string& aMsg : in : Debug message to output |
|
429 |
|
430 Return Values: None |
|
431 |
|
432 Errors/Exceptions: None |
|
433 |
|
434 -------------------------------------------------------------------------------*/ |
|
435 void Util::Debug(const string& aMsg) |
|
436 { |
|
437 Print(cout, aMsg, debug); |
|
438 } |
|
439 |
|
440 /*------------------------------------------------------------------------------- |
|
441 |
|
442 Class: Util |
|
443 |
|
444 Method: Print |
|
445 |
|
446 Description: Prints output to a stream. |
|
447 |
|
448 Parameters: ostream& out : in : Stream where to write |
|
449 const string& aMsg : in : Message to output |
|
450 const VerboseLevel aLevel : in : Verbose level of message |
|
451 const long aCode : in : Possible error code if != 0 |
|
452 |
|
453 Return Values: None |
|
454 |
|
455 Errors/Exceptions: None |
|
456 |
|
457 -------------------------------------------------------------------------------*/ |
|
458 void Util::Print(ostream& out, |
|
459 const string& aMsg, |
|
460 const VerboseLevel aLevel, |
|
461 const long aCode) |
|
462 { |
|
463 #ifdef ENABLE_LOG_SYNC |
|
464 iCritical.Lock(); |
|
465 #endif |
|
466 |
|
467 if (aLevel <= iVerbose) |
|
468 { |
|
469 if (iTimestamp) |
|
470 { |
|
471 char timestamp[128]; |
|
472 _strdate(timestamp); |
|
473 char tmp[16]; |
|
474 _strtime(tmp); |
|
475 strcat(timestamp, " "); |
|
476 strcat(timestamp, tmp); |
|
477 struct _timeb tb; |
|
478 _ftime(&tb); |
|
479 sprintf(tmp, ".%03d", tb.millitm); |
|
480 strcat(timestamp, tmp); |
|
481 out << "[" << timestamp << "] "; |
|
482 } |
|
483 //out << "[" << GetCurrentThreadId() << "] "; |
|
484 out << aMsg; |
|
485 //if (aLevel == debug) |
|
486 { |
|
487 OutputDebugString(aMsg.c_str()); |
|
488 OutputDebugString("\r\n"); |
|
489 } |
|
490 if (aCode != 0) |
|
491 { |
|
492 out.setf(ios_base::hex, ios_base::basefield); |
|
493 out << " (error: 0x" << aCode << ")"; |
|
494 } |
|
495 out << endl; |
|
496 } |
|
497 #ifdef ENABLE_LOG_SYNC |
|
498 iCritical.Unlock(); |
|
499 #endif |
|
500 } |
|
501 |
|
502 /*------------------------------------------------------------------------------- |
|
503 |
|
504 Class: Util |
|
505 |
|
506 Method: ToLower |
|
507 |
|
508 Description: Converts string to lower case. |
|
509 |
|
510 Parameters: string& aString : in/out : String which is converted to lower case |
|
511 |
|
512 Return Values: string& : Reference to converted string. |
|
513 |
|
514 Errors/Exceptions: None |
|
515 |
|
516 -------------------------------------------------------------------------------*/ |
|
517 string& Util::ToLower(string& aString) |
|
518 { |
|
519 string::iterator p = aString.begin(); |
|
520 while (p != aString.end()) |
|
521 { |
|
522 *p = tolower(*p); |
|
523 ++p; |
|
524 } |
|
525 return aString; |
|
526 } |
|
527 |
|
528 /*------------------------------------------------------------------------------- |
|
529 |
|
530 Class: Util |
|
531 |
|
532 Method: ToUpper |
|
533 |
|
534 Description: Converts string to upper case. |
|
535 |
|
536 Parameters: string& aString : in/out : String which is converted to upper case |
|
537 |
|
538 Return Values: string& : Reference to converted string. |
|
539 |
|
540 Errors/Exceptions: None |
|
541 |
|
542 -------------------------------------------------------------------------------*/ |
|
543 string& Util::ToUpper(string& aString) |
|
544 { |
|
545 string::iterator p = aString.begin(); |
|
546 while (p != aString.end()) |
|
547 { |
|
548 *p = toupper(*p); |
|
549 ++p; |
|
550 } |
|
551 return aString; |
|
552 } |
|
553 |
|
554 /*------------------------------------------------------------------------------- |
|
555 |
|
556 Class: Util |
|
557 |
|
558 Method: Hex |
|
559 |
|
560 Description: Prints hex dump of char table to output. |
|
561 |
|
562 Parameters: const char aData[] : in : Data to be output as hex dump |
|
563 |
|
564 Return Values: None |
|
565 |
|
566 Errors/Exceptions: None |
|
567 |
|
568 -------------------------------------------------------------------------------*/ |
|
569 void Util::Hex(const char aData[]) |
|
570 { |
|
571 string s(aData); |
|
572 Hex(s); |
|
573 } |
|
574 |
|
575 /*------------------------------------------------------------------------------- |
|
576 |
|
577 Class: Util |
|
578 |
|
579 Method: Hex |
|
580 |
|
581 Description: Prints hex dump of string to output. |
|
582 |
|
583 Parameters: const string& aMsg : in : Data to be output as hex dump |
|
584 |
|
585 Return Values: None |
|
586 |
|
587 Errors/Exceptions: None |
|
588 |
|
589 -------------------------------------------------------------------------------*/ |
|
590 void Util::Hex(const string& aMsg) |
|
591 { |
|
592 Hex((const unsigned char *)aMsg.c_str(), aMsg.length()); |
|
593 } |
|
594 |
|
595 /*------------------------------------------------------------------------------- |
|
596 |
|
597 Class: Util |
|
598 |
|
599 Method: Hex |
|
600 |
|
601 Description: Prints hex dump of data to output. |
|
602 |
|
603 Parameters: const unsigned char* aData : in : Pointer to data to be output |
|
604 as hex dump |
|
605 const int aLength : in : The lenght of data |
|
606 |
|
607 Return Values: None |
|
608 |
|
609 Errors/Exceptions: None |
|
610 |
|
611 -------------------------------------------------------------------------------*/ |
|
612 void Util::Hex(const unsigned char* aData, const int aLength) |
|
613 { |
|
614 Hex((void *)aData, aLength); |
|
615 } |
|
616 |
|
617 /*------------------------------------------------------------------------------- |
|
618 |
|
619 Class: Util |
|
620 |
|
621 Method: Hex |
|
622 |
|
623 Description: Prints hex dump of data to output. |
|
624 |
|
625 Parameters: void *aData : in : Pointer to data to be output as hex dump |
|
626 const int aLength : in : The lenght of data |
|
627 |
|
628 Return Values: None |
|
629 |
|
630 Errors/Exceptions: None |
|
631 |
|
632 -------------------------------------------------------------------------------*/ |
|
633 void Util::Hex(void *aData, const int aLength) |
|
634 { |
|
635 Hex(aData, aLength, NULL); |
|
636 } |
|
637 |
|
638 void Util::Hex(void* aData, const int aLength, string* aHexDump) |
|
639 { |
|
640 #ifdef ENABLE_LOG_SYNC |
|
641 iCritical.Lock(); |
|
642 #endif |
|
643 |
|
644 char line[16]; |
|
645 int line_len = 16; |
|
646 int bytes_to_copy = 0; |
|
647 int printed = 0; |
|
648 bool print_out = (aHexDump == NULL ? true : false); |
|
649 unsigned char *data = (unsigned char *)aData; |
|
650 |
|
651 if (print_out) |
|
652 { |
|
653 printf("\n"); |
|
654 } |
|
655 else |
|
656 { |
|
657 aHexDump->append("\r\n"); |
|
658 } |
|
659 while (printed < aLength) |
|
660 { |
|
661 bytes_to_copy = ((aLength - printed >= line_len ? line_len : (aLength - printed))); |
|
662 memset(line, 0, sizeof(line)); |
|
663 memcpy(line, &data[printed], bytes_to_copy); |
|
664 for (int j = 0; j < line_len; j++) |
|
665 { |
|
666 char hex[4]; |
|
667 sprintf(hex, "%02X ", (unsigned char)line[j]); |
|
668 if (print_out) |
|
669 { |
|
670 printf("%s", hex); |
|
671 } |
|
672 else |
|
673 { |
|
674 string s(hex); |
|
675 aHexDump->append(s); |
|
676 } |
|
677 } |
|
678 if (print_out) printf(" | "); |
|
679 for (int j = 0; j < line_len; j++) |
|
680 { |
|
681 char c = line[j]; |
|
682 if (c >= ' ' && c <= 'z') |
|
683 { |
|
684 if (print_out) |
|
685 { |
|
686 printf("%c", c); |
|
687 } |
|
688 else |
|
689 { |
|
690 char tmp[2]; |
|
691 sprintf(tmp, "%c", c); |
|
692 string s(tmp); |
|
693 aHexDump->append(s); |
|
694 } |
|
695 } |
|
696 else |
|
697 { |
|
698 if (print_out) |
|
699 { |
|
700 printf("."); |
|
701 } |
|
702 else |
|
703 { |
|
704 aHexDump->append("."); |
|
705 } |
|
706 } |
|
707 } |
|
708 if (print_out) |
|
709 { |
|
710 printf("\n"); |
|
711 } |
|
712 else |
|
713 { |
|
714 aHexDump->append("\r\n"); |
|
715 } |
|
716 if ((printed - line_len) < aLength) |
|
717 { |
|
718 printed += 16; |
|
719 } |
|
720 else |
|
721 { |
|
722 printed = aLength - printed; |
|
723 } |
|
724 } |
|
725 if (print_out) printf("\n"); |
|
726 |
|
727 #ifdef ENABLE_LOG_SYNC |
|
728 iCritical.Unlock(); |
|
729 #endif |
|
730 } |
|
731 |
|
732 void Util::CheckCommandlineParam( const string& paramname, string& paramvalue ) |
|
733 { |
|
734 // Command line parameter overrides ini-file value |
|
735 if ( !g_parameters[paramname].empty() ) |
|
736 paramvalue = g_parameters[paramname]; |
|
737 } |
|
738 |
|
739 // End of the file |
|