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 file contains the header file of the Util and UtilError. |
|
16 */ |
|
17 |
|
18 #ifndef UTIL_H |
|
19 #define UTIL_H |
|
20 |
|
21 #pragma warning (push, 3) |
|
22 #pragma warning( disable : 4702 ) // Unreachable code warning |
|
23 #pragma warning( disable : 4290 ) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow) |
|
24 #pragma warning ( disable : 4786 ) |
|
25 |
|
26 // INCLUDE FILES |
|
27 #include <iostream> |
|
28 #include <fstream> |
|
29 #include <map> |
|
30 #include <string> |
|
31 #include <time.h> |
|
32 #include <windows.h> |
|
33 #include <sys/timeb.h> |
|
34 #include "error.h" |
|
35 |
|
36 #define ENABLE_LOG_SYNC //makes logging methods synced to access by multiple threads |
|
37 |
|
38 #ifdef ENABLE_LOG_SYNC |
|
39 #include "sync.h" |
|
40 #endif |
|
41 |
|
42 using namespace std; |
|
43 |
|
44 //********************************************************************************** |
|
45 // Class UtilError |
|
46 // |
|
47 // Error class which is thrown if an exception occurs |
|
48 // in Util operations. Holds error code and string. |
|
49 //********************************************************************************** |
|
50 class UtilError |
|
51 { |
|
52 public: |
|
53 string iError; |
|
54 HRESULT iResult; |
|
55 UtilError(string aError, HRESULT aResult) |
|
56 { |
|
57 iError = aError; |
|
58 iResult = aResult; |
|
59 } |
|
60 }; |
|
61 |
|
62 //********************************************************************************** |
|
63 // Class Util |
|
64 // |
|
65 // Provides static utility functions for logging and string handling. |
|
66 //********************************************************************************** |
|
67 class Util |
|
68 { |
|
69 public: // Enumerations |
|
70 enum VerboseLevel {none, // no output |
|
71 result, // only result is reported |
|
72 error, // errors reported |
|
73 info, // more info reported |
|
74 debug}; |
|
75 |
|
76 public: // Constructors |
|
77 /* |
|
78 * Default constructor which initializes logging level. |
|
79 */ |
|
80 Util(const VerboseLevel aVerbose = none) { iVerbose = aVerbose; } |
|
81 |
|
82 public: |
|
83 /* |
|
84 * Returns current verbose level. |
|
85 */ |
|
86 static VerboseLevel GetVerboseLevel(); |
|
87 |
|
88 /* |
|
89 * Sets new verbose level by string "none", "result", "error", "info" or "debug" |
|
90 */ |
|
91 static void SetVerboseLevel(const string& aVerbose); |
|
92 |
|
93 /* |
|
94 * Sets new verbose level by enumeration. |
|
95 */ |
|
96 static void SetVerboseLevel(const VerboseLevel aVerbose); |
|
97 |
|
98 /* |
|
99 * Enables/disables timestamp from output by string "yes" or "no" |
|
100 */ |
|
101 static void SetTimestamp(const string& aTimestamp); |
|
102 |
|
103 /* |
|
104 * Enables/disables timestamp from output. 0 is disabled. |
|
105 */ |
|
106 static void SetTimestamp(const int aTimestamp); |
|
107 |
|
108 /* |
|
109 * Parses command line parameters to map. |
|
110 */ |
|
111 static void ParseCmdLine(const int aArgc, char **aArgs, map<string, string>& aMap) throw (UtilError); |
|
112 |
|
113 /* |
|
114 * Returns value by key from a map. |
|
115 */ |
|
116 static void GetValue(const string& aKey, const map<string, string>& aMap, string& aValue) throw (UtilError); |
|
117 |
|
118 /* |
|
119 * Reads properties from a file to a map. |
|
120 */ |
|
121 static void ReadProperties(const string& aFilename, map<string, string>& aMap) throw (UtilError); |
|
122 |
|
123 /* |
|
124 * Prints message to output in error level. |
|
125 */ |
|
126 static void Error(const string& aMsg); |
|
127 |
|
128 /* |
|
129 * Prints message to output in error level with error code number. |
|
130 */ |
|
131 static void Error(const string& aMsg, const long aCode); |
|
132 |
|
133 /* |
|
134 * Prints hex dump of data to output. |
|
135 */ |
|
136 static void Hex(void *aData, const int aLength); |
|
137 |
|
138 /* |
|
139 * Prints hex dump of char table to output. |
|
140 */ |
|
141 static void Hex(const char aData[]); |
|
142 |
|
143 /* |
|
144 * Prints hex dump of string to output. |
|
145 */ |
|
146 static void Hex(const string& aMsg); |
|
147 |
|
148 /* |
|
149 * Prints hex dump of data to output. |
|
150 */ |
|
151 static void Hex(const unsigned char* aData, const int aLength); |
|
152 |
|
153 /* |
|
154 * Prints hex dump of data to output. |
|
155 */ |
|
156 static void Hex(void* aData, const int aLength, string* aHexDump); |
|
157 |
|
158 /* |
|
159 * Prints message to output in result level. |
|
160 */ |
|
161 static void Log(const string& aMsg); |
|
162 |
|
163 /* |
|
164 * Prints message to output in info level. |
|
165 */ |
|
166 static void Info(const string& aMsg); |
|
167 |
|
168 /* |
|
169 * Prints message to output in debug level. |
|
170 */ |
|
171 static void Debug(const string& aMsg); |
|
172 |
|
173 /* |
|
174 * Prints output to a stream. |
|
175 */ |
|
176 static void Print(ostream& out, |
|
177 const string& aMsg, |
|
178 const VerboseLevel aLevel = none, |
|
179 const long aCode = 0); |
|
180 |
|
181 /* |
|
182 * Converts string to lower case. |
|
183 */ |
|
184 static string& ToLower(string& aString); |
|
185 |
|
186 /* |
|
187 * Converts string to upper case. |
|
188 */ |
|
189 static string& ToUpper(string& aString); |
|
190 |
|
191 /* |
|
192 * Sets paramvalue if command line parameter is found |
|
193 */ |
|
194 static void CheckCommandlineParam( const string& paramname, string& paramvalue ); |
|
195 |
|
196 private: |
|
197 static VerboseLevel iVerbose; |
|
198 static int iTimestamp; |
|
199 #ifdef ENABLE_LOG_SYNC |
|
200 static Mutex iCritical; |
|
201 #endif |
|
202 }; |
|
203 |
|
204 #pragma warning (pop) |
|
205 |
|
206 #endif |
|
207 |
|
208 // End of file |
|