Skip to content

Commit ec870c4

Browse files
committed
Add Log system + logfile
Add Exception to unpacker Add MemAlloc
1 parent c0807ef commit ec870c4

13 files changed

Lines changed: 241 additions & 20 deletions

File tree

cmake/Packer.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ set( SRC_FOLDERS
99
${CMAKE_CURRENT_SOURCE_DIR}/sources/
1010
${CMAKE_CURRENT_SOURCE_DIR}/sources/file/
1111
${CMAKE_CURRENT_SOURCE_DIR}/sources/pack/
12+
${CMAKE_CURRENT_SOURCE_DIR}/sources/Log/
1213
)
1314
## INCLUDE FOLDERS
1415
set( INC_FOLDERS
1516
${CMAKE_CURRENT_SOURCE_DIR}/includes/
1617
${CMAKE_CURRENT_SOURCE_DIR}/includes/file/
1718
${CMAKE_CURRENT_SOURCE_DIR}/includes/pack/
19+
${CMAKE_CURRENT_SOURCE_DIR}/includes/Log/
1820
)
1921

2022
## GET SOURCES

cmake/UnPacker.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ set( SRC_FOLDERS
99
${CMAKE_CURRENT_SOURCE_DIR}/sources/
1010
${CMAKE_CURRENT_SOURCE_DIR}/sources/file/
1111
${CMAKE_CURRENT_SOURCE_DIR}/sources/unpack/
12+
${CMAKE_CURRENT_SOURCE_DIR}/sources/exception/
13+
${CMAKE_CURRENT_SOURCE_DIR}/sources/utils/
14+
${CMAKE_CURRENT_SOURCE_DIR}/sources/log/
1215
)
1316
## INCLUDE FOLDERS
1417
set( INC_FOLDERS
1518
${CMAKE_CURRENT_SOURCE_DIR}/includes/
1619
${CMAKE_CURRENT_SOURCE_DIR}/includes/file/
1720
${CMAKE_CURRENT_SOURCE_DIR}/includes/unpack/
21+
${CMAKE_CURRENT_SOURCE_DIR}/includes/exception/
22+
${CMAKE_CURRENT_SOURCE_DIR}/includes/log/
1823
)
1924

2025
## GET SOURCES
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
** ShipWreck Engine , 2023
3+
** FileException.hpp
4+
*/
5+
6+
#ifndef SW_PACKER_FILEEXCEPTION_HPP
7+
#define SW_PACKER_FILEEXCEPTION_HPP
8+
9+
#include <exception>
10+
#include <string>
11+
12+
namespace sw
13+
{
14+
class FileException : public std::exception
15+
{
16+
private:
17+
std::string m_reason;
18+
public:
19+
explicit FileException(std::string&& reason);
20+
[[nodiscard]] const char *what() const override;
21+
};
22+
}
23+
24+
#endif //SW_PACKER_FILEEXCEPTION_HPP
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
** ShipWreck Engine , 2023
3+
** MemoryException.hpp
4+
*/
5+
6+
#ifndef SW_PACKER_MEMORYEXCEPTION_HPP
7+
#define SW_PACKER_MEMORYEXCEPTION_HPP
8+
9+
#include <exception>
10+
#include <string>
11+
12+
namespace sw {
13+
14+
class MemoryException : public std::exception
15+
{
16+
private:
17+
std::string m_message;
18+
public:
19+
explicit MemoryException(std::string message);
20+
[[nodiscard]]const char * what() const override;
21+
};
22+
23+
} // sw
24+
25+
#endif //SW_PACKER_MEMORYEXCEPTION_HPP

includes/file/File.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
#include "Chunk.hpp"
1414

15+
#define FILE_TYPE "SWFP"
16+
#define FILE_VERSION 100
17+
1518
namespace sw {
1619

1720
/// \brief Define the type of the file

includes/log/Log.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
** ShipWreck Engine , 2023
3+
** Log.hpp
4+
*/
5+
6+
#ifndef SW_PACKER_LOG_HPP
7+
#define SW_PACKER_LOG_HPP
8+
9+
#include <vector>
10+
#include <string>
11+
#include <map>
12+
13+
namespace sw {
14+
15+
class Log {
16+
public:
17+
enum Level {
18+
NONE,
19+
DEBUG,
20+
WARNING,
21+
ERROR,
22+
};
23+
static void AddLog(std::string&& message, Level logLevel = DEBUG);
24+
static void CloseLog();
25+
26+
private:
27+
static std::vector<std::string> m_logs;
28+
static std::map<Level, std::string> m_levels;
29+
};
30+
31+
} // sw
32+
33+
#endif //SW_PACKER_LOG_HPP

includes/utils.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
** ShipWreck Engine , 2023
3+
** utils.hpp
4+
*/
5+
6+
#ifndef SW_PACKER_UTILS_HPP
7+
#define SW_PACKER_UTILS_HPP
8+
9+
namespace sw {
10+
11+
void *MemAlloc(size_t size);
12+
13+
}
14+
15+
#endif //SW_PACKER_UTILS_HPP
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
** ShipWreck Engine , 2023
3+
** FileException.cpp
4+
*/
5+
6+
#include "FileException.hpp"
7+
8+
sw::FileException::FileException(std::string&& reason) :
9+
m_reason(reason)
10+
{}
11+
12+
const char *sw::FileException::what() const
13+
{
14+
return m_reason.c_str();
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
** ShipWreck Engine , 2023
3+
** MemoryException.cpp
4+
*/
5+
6+
#include "MemoryException.hpp"
7+
8+
sw::MemoryException::MemoryException(std::string message) :
9+
m_message(message)
10+
{}
11+
12+
const char *sw::MemoryException::what() const
13+
{
14+
return m_message.c_str();
15+
}

sources/log/Log.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
** ShipWreck Engine , 2023
3+
** Log.cpp
4+
*/
5+
6+
#include <fstream>
7+
#include <iomanip>
8+
#include <sstream>
9+
#include <iostream>
10+
11+
#include "Log.hpp"
12+
13+
std::vector<std::string> sw::Log::m_logs;
14+
std::map<sw::Log::Level, std::string> sw::Log::m_levels = {
15+
{NONE, ""},
16+
{DEBUG, "Debug"},
17+
{WARNING, "Warning"},
18+
{ERROR, "Error"},
19+
};
20+
21+
void sw::Log::AddLog(std::string&& message, Level logLevel)
22+
{
23+
auto t = std::time(nullptr);
24+
auto tm = *std::localtime(&t);
25+
std::ostringstream ossLog;
26+
27+
ossLog << "[" << std::put_time(&tm, "%H:%M:%S") << "] " << m_levels[logLevel] << ": " << message;
28+
m_logs.emplace_back(ossLog.str());
29+
}
30+
31+
void sw::Log::CloseLog()
32+
{
33+
if (m_logs.empty())
34+
return;
35+
auto t = std::time(nullptr);
36+
auto tm = *std::localtime(&t);
37+
std::ostringstream oss;
38+
oss << std::put_time(&tm, "%d-%m-%Y_%H-%M-%S");
39+
std::fstream outFile("./SWPacker_" + oss.str() + ".txt", std::ios::out);
40+
41+
if (!outFile.is_open()) {
42+
std::cerr << "ERROR: Unable to create log file! [" << "SWPacker_" + oss.str() << "]" << std::endl;
43+
return;
44+
}
45+
for (const auto & m_log : m_logs) {
46+
outFile.write(m_log.c_str(), m_log.size());
47+
outFile.write("\n", 1);
48+
}
49+
outFile.close();
50+
}

0 commit comments

Comments
 (0)