44 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
55*/
66#pragma once
7-
87#include < string>
9- #include < cstring >
8+ #include < memory >
109#include < mutex>
1110
12- enum { QISUCCESS = 0 };
13- #define LOGEXT " log"
14-
15- class auto_lock
16- {
17- std::mutex mtx;
18- public:
19- auto_lock () { mtx.lock (); }
20- ~auto_lock () { mtx.unlock (); }
21- };
2211
23- namespace FXString
12+ namespace Common
2413{
2514 std::wstring ToWString (std::string src);
2615 inline std::wstring ToWString ( const std::wstring &src ) { return src; }
27- inline std::wstring ToWString ( const CString &src) { return src.operator LPCWSTR (); }
28-
29- inline CString ToTString (const char * src) { return ToWString (src).c_str (); }
30- inline CString ToTString (const wchar_t * src) { return src; }
16+ inline std::wstring ToWString ( const LPCWSTR src ) { return std::wstring ( src ); }
3117
3218 std::string ToString ( std::wstring src);
3319 inline std::string ToString ( const std::string &src ) { return src; }
34- // inline std::string ToString( const CString &src ) { return ToString( std::wstring(src.operator LPCWSTR()) ); }
20+ inline std::string ToString ( const LPCWSTR src ) { return ToString ( std::wstring (src) ); }
21+
22+ inline CString ToCString (const char * src) { return ToWString (src).c_str (); }
23+ inline CString ToCString (const wchar_t * src) { return src; }
24+
25+ inline std::string ToString ( int src ) { return std::to_string ( src ); }
26+ inline std::string ToString ( long src ) { return std::to_string ( src ); }
27+ inline std::string ToString ( long long src ) { return std::to_string ( src ); }
28+ inline std::string ToString ( unsigned src ) { return std::to_string ( src ); }
29+ inline std::string ToString ( unsigned long src ) { return std::to_string ( src ); }
30+ inline std::string ToString ( unsigned long long src ) { return std::to_string ( src ); }
31+ inline std::string ToString ( float src ) { return std::to_string ( src ); }
32+ inline std::string ToString ( double src ) { return std::to_string ( src ); }
33+ inline std::string ToString ( long double src ) { return std::to_string ( src ); }
34+
35+ inline std::wstring ToWString ( int src ) { return std::to_wstring ( src ); }
36+ inline std::wstring ToWString ( long src ) { return std::to_wstring ( src ); }
37+ inline std::wstring ToWString ( long long src ) { return std::to_wstring ( src ); }
38+ inline std::wstring ToWString ( unsigned src ) { return std::to_wstring ( src ); }
39+ inline std::wstring ToWString ( unsigned long src ) { return std::to_wstring ( src ); }
40+ inline std::wstring ToWString ( unsigned long long src ) { return std::to_wstring ( src ); }
41+ inline std::wstring ToWString ( float src ) { return std::to_wstring ( src ); }
42+ inline std::wstring ToWString ( double src ) { return std::to_wstring ( src ); }
43+ inline std::wstring ToWString ( long double src ) { return std::to_wstring ( src ); }
44+
45+ inline CString ToCString ( int src ) { return std::to_wstring ( src ).c_str (); }
46+ inline CString ToCString ( long src ) { return std::to_wstring ( src ).c_str (); }
47+ inline CString ToCString ( long long src ) { return std::to_wstring ( src ).c_str (); }
48+ inline CString ToCString ( unsigned src ) { return std::to_wstring ( src ).c_str (); }
49+ inline CString ToCString ( unsigned long src ) { return std::to_wstring ( src ).c_str (); }
50+ inline CString ToCString ( unsigned long long src ) { return std::to_wstring ( src ).c_str (); }
51+ inline CString ToCString ( float src ) { return std::to_wstring ( src ).c_str (); }
52+ inline CString ToCString ( double src ) { return std::to_wstring ( src ).c_str (); }
53+ inline CString ToCString ( long double src ) { return std::to_wstring ( src ).c_str (); }
3554
36- std::string CopyAndReplaceAll (std::string src, const std::string &searchStr, const std::string &ReplacementStr);
37- void ReplaceAll (std::string &src, const std::string &searchStr, const std::string &ReplacementStr);
55+ std::string ReplaceInCopy (const std::string &src, const std::string &searchStr, const std::string &ReplacementStr);
56+ std::string Replace (std::string &src, const std::string &searchStr, const std::string &ReplacementStr);
57+ void ReplaceAll ( std::string &src, const std::string &searchStr, const std::string &ReplacementStr );
3858
3959 template <class strType1 , class strType2 > bool MatchesNoCase (strType1 str1, strType2 str2)
4060 {
@@ -50,6 +70,97 @@ namespace FXString
5070 inline DWORD StrDwLen ( const char * str ) { return static_cast <DWORD>(strlen ( str )); }
5171 inline DWORD StrDwLen ( const wchar_t * str ) { return static_cast <DWORD>(wcslen ( str )); }
5272 inline DWORD StrDwLen ( const CString &str ) { return static_cast <DWORD>(str.GetLength ()); }
73+
74+
75+ class errno_exception
76+ {
77+ int m_errno;
78+ std::string m_what;
79+ std::exception *m_exception;
80+ std::string m_name;
81+ public:
82+ errno_exception ( const char *name, std::exception &Exception, int no = GetLastError(), std::string s = " " ) :m_errno( no ), m_what( s.size() ? s : Exception.what() ), m_exception( &Exception ), m_name( name ) {}
83+ errno_exception ( const char *s = NULL , int no = GetLastError() ) :m_errno( no ), m_what( (s == NULL ) ? " Unknown Exception" : s ), m_exception( NULL ), m_name( (s == NULL ) ? " Unknown Exception" : " Exception" ) {}
84+ errno_exception ( std::string &s, int no = GetLastError() ) :m_errno( no ), m_what( s ), m_exception( NULL ), m_name( " Exception" ) {}
85+ const char * what () { return (m_exception == NULL ) ? m_what.c_str () : m_exception->what (); }
86+ int code () { return m_errno; }
87+ const char * name () { return m_name.c_str (); }
88+ };
89+
90+ class auto_lock
91+ {
92+ std::mutex mtx;
93+ public:
94+ auto_lock (){ mtx.lock (); }
95+ ~auto_lock (){ mtx.unlock (); }
96+ };
97+
98+ void TraceDiagnosticInfo ( std::string msg );
99+
100+ //
101+ // enum Attrib
102+ // {
103+ // #ifdef _DEBUG
104+ // DEBUG_ON = 1 << 31,
105+ // #else
106+ // DEBUG_ON = 0,
107+ // #endif
108+ // ONFIRSTPASS = 1 << 18, // Log, Popup, or trace only on first pass
109+ // LOG_ON_EXIT = 1 << 19, // Postpone logging until exit from scope
110+ //
111+ // LOG_REL = 1 << 26, // Log only in release mode. Use in conjuntion with INF, WRN, ERR, & FATAL
112+ // LOG_DBG = 1 << 27, // Log only in debug mode. Use in conjuntion with INF, WRN, ERR, & FATAL
113+ // POP_REL = 1 << 28, // Popup or Modeless only in release mode
114+ // POP_DBG = 1 << 29, // Popup or Modeless only in debug mode
115+ //
116+ // TIMEOUT10Sec = 10, // Have a 10 second time-out for MODELESS or POPUPT
117+ // TIMEOUT30Sec = 30, // Have a 30 second time-out for MODELESS or POPUPT
118+ // TIMEOUT1Min = 60, // Have a 1 minute time-out for MODELESS or POPUPT
119+ // TIMEOUT2Min = 120, // Have a 2 minutes time-out for MODELESS or POPUPT
120+ // TIMEOUT5Min = 300, // Have a 5 minutes time-out for MODELESS or POPUPT
121+ // TIMEOUT10Min = 600, // Have a 10 minutes time-out for MODELESS or POPUPT
122+ // TIMEOUT15Min = 900, // Have a 15 minutes time-out for MODELESS or POPUPT
123+ //
124+ // };
125+ //
126+ // enum LogType
127+ // {
128+ // TRACEMSG = 1 << 20, // Trace message (only in debug mode)
129+ // LOGMSG = 1 << 21, // Log to a file
130+ // STDCERR = 1 << 22, // Log to std::cerr
131+ // POPUP = 1 << 23, // Call MessageBox with logging details
132+ // MODELESS = 1 << 24, // Create a modeless window that will close automatically via time-out
133+ // POPUPT = 1 << 25, // Call MessageBox which will close automatically via time-out
134+ // };
135+ //
136+ // enum Verbosity
137+ // {
138+ // //Verbosity Level
139+ // OFF = 1 << 12, // Logging off
140+ // DBG = 1 << 13, // Debug logging
141+ // INF = 1 << 14, // Informational logging
142+ // WRN = 1 << 15, // An unexpected result occurred, but the program can continue safely
143+ // ERR = 1 << 16, // An unexpected result occurred, which stops the program from completing a required task
144+ // FATAL = 1 << 17, // An error occurred, in which the program can not continue, and must exit
145+ // };
146+ //
147+ // class Logging
148+ // {
149+ // public:
150+ // Logging( const char* SoureCode_FileName, int SourceCode_LineNo, const char* SoureCode_FunctionName, Verbosity verbosity, const wchar_t* pszFormat, CString s1 );
151+ // Logging( const char* SoureCode_FileName, int SourceCode_LineNo, const char* SoureCode_FunctionName, Verbosity verbosity, const wchar_t* pszFormat, CString s1, CString s2 );
152+ // //Logging( const char* SoureCode_FileName, int SourceCode_LineNo, const char* SoureCode_FunctionName, Verbosity verbosity, const wchar_t* pszFormat, va_list argList );
153+ // //Logging( const char* SoureCode_FileName, int SourceCode_LineNo, const char* SoureCode_FunctionName, Verbosity verbosity, const char* pszFormat, va_list argList );
154+ // static int GetGlobalLoggingSettings();
155+ // static int AddAttrib( int logattrib );
156+ // static int RemoveAttrib( int logattrib );
157+ // static int SetAttrib( int logattrib );
158+ // protected:
159+ // static int GlobalLoggingSettings;
160+ // };
53161}
54162
163+ #define REPORT_ERR_AND_EXIT (exit_err_no, msg_format, ...) {CString msg; msg.Format (_T (" Error: " msg_format " Performming early exit due to error." ),__VA_ARGS__);AfxMessageBox (msg);exit (exit_err_no);}
164+
165+ #define ERR_LOG (msg_format, ... ) {Common::Logging logging (__FILE__, __LINE__, __FUNCTION__, Common::Verbosity::ERR, msg_format __VA_OPT__ (,) __VA_ARGS__);}
55166
0 commit comments