Scope
ScopeLogger_p.h
1 #pragma once
2 
3 #include "ScopeLogger.h"
4 #include "helpers/Active.h"
5 #include "parameters/Scope.h"
6 #include "ScopeDatatypes.h"
7 #include "gui/LogFrame.h"
8 
9 namespace scope {
10 
13 
14 protected:
17 
19  std::wstring file_path;
20 
23 
25  std::wofstream userlogfile;
26 
28  log_message_type file_messages;
29 
31  log_message_type console_message;
32 
34  log_message_type logframe_message;
35 
38 
40  std::wstring logbooktext;
41 
43  std::list<std::wstring> logbookentries;
44 
45 protected:
47  Impl(const Impl& i);
48 
50  Impl operator=(const Impl& i);
51 
53  ControllerReturnStatus WriteToConsole(StopCondition* const sc, const std::wstring message) {
54  DBOUT(L"" << message.c_str());
55  return finished;
56  }
57 
59  ControllerReturnStatus WriteToLogbox(StopCondition* const sc, const std::wstring message, const log_message_type msgtype) {
60  logbookentries.push_back(message+L"\r\n");
61 
62  // Get current logbook stuff from log window
63  if ( logframe != nullptr )
64  logbooktext = logframe->GetLogText();
65 
66  // Add linebreak if needed
67  if ( logbooktext.length() > 2 )
68  if ( logbooktext.substr(logbooktext.length()-2, 2) != L"\r\n" )
69  logbooktext += L"\r\n";
70 
71  // Add message
72  logbooktext += message+L"\r\n";
73 
74  // Flush to disk and log window
75  FlushLogbox(sc);
76 
77  return finished;
78  }
79 
82  ControllerReturnStatus FlushLogbox(StopCondition* const sc) {
83  if ( logframe != nullptr )
84  logframe->ReplaceLogText(logbooktext);
85  if ( filepath_set ) {
86  if ( !userlogfile.is_open() )
87  userlogfile.open(file_path, std::ios_base::out | std::ios_base::trunc); // Trunc: overwrite
88  userlogfile << logbooktext;
89  userlogfile.close();
90  }
91  return ControllerReturnStatus::finished;
92  }
93 
94 public:
96  Impl(const log_message_type& filem = log_all, const log_message_type& consolem = log_all, const log_message_type& logboxm = log_all)
97  : file_path(L"")
98  , filepath_set(false)
99  , file_messages(filem)
100  , console_message(consolem)
101  , logframe_message(logboxm)
102  , logframe(nullptr)
103  , logbookentries(1, L"")
104  , logbooktext(L"") {
105  Log(L"Logbook started", log_info);
106  }
107 
108  ~Impl() {
109  if ( userlogfile.is_open() )
110  userlogfile.close();
111  }
112 
114  void SetFilepath(const std::wstring& _filepath = L"C:\\ScopeData") {
115  if ( !filepath_set ) {
116  file_path = _filepath + L"\\log_" + GetCurrentDateString() + L"_" + GetCurrentTimeString(true) + L".txt";
117 
118  userlogfile.open(file_path);
119  if ( userlogfile.bad() )
120  throw std::invalid_argument("Unable to open user log file\n");
121  userlogfile.close(); // We only wanted it to be cleared/old overwritten, do not keep it open
122 
123  filepath_set = true;
124 
125  Log(L"Logfile started", log_info);
126  }
127  }
128 
130  void Log(const std::wstring& message, const log_message_type& msgtype) {
131  SYSTEMTIME st;
132  GetLocalTime(&st);
133  std::wostringstream logmsg;
134  logmsg << st.wHour << L":" << std::setfill(L'0') << std::setw(2) << st.wMinute << L"::" << st.wSecond << L" - ";
135  logmsg << message;
136  active.Send(std::bind(&Impl::WriteToConsole, this, std::placeholders::_1, logmsg.str()));
137  // Console is flushed to disk
138  active.Send(std::bind(&Impl::WriteToLogbox, this, std::placeholders::_1, logmsg.str(), msgtype));
139  }
140 
142  void AttachLogFrame(gui::CLogFrame* const _logframe) {
143  logframe = _logframe;
144  active.Send(std::bind(&Impl::FlushLogbox, this, std::placeholders::_1));
145  }
146 
149  return this->logframe->m_hWnd;
150  }
151 
153  bool HasLogFrame() const {
154  return logframe!=nullptr;
155  }
156 
159  if ( logframe != nullptr )
160  logbooktext = logframe->GetLogText();
161  active.Send(std::bind(&Impl::FlushLogbox, this, std::placeholders::_1));
162  }
163 
165  void DetachLogFrame() {
166  logbooktext = logframe->GetLogText();
167  logframe = nullptr;
168  }
169 
173  void Shutdown() {
174  active.Quit();
175  }
176 };
177 
178 }
179 
Impl(const log_message_type &filem=log_all, const log_message_type &consolem=log_all, const log_message_type &logboxm=log_all)
Create logfiles.
Definition: ScopeLogger_p.h:96
std::wofstream userlogfile
File with user comments and logging of performed scans etc.
Definition: ScopeLogger_p.h:25
Active< ControllerReturnStatus > active
Active object for running the loggings.
Definition: ScopeLogger_p.h:16
Thread-safe lock-free bool to signal a requested stop to the worker function currently executed in th...
Definition: helpers.h:87
void Quit()
Send lambda with done=true to the worker, join the worker thread, and clear the packaged task queue...
Definition: Active.h:80
void GetUserLoggings()
Gets the text in the logbook window and calls FlushLogbox.
ControllerReturnStatus WriteToLogbox(StopCondition *const sc, const std::wstring message, const log_message_type msgtype)
Writes message to the logbook window.
Definition: ScopeLogger_p.h:59
std::wstring logbooktext
the complete log text as a string
Definition: ScopeLogger_p.h:40
Impl(const Impl &i)
disable copy
ControllerReturnStatus FlushLogbox(StopCondition *const sc)
Updates the logbook window , writes complete logbook to disk, overwrites the old logbook on disk...
Definition: ScopeLogger_p.h:82
void SetFilepath(const std::wstring &_filepath=L"C:\\ScopeData")
Sets the filepath and creates logfile.
Implementation class of the ScopeLogger.
Definition: ScopeLogger_p.h:12
gui::CLogFrame * logframe
pointer to the log window frame
Definition: ScopeLogger_p.h:37
std::future< RT > Send(const Command &_cmd)
Sends a worker function/packaged task to the queue to be executed in the Active's thread...
Definition: Active.h:59
log_message_type console_message
which message types to print out on debug console
Definition: ScopeLogger_p.h:31
std::wstring GetCurrentDateString()
Definition: helpers.cpp:4
In here all declarations for all kinds of datatypes Scope needs.
Frame window class for the log.
Definition: LogFrame.h:10
void AttachLogFrame(gui::CLogFrame *const _logframe)
Attaches a CLogFrame as the logbook window.
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
Impl operator=(const Impl &i)
disable assignment
ControllerReturnStatus WriteToConsole(StopCondition *const sc, const std::wstring message)
Writes message to debug console.
Definition: ScopeLogger_p.h:53
void DetachLogFrame()
Detaches a CLogFrame.
void Shutdown()
Shutdown the Active, called from Run in scope.cpp.
log_message_type file_messages
which message types to save to disk
Definition: ScopeLogger_p.h:28
bool filepath_set
true if file path set and files opened
Definition: ScopeLogger_p.h:22
std::wstring file_path
path string for log file
Definition: ScopeLogger_p.h:19
log_message_type logframe_message
which message types to print in log frame
Definition: ScopeLogger_p.h:34
void Log(const std::wstring &message, const log_message_type &msgtype)
Logs a message.
std::list< std::wstring > logbookentries
a vector of all log entries done by scope
Definition: ScopeLogger_p.h:43
HWND GetLogFrameWindow()
Saves log frame parameters into Window (for recreating windows on startup)
std::wstring GetCurrentTimeString(const bool &_filenamecompatible)
Definition: helpers.cpp:12