lesson 1 #1
|
@ -3,16 +3,75 @@
|
|||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
namespace MyTools {
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void OpenLogFile(const std::string& FN);
|
||||
namespace MyTools
|
||||
{
|
||||
|
||||
void CloseLogFile();
|
||||
// void OpenLogFile(const std::string &FN);
|
||||
// void CloseLogFile();
|
||||
// void WriteToLog(const std::string &str);
|
||||
// void WriteToLog(const std::string &str, int n);
|
||||
// void WriteToLog(const std::string &str, double d);
|
||||
|
||||
void WriteToLog(const std::string& str);
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
virtual void OpenLogFile(const std::string &FN) = 0;
|
||||
virtual void CloseLogFile() = 0;
|
||||
virtual void WriteToLog(const std::string &str) = 0;
|
||||
virtual void WriteToLog(const std::string &str, int n) = 0;
|
||||
virtual void WriteToLog(const std::string &str, double d) = 0;
|
||||
virtual ~Logger() {}
|
||||
};
|
||||
|
||||
void WriteToLog(const std::string& str, int n);
|
||||
class FileLoggerSingleton: public Logger
|
||||
{
|
||||
public:
|
||||
static FileLoggerSingleton& getInstance()
|
||||
{
|
||||
static FileLoggerSingleton _instance;
|
||||
return _instance;
|
||||
}
|
||||
|
||||
void WriteToLog(const std::string& str, double d);
|
||||
void OpenLogFile(const std::string &FN) override;
|
||||
void CloseLogFile() override;
|
||||
void WriteToLog(const std::string &str) override;
|
||||
void WriteToLog(const std::string &str, int n) override;
|
||||
void WriteToLog(const std::string &str, double d) override;
|
||||
private:
|
||||
FileLoggerSingleton() {}
|
||||
FileLoggerSingleton(const FileLoggerSingleton&) = delete;
|
||||
FileLoggerSingleton& operator=(const FileLoggerSingleton&) = delete;
|
||||
FileLoggerSingleton& operator=(FileLoggerSingleton&&) = delete;
|
||||
};
|
||||
|
||||
}; // namespace MyTools
|
||||
class LoggerSingleton: public Logger
|
||||
{
|
||||
public:
|
||||
static LoggerSingleton& getInstance()
|
||||
{
|
||||
static LoggerSingleton _instance;
|
||||
return _instance;
|
||||
}
|
||||
|
||||
void OpenLogFile(const std::string &FN) override;
|
||||
void CloseLogFile() override;
|
||||
void WriteToLog(const std::string &str) override;
|
||||
void WriteToLog(const std::string &str, int n) override;
|
||||
void WriteToLog(const std::string &str, double d) override;
|
||||
~LoggerSingleton();
|
||||
|
||||
private:
|
||||
LoggerSingleton() {}
|
||||
LoggerSingleton(const LoggerSingleton&) = delete;
|
||||
LoggerSingleton& operator=(const LoggerSingleton&) = delete;
|
||||
LoggerSingleton& operator=(LoggerSingleton&&) = delete;
|
||||
FileLoggerSingleton& LoadLoggerSingletone();
|
||||
|
||||
FileLoggerSingleton *_logger = NULL;
|
||||
std::vector<double> times;
|
||||
};
|
||||
|
||||
} // namespace MyTools
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Timer.h
|
||||
*
|
||||
* Created on: 21 дек. 2021 г.
|
||||
* Author: alexander
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
static Timer& getInstance()
|
||||
{
|
||||
static Timer _instance;
|
||||
return _instance;
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
m_beg = clock_t::now();
|
||||
}
|
||||
|
||||
double end() const
|
||||
{
|
||||
return elapsed() * 1000;
|
||||
}
|
||||
|
||||
private:
|
||||
using clock_t = std::chrono::high_resolution_clock;
|
||||
using second_t = std::chrono::duration<double, std::ratio<1> >;
|
||||
|
||||
std::chrono::time_point<clock_t> m_beg;
|
||||
|
||||
double elapsed() const
|
||||
{
|
||||
return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
|
||||
}
|
||||
|
||||
Timer() {}
|
||||
Timer(const Timer&) = delete;
|
||||
Timer& operator=(const Timer&) = delete;
|
||||
Timer& operator=(Timer&&) = delete;
|
||||
};
|
320028
SBomber/log.txt
320028
SBomber/log.txt
File diff suppressed because it is too large
Load Diff
|
@ -32,7 +32,7 @@ int _kbhit()
|
|||
|
||||
int main(void)
|
||||
{
|
||||
MyTools::OpenLogFile("log.txt");
|
||||
MyTools::LoggerSingleton::getInstance().OpenLogFile("log.txt");
|
||||
|
||||
SBomber game;
|
||||
|
||||
|
@ -55,7 +55,7 @@ int main(void)
|
|||
|
||||
} while (!game.GetExitFlag());
|
||||
|
||||
MyTools::CloseLogFile();
|
||||
MyTools::LoggerSingleton::getInstance().CloseLogFile();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -9,42 +9,110 @@
|
|||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <numeric>
|
||||
#include "Timer.h"
|
||||
|
||||
namespace MyTools {
|
||||
namespace MyTools
|
||||
{
|
||||
|
||||
std::ofstream logOut;
|
||||
std::ofstream logOut;
|
||||
|
||||
void OpenLogFile(const std::string &FN) { logOut.open(FN, std::ios_base::out); }
|
||||
void FileLoggerSingleton::OpenLogFile(const std::string &FN)
|
||||
{
|
||||
logOut.open(FN, std::ios_base::out);
|
||||
}
|
||||
|
||||
void CloseLogFile() {
|
||||
if (logOut.is_open()) {
|
||||
logOut.close();
|
||||
}
|
||||
}
|
||||
void FileLoggerSingleton::CloseLogFile()
|
||||
{
|
||||
if (logOut.is_open())
|
||||
{
|
||||
logOut.close();
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetCurDateTime() {
|
||||
auto cur = std::chrono::system_clock::now();
|
||||
time_t time = std::chrono::system_clock::to_time_t(cur);
|
||||
char* buf = ctime(&time);
|
||||
return std::string(buf);
|
||||
}
|
||||
std::string GetCurDateTime()
|
||||
{
|
||||
auto cur = std::chrono::system_clock::now();
|
||||
time_t time = std::chrono::system_clock::to_time_t(cur);
|
||||
char *buf = ctime(&time);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
void WriteToLog(const std::string &str) {
|
||||
if (logOut.is_open()) {
|
||||
logOut << GetCurDateTime() << " - " << str << std::endl;
|
||||
}
|
||||
}
|
||||
void FileLoggerSingleton::WriteToLog(const std::string &str)
|
||||
{
|
||||
if (logOut.is_open())
|
||||
{
|
||||
logOut << GetCurDateTime() << " - " << str << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void WriteToLog(const std::string &str, int n) {
|
||||
if (logOut.is_open()) {
|
||||
logOut << GetCurDateTime() << " - " << str << n << std::endl;
|
||||
}
|
||||
}
|
||||
void FileLoggerSingleton::WriteToLog(const std::string &str, int n)
|
||||
{
|
||||
if (logOut.is_open())
|
||||
{
|
||||
logOut << GetCurDateTime() << " - " << str << n << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void WriteToLog(const std::string &str, double d) {
|
||||
if (logOut.is_open()) {
|
||||
logOut << GetCurDateTime() << " - " << str << d << std::endl;
|
||||
}
|
||||
}
|
||||
void FileLoggerSingleton::WriteToLog(const std::string &str, double d)
|
||||
{
|
||||
if (logOut.is_open())
|
||||
{
|
||||
logOut << GetCurDateTime() << " - " << str << d << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void LoggerSingleton::OpenLogFile(const std::string &FN)
|
||||
{
|
||||
Timer::getInstance().start();
|
||||
LoadLoggerSingletone().OpenLogFile(FN);
|
||||
times.push_back(Timer::getInstance().end());
|
||||
}
|
||||
|
||||
void LoggerSingleton::CloseLogFile()
|
||||
{
|
||||
Timer::getInstance().start();
|
||||
LoadLoggerSingletone().CloseLogFile();
|
||||
times.push_back(Timer::getInstance().end());
|
||||
}
|
||||
|
||||
void LoggerSingleton::WriteToLog(const std::string &str)
|
||||
{
|
||||
Timer::getInstance().start();
|
||||
LoadLoggerSingletone().WriteToLog(str);
|
||||
times.push_back(Timer::getInstance().end());
|
||||
}
|
||||
|
||||
void LoggerSingleton::WriteToLog(const std::string &str, int n)
|
||||
{
|
||||
Timer::getInstance().start();
|
||||
LoadLoggerSingletone().WriteToLog(str, n);
|
||||
times.push_back(Timer::getInstance().end());
|
||||
}
|
||||
|
||||
void LoggerSingleton::WriteToLog(const std::string &str, double d)
|
||||
{
|
||||
Timer::getInstance().start();
|
||||
LoadLoggerSingletone().WriteToLog(str, d);
|
||||
times.push_back(Timer::getInstance().end());
|
||||
}
|
||||
|
||||
LoggerSingleton::~LoggerSingleton()
|
||||
{
|
||||
if (times.size())
|
||||
{
|
||||
std::cout << "Среднее время выполнения операций: " << std::accumulate(times.begin(), times.end(), 0.0) / times.size() << " секунд" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
FileLoggerSingleton& LoggerSingleton::LoadLoggerSingletone()
|
||||
{
|
||||
if (!_logger)
|
||||
{
|
||||
_logger = &FileLoggerSingleton::getInstance();
|
||||
}
|
||||
|
||||
return *_logger;
|
||||
}
|
||||
|
||||
} // namespace MyTools
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
SBomber::SBomber()
|
||||
: exitFlag(false), startTime(0), finishTime(0), deltaTime(0), passedTime(0),
|
||||
fps(0), bombsNumber(10), score(0) {
|
||||
MyTools::WriteToLog(std::string(__func__) + " was invoked");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
|
||||
|
||||
Plane* p = new Plane;
|
||||
p->SetDirection(1, 0.1);
|
||||
|
@ -79,7 +79,7 @@ SBomber::~SBomber() {
|
|||
}
|
||||
|
||||
void SBomber::MoveObjects() {
|
||||
MyTools::WriteToLog(std::string(__func__) + " was invoked");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
|
||||
|
||||
for (size_t i = 0; i < vecDynamicObj.size(); i++) {
|
||||
if (vecDynamicObj[i] != nullptr) {
|
||||
|
@ -89,7 +89,7 @@ void SBomber::MoveObjects() {
|
|||
};
|
||||
|
||||
void SBomber::CheckObjects() {
|
||||
MyTools::WriteToLog(std::string(__func__) + " was invoked");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
|
||||
|
||||
CheckPlaneAndLevelGUI();
|
||||
CheckBombsAndGround();
|
||||
|
@ -225,7 +225,7 @@ void SBomber::ProcessKBHit() {
|
|||
c = getchar();
|
||||
}
|
||||
|
||||
MyTools::WriteToLog(std::string(__func__) + " was invoked. key = ", c);
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked. key = ", c);
|
||||
|
||||
switch (c) {
|
||||
|
||||
|
@ -255,7 +255,7 @@ void SBomber::ProcessKBHit() {
|
|||
}
|
||||
|
||||
void SBomber::DrawFrame() {
|
||||
MyTools::WriteToLog(std::string(__func__) + " was invoked");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
|
||||
|
||||
for (size_t i = 0; i < vecDynamicObj.size(); i++) {
|
||||
if (vecDynamicObj[i] != nullptr) {
|
||||
|
@ -276,7 +276,7 @@ void SBomber::DrawFrame() {
|
|||
}
|
||||
|
||||
void SBomber::TimeStart() {
|
||||
MyTools::WriteToLog(std::string(__func__) + " was invoked");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
|
||||
startTime = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::high_resolution_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
@ -287,12 +287,12 @@ void SBomber::TimeFinish() {
|
|||
deltaTime = uint16_t(finishTime - startTime);
|
||||
passedTime += deltaTime;
|
||||
|
||||
MyTools::WriteToLog(std::string(__func__) + " deltaTime = ", (int)deltaTime);
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " deltaTime = ", (int)deltaTime);
|
||||
}
|
||||
|
||||
void SBomber::DropBomb() {
|
||||
if (bombsNumber > 0) {
|
||||
MyTools::WriteToLog(std::string(__func__) + " was invoked");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
|
||||
|
||||
Plane* pPlane = FindPlane();
|
||||
double x = pPlane->GetX() + 4;
|
||||
|
|
|
@ -33,15 +33,15 @@ IScreen& getInternalInstance() {
|
|||
class ScreenSingletonProxy : public IScreen {
|
||||
public:
|
||||
virtual void ClrScr() override {
|
||||
MyTools::WriteToLog("ClrScr invoke begin");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog("ClrScr invoke begin");
|
||||
getInternalInstance().ClrScr();
|
||||
MyTools::WriteToLog("ClrScr invoke end");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog("ClrScr invoke end");
|
||||
}
|
||||
virtual void GotoXY(double x, double y) override {
|
||||
|
||||
MyTools::WriteToLog("GotoXY invoke begin");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog("GotoXY invoke begin");
|
||||
getInternalInstance().GotoXY(x, y);
|
||||
MyTools::WriteToLog("GotoXY invoke end");
|
||||
MyTools::LoggerSingleton::getInstance().WriteToLog("GotoXY invoke end");
|
||||
}
|
||||
virtual uint16_t GetMaxX() override {
|
||||
return getInternalInstance().GetMaxX();
|
||||
|
|
Loading…
Reference in New Issue