Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThreadedLogging.h
Go to the documentation of this file.
1 // ThreadedLogging.h
2 // Provides an interface for logging messages to disk from another thread.
3 // Phasor uses this to reduce lag caused by logging messages (disk io is
4 // really slow).
5 #pragma once
6 
7 #include "Logging.h"
8 #include "PhasorThread.h"
9 
10 class CLogThreadEvent;
11 
12 #define DEFAULT_SAVE_DELAY 1000
13 
15 {
16 private:
17  // cs locks lines, loggingStreamCS locks the stream
18  CRITICAL_SECTION cs, loggingStreamCS;
19  DWORD id; // timer id
20  PhasorThread& thread;
21  std::shared_ptr<CLogThreadEvent> threadEvent;
22  typedef std::list<std::wstring> lines_t;
23  std::unique_ptr<lines_t> lines;
24  bool bDoTimestamp;
25  DWORD dwDelay;
26 
27  void Initialize(DWORD dwDelay);
28  // Write the lines in data to file, then cleanup data.
29  void LogLinesAndCleanup(std::unique_ptr<lines_t> data);
30  // Reallocate lines.
31  void AllocateLines();
32 
33 protected:
34  // CLoggingStream
35  bool Write(const std::wstring& str) override;
36 
37 public:
38  CThreadedLogging(const std::wstring& dir, const std::wstring& file,
39  const std::wstring& movedir,
40  PhasorThread& thread,
41  DWORD dwDelay=DEFAULT_SAVE_DELAY);
42  CThreadedLogging(const CLoggingStream& stream, PhasorThread& thread,
43  DWORD dwDelay=DEFAULT_SAVE_DELAY);
45 
46  // COutStream
47  std::unique_ptr<COutStream> clone() const override;
48 
49  // CLoggingStream isn't threadsafe, so we're responsible for thread safety.
50  void SetMoveSize(DWORD kbSize) override;
51  void SetMoveDirectory(const std::wstring& move_to) override;
52  void SetOutFile(const std::wstring& directory,const std::wstring& fileName) override;
53  void SetOutFile(const std::wstring& fileName) override; // use cur dir
54  void EnableTimestamp(bool state) override;
55 
56  // COutStream isn't thread safe either.
57  void AppendData(const std::wstring& str) override;
58  void AppendData(wchar_t c) override;
59  void Reserve(size_t size) override;
60 
61  friend class CLogThreadEvent;
62 };
63 
64 // Helper class which is invoked by PhasorThread when data can be saved.
66 {
67 private:
68  friend class CThreadLogging;
69  CThreadedLogging& owner;
70 
71  CLogThreadEvent(CThreadedLogging& owner, DWORD dwDelay);
72 
73 public:
74  // PhasorThreadEvent
75  void OnEventAux(PhasorThread& thread) override;
76 
77  friend class CThreadedLogging;
78 };