1 | //////////////////////////////////////////////////////////// |
---|
2 | // Copyright (C) Roman Ryltsov, 2008-2013 |
---|
3 | // Created by Roman Ryltsov roman@alax.info |
---|
4 | |
---|
5 | #include "stdafx.h" |
---|
6 | #include "rodshow.h" |
---|
7 | |
---|
8 | //////////////////////////////////////////////////////////// |
---|
9 | // CModule |
---|
10 | |
---|
11 | class CModule : |
---|
12 | public CAtlExeModuleT<CModule> |
---|
13 | { |
---|
14 | public: |
---|
15 | CPath m_sPath; |
---|
16 | CRoArrayT<CStringA> m_FilterArray; |
---|
17 | BOOL m_bFullFile; |
---|
18 | BOOL m_bIgnoreCase; |
---|
19 | |
---|
20 | public: |
---|
21 | // CModule |
---|
22 | CModule() throw() |
---|
23 | { |
---|
24 | AtlTraceSetDefaultSettings(); |
---|
25 | _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this); |
---|
26 | m_bFullFile = FALSE; |
---|
27 | m_bIgnoreCase = FALSE; |
---|
28 | } |
---|
29 | ~CModule() throw() |
---|
30 | { |
---|
31 | _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this); |
---|
32 | } |
---|
33 | HRESULT PreMessageLoop(INT nShowCommand) |
---|
34 | { |
---|
35 | __C(__super::PreMessageLoop(nShowCommand)); |
---|
36 | return S_OK; |
---|
37 | } |
---|
38 | VOID RunMessageLoop() |
---|
39 | { |
---|
40 | #pragma region Input Validation and Syntax |
---|
41 | if(!_tcslen(m_sPath) || m_FilterArray.IsEmpty()) |
---|
42 | { |
---|
43 | _tprintf(_T("Syntax: CheckLog <options> <log-path> <filter-1> [<filter-2> ...]\n")); |
---|
44 | _tprintf(_T(" /f - full file check, not from current position\n")); |
---|
45 | _tprintf(_T(" /i - ignore case\n")); |
---|
46 | AtlThrow(S_FALSE); |
---|
47 | } |
---|
48 | #pragma endregion |
---|
49 | _tprintf(_T("Path: %s\n"), m_sPath); |
---|
50 | CAtlFile File; |
---|
51 | __C(File.Create(m_sPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING)); |
---|
52 | if(!m_bFullFile) |
---|
53 | __C(File.Seek(0, FILE_END)); |
---|
54 | ULONGLONG nPosition; |
---|
55 | __C(File.GetPosition(nPosition)); |
---|
56 | _tprintf(_T("File Position: %I64d\n"), nPosition); |
---|
57 | _tprintf(_T("Filter: %d items\n"), m_FilterArray.GetCount()); |
---|
58 | for(SIZE_T nIndex = 0; nIndex < m_FilterArray.GetCount(); nIndex++) |
---|
59 | _tprintf(_T("\t") _T("%hs\n"), m_FilterArray[nIndex]); |
---|
60 | _tprintf(_T("\n"), m_sPath); |
---|
61 | // SUGG: Implement Console Control Handler |
---|
62 | static const SIZE_T g_nDataCapacity = 64 << 10; // 64 KB |
---|
63 | CHeapPtr<CHAR> pszData; |
---|
64 | __D(pszData.Allocate(g_nDataCapacity + 1), E_OUTOFMEMORY); |
---|
65 | SIZE_T nDataSize = 0; |
---|
66 | BOOL bWaitUpdate = FALSE; |
---|
67 | for(; ; ) |
---|
68 | { |
---|
69 | // SUGG: Read Directory Changes |
---|
70 | if(bWaitUpdate) |
---|
71 | Sleep(1000); |
---|
72 | bWaitUpdate = TRUE; |
---|
73 | const SIZE_T nLockSize = g_nDataCapacity - nDataSize; |
---|
74 | const HRESULT nLockRangeResult = File.LockRange(nPosition, nLockSize); |
---|
75 | _Z45_HRESULT(nLockRangeResult); |
---|
76 | if(FAILED(nLockRangeResult)) |
---|
77 | continue; |
---|
78 | DWORD nReadDataSize = 0; |
---|
79 | _ATLTRY |
---|
80 | { |
---|
81 | __C(File.Read(pszData + nDataSize, (DWORD) nLockSize, nReadDataSize)); |
---|
82 | } |
---|
83 | _ATLCATCHALL() |
---|
84 | { |
---|
85 | _V(File.UnlockRange(nPosition, nLockSize)); |
---|
86 | _ATLRETHROW; |
---|
87 | } |
---|
88 | __C(File.UnlockRange(nPosition, nLockSize)); |
---|
89 | if(!nReadDataSize) |
---|
90 | continue; |
---|
91 | bWaitUpdate = FALSE; |
---|
92 | nDataSize += nReadDataSize; |
---|
93 | nPosition += nReadDataSize; |
---|
94 | pszData[nDataSize] = 0; |
---|
95 | LPSTR pszDataPointer = pszData; |
---|
96 | for(; ; ) |
---|
97 | { |
---|
98 | LPSTR pszSeparator = strchr(pszDataPointer, '\n'); |
---|
99 | if(!pszSeparator) |
---|
100 | break; |
---|
101 | *pszSeparator = 0; |
---|
102 | if(pszSeparator > pszDataPointer && pszSeparator[-1] == '\r') |
---|
103 | pszSeparator[-1] = 0; |
---|
104 | BOOL bFound = FALSE; |
---|
105 | for(SIZE_T nIndex = 0; nIndex < m_FilterArray.GetCount(); nIndex++) |
---|
106 | { |
---|
107 | LPSTR pszLocation = m_bIgnoreCase ? StrStrIA(pszDataPointer, m_FilterArray[nIndex]) : strstr(pszDataPointer, m_FilterArray[nIndex]); |
---|
108 | if(pszLocation) |
---|
109 | { |
---|
110 | bFound = TRUE; |
---|
111 | break; |
---|
112 | } |
---|
113 | } |
---|
114 | if(bFound) |
---|
115 | _tprintf(_T("%hs\n"), pszDataPointer); |
---|
116 | pszDataPointer = pszSeparator + 1; |
---|
117 | } |
---|
118 | _A(pszDataPointer >= pszData); |
---|
119 | if(pszDataPointer <= pszData) |
---|
120 | continue; |
---|
121 | _A(pszDataPointer <= pszData + g_nDataCapacity); |
---|
122 | nDataSize = pszData + nDataSize - pszDataPointer; |
---|
123 | memmove(pszData, pszDataPointer, nDataSize); |
---|
124 | } |
---|
125 | } |
---|
126 | }; |
---|
127 | |
---|
128 | //////////////////////////////////////////////////////////// |
---|
129 | // Main |
---|
130 | |
---|
131 | int _tmain(int argc, _TCHAR* argv[]) |
---|
132 | { |
---|
133 | _ATLTRY |
---|
134 | { |
---|
135 | CModule Module; |
---|
136 | #pragma region Parse Command Line |
---|
137 | for(INT nIndex = 1; nIndex < argc; nIndex++) |
---|
138 | { |
---|
139 | CString sArgument = argv[nIndex]; |
---|
140 | _A(!sArgument.IsEmpty()); |
---|
141 | #pragma region Switches |
---|
142 | if(_tcschr(_T("-/"), sArgument[0])) |
---|
143 | { |
---|
144 | sArgument.Delete(0); |
---|
145 | #pragma region Switch Value/Specification |
---|
146 | CString sArgumentValue; |
---|
147 | if(sArgument.GetLength() > 1) |
---|
148 | { |
---|
149 | SIZE_T nIndex = 1; |
---|
150 | if(sArgument[1] == _T(':')) |
---|
151 | nIndex++; |
---|
152 | sArgumentValue = (LPCTSTR) sArgument + nIndex; |
---|
153 | } |
---|
154 | INT nIntegerArgumentValue = 0; |
---|
155 | const BOOL bIntegerArgumentValueValid = !sArgumentValue.IsEmpty() ? AtlStringToInteger(sArgumentValue, nIntegerArgumentValue) : FALSE; |
---|
156 | #pragma endregion |
---|
157 | //if(_tcschr(_T("Ff"), sArgument[0])) // Full File |
---|
158 | //{ |
---|
159 | // //__D(bIntegerArgumentValueValid, E_INVALIDARG); |
---|
160 | // //_tprintf(_T("Option: Sample Rate, %d\n"), nIntegerArgumentValue); |
---|
161 | // Module.m_bFullFile = TRUE; |
---|
162 | //} else |
---|
163 | if(_tcschr(_T("Ff"), sArgument[0])) // Full File |
---|
164 | { |
---|
165 | Module.m_bFullFile = TRUE; |
---|
166 | } else |
---|
167 | if(_tcschr(_T("Ii"), sArgument[0])) // Ignore Case |
---|
168 | { |
---|
169 | Module.m_bIgnoreCase = TRUE; |
---|
170 | } |
---|
171 | continue; |
---|
172 | } |
---|
173 | #pragma endregion |
---|
174 | if(sArgument.GetLength() >= 2 && sArgument[0] == _T('"') && sArgument[sArgument.GetLength() - 1] == _T('"')) |
---|
175 | sArgument = sArgument.Mid(1, sArgument.GetLength() - 2); |
---|
176 | if(!_tcslen(Module.m_sPath)) |
---|
177 | Module.m_sPath = (LPCTSTR) sArgument; |
---|
178 | else |
---|
179 | _W(Module.m_FilterArray.Add(CStringA(sArgument)) >= 0); |
---|
180 | } |
---|
181 | #pragma endregion |
---|
182 | Module.WinMain(SW_SHOWNORMAL); |
---|
183 | } |
---|
184 | _ATLCATCH(Exception) |
---|
185 | { |
---|
186 | if(FAILED((HRESULT) Exception)) |
---|
187 | _tprintf(_T("Error 0x%08X: %s\n"), (HRESULT) Exception, AtlFormatSystemMessage(Exception).TrimRight(_T("\t\n\r ."))); |
---|
188 | return (INT) (HRESULT) Exception; |
---|
189 | } |
---|
190 | _ATLCATCHALL() |
---|
191 | { |
---|
192 | _tprintf(_T("Fatal Error\n")); |
---|
193 | return (INT) E_FAIL; |
---|
194 | } |
---|
195 | return 0; |
---|
196 | } |
---|