Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : /*
24 : * IO event recording
25 : */
26 :
27 : #include "precompiled.h"
28 :
29 : #include "trace.h"
30 :
31 : #include "lib/allocators/dynarray.h"
32 : #include "lib/allocators/pool.h"
33 : #include "lib/code_annotation.h"
34 : #include "lib/debug.h"
35 : #include "lib/secure_crt.h"
36 : #include "lib/sysdep/sysdep.h" // sys_OpenFile
37 : #include "lib/timer.h" // timer_Time
38 :
39 : #include <cerrno>
40 : #include <clocale>
41 : #include <cstdint>
42 : #include <cstdio>
43 : #include <cwchar>
44 : #include <new>
45 : #include <sstream>
46 :
47 :
48 0 : /*virtual*/ ITrace::~ITrace()
49 : {
50 :
51 0 : }
52 :
53 :
54 : //-----------------------------------------------------------------------------
55 :
56 2 : TraceEntry::TraceEntry(EAction action, const Path& pathname, size_t size)
57 2 : : m_timestamp((float)timer_Time())
58 2 : , m_action(action)
59 2 : , m_pathname(pathname)
60 2 : , m_size(size)
61 : {
62 2 : }
63 :
64 :
65 3 : TraceEntry::TraceEntry(const std::wstring& text)
66 : {
67 : // swscanf is far too awkward to get working cross-platform,
68 : // so use iostreams here instead
69 :
70 3 : wchar_t dummy;
71 3 : wchar_t action;
72 :
73 3 : std::wstringstream stream(text);
74 3 : stream >> m_timestamp;
75 :
76 3 : stream >> dummy;
77 3 : ENSURE(dummy == ':');
78 :
79 3 : stream >> action;
80 3 : ENSURE(action == 'L' || action == 'S');
81 3 : m_action = (EAction)action;
82 :
83 3 : stream >> dummy;
84 3 : ENSURE(dummy == '"');
85 :
86 3 : Path::String pathname;
87 3 : std::getline(stream, pathname, L'"');
88 9 : m_pathname = Path(pathname);
89 :
90 3 : stream >> m_size;
91 :
92 3 : ENSURE(stream.get() == '\n');
93 : // NOTE: Don't use good() here - it fails due to a bug in older libc++ versions
94 3 : ENSURE(!stream.bad() && !stream.fail());
95 3 : ENSURE(stream.get() == WEOF);
96 3 : }
97 :
98 :
99 2 : std::wstring TraceEntry::EncodeAsText() const
100 : {
101 : // Ensure timestamp gets correctly encoded.
102 2 : char* oldLocale = setlocale(LC_ALL, "C");
103 :
104 2 : const wchar_t action = (wchar_t)m_action;
105 2 : wchar_t buf[1000];
106 2 : swprintf_s(buf, ARRAY_SIZE(buf), L"%#010f: %lc \"%ls\" %lu\n", m_timestamp, action, m_pathname.string().c_str(), (unsigned long)m_size);
107 2 : setlocale(LC_ALL, oldLocale);
108 2 : return buf;
109 : }
110 :
111 :
112 : //-----------------------------------------------------------------------------
113 :
114 : class Trace_Dummy : public ITrace
115 : {
116 : public:
117 143 : Trace_Dummy(size_t /*maxSize*/)
118 143 : {
119 :
120 : }
121 :
122 14088 : virtual void NotifyLoad(const Path&, size_t /*size*/)
123 : {
124 14088 : }
125 :
126 2108 : virtual void NotifyStore(const Path&, size_t /*size*/)
127 : {
128 2108 : }
129 :
130 0 : virtual Status Load(const OsPath&)
131 : {
132 0 : return INFO::OK;
133 : }
134 :
135 0 : virtual Status Store(const OsPath&) const
136 : {
137 0 : return INFO::OK;
138 : }
139 :
140 0 : virtual const TraceEntry* Entries() const
141 : {
142 0 : return 0;
143 : }
144 :
145 0 : virtual size_t NumEntries() const
146 : {
147 0 : return 0;
148 : }
149 : };
150 :
151 :
152 : //-----------------------------------------------------------------------------
153 :
154 : class Trace : public ITrace
155 : {
156 : public:
157 0 : Trace(size_t maxSize)
158 0 : {
159 0 : (void)pool_create(&m_pool, maxSize, sizeof(TraceEntry));
160 0 : }
161 :
162 0 : virtual ~Trace()
163 0 : {
164 0 : for(size_t i = 0; i < NumEntries(); i++)
165 : {
166 0 : TraceEntry* entry = (TraceEntry*)(uintptr_t(m_pool.da.base) + i*m_pool.el_size);
167 0 : entry->~TraceEntry();
168 : }
169 :
170 0 : (void)pool_destroy(&m_pool);
171 0 : }
172 :
173 0 : virtual void NotifyLoad(const Path& pathname, size_t size)
174 : {
175 0 : new(Allocate()) TraceEntry(TraceEntry::Load, pathname, size);
176 0 : }
177 :
178 0 : virtual void NotifyStore(const Path& pathname, size_t size)
179 : {
180 0 : new(Allocate()) TraceEntry(TraceEntry::Store, pathname, size);
181 0 : }
182 :
183 0 : virtual Status Load(const OsPath& pathname)
184 : {
185 0 : pool_free_all(&m_pool);
186 :
187 0 : errno = 0;
188 0 : FILE* file = sys_OpenFile(pathname, "rt");
189 0 : if(!file)
190 0 : WARN_RETURN(StatusFromErrno());
191 :
192 0 : for(;;)
193 : {
194 0 : wchar_t text[500];
195 0 : if(!fgetws(text, ARRAY_SIZE(text)-1, file))
196 : break;
197 0 : new(Allocate()) TraceEntry(text);
198 0 : }
199 0 : fclose(file);
200 :
201 0 : return INFO::OK;
202 : }
203 :
204 0 : virtual Status Store(const OsPath& pathname) const
205 : {
206 0 : errno = 0;
207 0 : FILE* file = sys_OpenFile(pathname, "at");
208 0 : if(!file)
209 0 : WARN_RETURN(StatusFromErrno());
210 0 : for(size_t i = 0; i < NumEntries(); i++)
211 : {
212 0 : std::wstring text = Entries()[i].EncodeAsText();
213 0 : fputws(text.c_str(), file);
214 0 : }
215 0 : (void)fclose(file);
216 0 : return INFO::OK;
217 : }
218 :
219 0 : virtual const TraceEntry* Entries() const
220 : {
221 0 : return (const TraceEntry*)m_pool.da.base;
222 : }
223 :
224 0 : virtual size_t NumEntries() const
225 : {
226 0 : return m_pool.da.pos / m_pool.el_size;
227 : }
228 :
229 : private:
230 0 : void* Allocate()
231 : {
232 0 : void* p = pool_alloc(&m_pool, 0);
233 0 : ENSURE(p);
234 0 : return p;
235 : }
236 :
237 : Pool m_pool;
238 : };
239 :
240 :
241 143 : PITrace CreateDummyTrace(size_t maxSize)
242 : {
243 143 : return PITrace(new Trace_Dummy(maxSize));
244 : }
245 :
246 0 : PITrace CreateTrace(size_t maxSize)
247 : {
248 0 : return PITrace(new Trace(maxSize));
249 : }
|