TDME2  1.9.200
Console.cpp
Go to the documentation of this file.
1 #include <memory>
2 #include <filesystem>
3 #include <fstream>
4 #include <iostream>
5 #include <string>
6 
7 #include <tdme/tdme.h>
10 #include <tdme/utilities/Console.h>
11 #include <tdme/utilities/Time.h>
12 
13 using namespace std;
14 
19 
20 Mutex Console::mutex("console");
21 bool Console::newline = false;
22 vector<string> Console::messages;
23 Console::Logger* Console::logger = nullptr;
24 unique_ptr<Console::LogWriterThread> Console::logWriterThread = make_unique<Console::LogWriterThread>();
25 
26 Console::LogWriterThread::LogWriterThread(): Thread("console-logwriter-thread") {
27  ofstream ofs(std::filesystem::u8path("console.log"), ofstream::trunc);
28  ofs.close();
29  start();
30 }
31 
33 }
34 
36  while (isStopRequested() == false) {
38  if (Console::messages.size() > HISTORY_LINECOUNT) flush();
40  Thread::sleep(1000);
41  }
43  if (Console::messages.size() > 0) flush();
45 }
46 
48  ofstream ofs(std::filesystem::u8path("console.log"), ofstream::app);
49  for (const auto& message: Console::messages) {
50  ofs << message << endl;
51  }
52  ofs.close();
53  Console::messages.clear();
54 }
55 
57  if (Console::logger == logger) return;
58  if (Console::logger != nullptr) delete Console::logger;
60 }
61 
62 void Console::println(const string_view& str)
63 {
64  mutex.lock();
65  //
66  if (messages.empty() == true || newline == true) messages.push_back(string());
67  messages[messages.size() - 1]+= str;
68  if (messages.size() == HISTORY_LINECOUNT) messages.erase(messages.begin());
69  newline = true;
70  //
71  if (logger != nullptr) logger->println(str);
72  cout << str << endl;
73  //
74  mutex.unlock();
75 }
76 
77 void Console::print(const string_view& str)
78 {
79  mutex.lock();
80  //
81  if (messages.empty() == true || newline == true) messages.push_back(string());
82  messages[messages.size() - 1]+= str;
83  if (messages.size() == HISTORY_LINECOUNT) messages.erase(messages.begin());
84  newline = false;
85  //
86  if (logger != nullptr) logger->print(str);
87  cout << str;
88  //
89  mutex.unlock();
90 }
91 
93 {
94  mutex.lock();
95  //
96  messages.push_back(string());
97  if (messages.size() == HISTORY_LINECOUNT) messages.erase(messages.begin());
98  newline = true;
99  //
100  if (logger != nullptr) logger->println();
101  cout << endl;
102  //
103  mutex.unlock();
104 }
105 
107  // shut down logwriter thread
108  Console::logWriterThread->stop();
109  Console::logWriterThread->join();
110  Console::logWriterThread = nullptr;
111 }
Mutex implementation.
Definition: Mutex.h:19
void unlock()
Unlocks this mutex.
Definition: Mutex.h:54
void lock()
Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
Definition: Mutex.h:47
Base class for threads.
Definition: Thread.h:20
virtual void start()
Starts this objects thread.
Definition: Thread.h:62
void run()
Abstract run() method, should be implemented by subclassed class, will be called after spawn by start...
Definition: Console.cpp:35
Console class.
Definition: Console.h:29
static void println()
Print new line to console.
Definition: Console.cpp:92
static void setLogger(Logger *logger)
Set logger.
Definition: Console.cpp:56
static STATIC_DLL_IMPEXT bool newline
Definition: Console.h:85
static void shutdown()
Shutdown console logging and especially writing log to file.
Definition: Console.cpp:106
static STATIC_DLL_IMPEXT vector< string > messages
Definition: Console.h:86
static STATIC_DLL_IMPEXT Logger * logger
Definition: Console.h:87
static void print(const string_view &str)
Print given string.
Definition: Console.cpp:77
static STATIC_DLL_IMPEXT unique_ptr< LogWriterThread > logWriterThread
Definition: Console.h:88
static constexpr int HISTORY_LINECOUNT
Definition: Console.h:32
static STATIC_DLL_IMPEXT Mutex mutex
Definition: Console.h:84
Time utility class.
Definition: Time.h:20
virtual void println(const string_view &str)=0
virtual void print(const string_view &str)=0