Skip to content

Commit 89288e3

Browse files
committed
Refactor the project
1 parent 3a0e501 commit 89288e3

19 files changed

+597
-445
lines changed

CMakeLists.txt

+11-9
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ include_directories(src include)
88

99
set(
1010
LIBRARY_SOURCES
11-
src/Logger.cpp
1211
include/Logger.hpp
12+
src/Logger.cpp
13+
include/String.hpp
14+
src/String.cpp
15+
include/Convert.hpp
16+
src/Convert.cpp
1317
include/Time.hpp
1418
src/Time.cpp
1519
include/Format.hpp
1620
src/Format.cpp
17-
include/Log.hpp
18-
src/Log.cpp
19-
include/Convert.hpp
20-
src/Convert.cpp
21-
include/StringOps.hpp
22-
src/StringOps.cpp
21+
include/Console.hpp
22+
src/Console.cpp
2323
)
2424

25-
add_library(logger STATIC ${LIBRARY_SOURCES})
26-
add_executable(example ${LIBRARY_SOURCES} src/example/example.cpp)
25+
add_library(ssbl STATIC ${LIBRARY_SOURCES})
26+
add_executable(example src/Examples/Example.cpp)
27+
28+
target_link_libraries(example ssbl)

README.md

+12-14
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,20 @@ using namespace SSBL;
2121

2222
Logger logger;
2323

24-
// Configuration (color and timestamp)
25-
logger.SetConfig(Config::All);
26-
27-
// Timestamp formatting
28-
logger.SetTimestampFormat("%h:%m:%s");
24+
// Configuration
25+
logger
26+
.UseColor(true)
27+
.ShowTimestamp(true)
28+
.SetTimeFormat("%Y-%m-%d %H:%M:%S")
29+
.SetOutputFile("Example.log");
2930

3031
// Log levels
31-
logger.Log() << "Hello, Info!\n";
32-
logger.LogWarn() << "Hello, Warning!\n";
33-
logger.LogError() << "Hello, Error!\n";
34-
logger.LogFatal() << "Hello, Fatal!\n";
32+
logger.Log() << "Hello, Info!";
33+
logger.LogWarn() << "Hello, Warning!";
34+
logger.LogError() << "Hello, Error!";
35+
logger.LogFatal() << "Hello, Fatal!";
3536

3637
// Formatted output
37-
logger.Log() << "My name is {}, and I am {} years old." << "John" << 35 << '\n';
38-
logger.Log() << "{2}, {1}!" << "World" << "Hello" << '\n';
39-
40-
// File output
41-
logger.LogToFile("Example.log") << "Hello, File!\n";
38+
logger.Log() << "My name is {}, and I am {} years old." << "John" << 35;
39+
logger.Log() << "{2}, {1}!" << "World" << "Hello";
4240
```

include/Console.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include "String.hpp"
4+
#include <ostream>
5+
6+
#if defined _WIN32
7+
8+
#include <windows.h>
9+
10+
#endif
11+
12+
namespace SSBL {
13+
class Logger;
14+
15+
#if defined _WIN32
16+
17+
enum class Color {
18+
DEFAULT = 15,
19+
RED = 12,
20+
YELLOW = 14,
21+
BLUE = 9
22+
};
23+
24+
#else
25+
26+
enum class Color {
27+
DEFAULT = 0,
28+
RED = 31,
29+
YELLOW = 33,
30+
BLUE = 34
31+
};
32+
33+
#endif
34+
35+
String ColorToAnsiEscapeCode(Color color, bool bold = false);
36+
void SetColor(Color color, std::ostream &output, bool bold = false);
37+
void ResetColor(std::ostream &output);
38+
}

include/Convert.hpp

-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
#pragma once
22

33
#include <optional>
4-
#include <sstream>
5-
#include <string>
64

75
namespace SSBL {
8-
template<typename T>
9-
std::string GenericToString(const T &generic) {
10-
std::stringstream ss;
11-
ss << generic;
12-
return ss.str();
13-
}
14-
156
std::optional<size_t> CharToSize(char character);
167
} // namespace SSBL

include/Format.hpp

+3-27
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
11
#pragma once
22

3-
#include "Convert.hpp"
4-
#include "StringOps.hpp"
5-
#include <sstream>
6-
#include <string>
3+
#include "String.hpp"
74

85
namespace SSBL {
9-
10-
std::string WithPadding(const std::string &string);
11-
12-
bool ContainsPlaceholder(const std::string &string);
13-
std::string IndexPlaceholders(const std::string &string);
14-
15-
template<typename T>
16-
std::string Format(const std::string &string, const T &value, const size_t insertNumber) {
17-
if (!ContainsPlaceholder(string))
18-
return string + GenericToString(value);
19-
20-
std::string out = IndexPlaceholders(string);
21-
const std::string placeholder = "{" + std::to_string(insertNumber) + "}";
22-
23-
out = ReplaceAll(out, placeholder, GenericToString(value));
24-
out = ReplaceAll(out, "\n", "");
25-
26-
if (const size_t pos = out.find('\n'); pos != std::string::npos)
27-
out.replace(pos, 1, "");
28-
29-
return out;
30-
}
31-
6+
String IndexEmptyPlaceholders(const String &string);
7+
String Format(const String &string, const String& insertString, size_t index = 1);
328
} // namespace SSBL

include/Log.hpp

-49
This file was deleted.

include/Logger.hpp

+48-45
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,70 @@
11
#pragma once
22

3-
#include "Format.hpp"
4-
#include "Log.hpp"
3+
#include "Console.hpp"
4+
#include "String.hpp"
55
#include <fstream>
6-
#include <iostream>
6+
#include <optional>
77
#include <sstream>
88

99
namespace SSBL {
10-
class Logger {
11-
public:
12-
Logger &Log(Level level = Level::Info);
13-
Logger &LogWarn();
14-
Logger &LogError();
15-
Logger &LogFatal();
16-
17-
Logger &LogToFile(const std::string &filePath, Level level = Level::Info);
10+
enum class LogLevel {
11+
NON = 0b0000,
12+
INF = 0b0001,
13+
WRN = 0b0010,
14+
ERR = 0b0100,
15+
FTL = 0b1000,
16+
ALL = 0b1111,
17+
};
1818

19-
template<typename T>
20-
Logger &operator<<(const T &stream) {
21-
if (!IsLevelIncluded(m_level))
22-
return *this;
19+
String LogLevelToString(LogLevel level);
20+
Color LogLevelToColor(LogLevel level);
2321

24-
m_stream.str(Format(m_stream.str(), GenericToString(stream), insertCount));
25-
insertCount++;
22+
struct LoggerSettings {
23+
bool useColor = false;
24+
bool showTimestamp = true;
25+
String timeFormat = "%y-%m-%d %D %H:%M:%S";
26+
};
2627

27-
if (Contains(GenericToString(stream), "\n")) {
28-
Send(m_stream.str());
29-
Flush();
30-
}
28+
class Logger;
3129

32-
return *this;
33-
}
30+
class LoggerStream {
31+
public:
32+
explicit LoggerStream(Logger &logger);
33+
~LoggerStream();
3434

35-
void SetOutStream(std::ostream *messageOutStream, std::ostream *errorOutStream);
36-
void SetConfig(const Config &config);
37-
void SetLevelMask(Level level);
38-
void SetTimestampFormat(const std::string &format);
39-
bool IsLevelIncluded(Level level) const;
35+
LoggerStream &operator<<(const String &string);
4036

4137
private:
42-
std::ostream *m_msgOutStream = &std::cout;
43-
std::ostream *m_errOutStream = &std::cerr;
44-
std::optional<std::ofstream> m_fileOutStream;
38+
Logger &m_logger;
39+
};
4540

46-
std::ostringstream m_stream;
47-
size_t insertCount = 0;
41+
class Logger {
42+
public:
43+
explicit Logger(const LoggerSettings &settings = LoggerSettings(), const String &outputFile = String::Empty());
44+
explicit Logger(const String &outputFile);
45+
~Logger();
4846

49-
Config m_config = Config::Timestamp;
50-
Level m_levelMask = Level::All;
51-
Level m_level = Level::Info;
47+
LoggerStream Log(LogLevel level = LogLevel::INF);
5248

53-
std::string m_timestampFormat = DEFAULT_FORMAT;
49+
LoggerStream LogWarn();
50+
LoggerStream LogError();
51+
LoggerStream LogFatal();
5452

55-
void Send(const std::string &string);
53+
Logger &UseColor(bool useColor);
54+
Logger &ShowTimestamp(bool showTimestamp);
55+
Logger &SetOutputFile(const String &fileName);
56+
Logger &SetTimeFormat(const String &timeFormat);
57+
58+
void operator<<(const String &string);
5659
void Flush();
5760

58-
void SetColor();
59-
void ResetColor() const;
61+
private:
62+
void ProcessStream();
6063

61-
#if _WIN32
62-
int GetColor() const;
63-
#else
64-
std::string GetColor() const;
65-
#endif
64+
LoggerSettings m_settings;
65+
String m_stream;
66+
LogLevel m_currentLevel = LogLevel::INF;
67+
size_t m_insertCount = 0;
68+
std::ofstream m_file;
6669
};
6770
} // namespace SSBL

0 commit comments

Comments
 (0)