Skip to content

Commit c428fbd

Browse files
committed
Add timestamp formatting
1 parent baf3990 commit c428fbd

File tree

9 files changed

+73
-31
lines changed

9 files changed

+73
-31
lines changed

CMakeLists.txt

+4-18
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/out/lib/${CMAKE_SYSTEM_
66
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/out/bin/${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_BUILD_TYPE})
77
include_directories(src include)
88

9-
add_library(
10-
logger STATIC
9+
set(
10+
LIBRARY_SOURCES
1111
src/Logger.cpp
1212
include/Logger.hpp
1313
include/Time.hpp
@@ -22,19 +22,5 @@ add_library(
2222
src/StringOps.cpp
2323
)
2424

25-
add_executable(
26-
example
27-
src/Logger.cpp
28-
include/Logger.hpp
29-
src/example/example.cpp
30-
include/Time.hpp
31-
src/Time.cpp
32-
include/Format.hpp
33-
src/Format.cpp
34-
include/Log.hpp
35-
src/Log.cpp
36-
include/Convert.hpp
37-
src/Convert.cpp
38-
include/StringOps.hpp
39-
src/StringOps.cpp
40-
)
25+
add_library(logger STATIC ${LIBRARY_SOURCES})
26+
add_executable(example ${LIBRARY_SOURCES} src/example/example.cpp)

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ cmake --build ./build
2020
using namespace SSBL;
2121

2222
Logger logger;
23+
24+
// Configuration (color and timestamp)
2325
logger.SetConfig(Config::All);
2426

27+
// Timestamp formatting
28+
logger.SetTimestampFormat("%h:%m:%s");
29+
2530
// Log levels
2631
logger.Log() << "Hello, Info!\n";
2732
logger.LogWarn() << "Hello, Warning!\n";

include/Log.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "Time.hpp"
34
#include <string>
45

56
namespace SSBL {
@@ -43,6 +44,6 @@ constexpr Config operator~(Config level) {
4344
return static_cast<Config>(~static_cast<char>(level));
4445
}
4546

46-
std::string ToLog(const std::string &string, Level level, Config config);
47+
std::string ToLog(const std::string &string, Level level, Config config, const std::string &timestampFormat = DEFAULT_FORMAT);
4748
std::string LogLevelToString(Level level);
4849
} // namespace SSBL

include/Logger.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Logger {
3535
void SetOutStream(std::ostream *messageOutStream, std::ostream *errorOutStream);
3636
void SetConfig(const Config &config);
3737
void SetLevelMask(Level level);
38+
void SetTimestampFormat(const std::string &format);
3839
bool IsLevelIncluded(Level level) const;
3940

4041
private:
@@ -49,6 +50,8 @@ class Logger {
4950
Level m_levelMask = Level::All;
5051
Level m_level = Level::Info;
5152

53+
std::string m_timestampFormat = DEFAULT_FORMAT;
54+
5255
void Send(const std::string &string);
5356
void Flush();
5457

include/Time.hpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,20 @@
33
#include <string>
44

55
namespace SSBL {
6-
std::string GetTimestamp();
6+
7+
constexpr std::string DEFAULT_FORMAT = "%Y %M %D %h:%m:%s";
8+
9+
struct Time {
10+
size_t year;
11+
std::string month;
12+
size_t day;
13+
size_t hour;
14+
size_t minute;
15+
size_t second;
16+
17+
explicit Time(time_t time);
18+
19+
[[nodiscard]] std::string AsString(const std::string &format = DEFAULT_FORMAT) const;
20+
[[nodiscard]] static std::string GetCurrentTime(const std::string &format = DEFAULT_FORMAT);
21+
};
722
} // namespace SSBL

src/Log.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
namespace SSBL {
77

8-
std::string ToLog(const std::string &string, const Level level, const Config config) {
8+
std::string ToLog(const std::string &string, const Level level, const Config config, const std::string &timestampFormat) {
99
std::string log;
1010

1111
if (static_cast<bool>(config & Config::Timestamp))
12-
log += WithPadding(GetTimestamp());
12+
log += WithPadding(Time::GetCurrentTime(timestampFormat));
1313

1414
log += WithPadding(LogLevelToString(level));
1515
log += string;

src/Logger.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ void Logger::SetLevelMask(const Level level) {
4545
m_levelMask = level;
4646
}
4747

48+
void Logger::SetTimestampFormat(const std::string &format) {
49+
m_timestampFormat = format;
50+
}
51+
4852
bool Logger::IsLevelIncluded(const Level level) const {
4953
return (static_cast<int>(level) & static_cast<int>(m_levelMask)) != 0;
5054
}
@@ -62,14 +66,14 @@ void Logger::Send(const std::string &string) {
6266

6367
switch (m_level) {
6468
case Level::Fatal:
65-
*m_errOutStream << ToLog(string, m_level, m_config);
69+
*m_errOutStream << ToLog(string, m_level, m_config, m_timestampFormat);
6670
std::exit(EXIT_FAILURE);
6771
break;
6872
case Level::Error:
69-
*m_errOutStream << ToLog(string, m_level, m_config);
73+
*m_errOutStream << ToLog(string, m_level, m_config, m_timestampFormat);
7074
break;
7175
default:
72-
*m_msgOutStream << ToLog(string, m_level, m_config);
76+
*m_msgOutStream << ToLog(string, m_level, m_config, m_timestampFormat);
7377
break;
7478
}
7579
}

src/Time.cpp

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
#include "Time.hpp"
22

3+
#include "StringOps.hpp"
34
#include <chrono>
45
#include <ctime>
56

67
namespace SSBL {
7-
std::string GetTimestamp() {
8-
using std::chrono::system_clock;
8+
Time::Time(const time_t time) {
9+
const std::string timeString = std::ctime(&time);
910

10-
const auto currentTime = system_clock::to_time_t(system_clock::now());
11+
year = std::stoi(timeString.substr(20, 4));
12+
month = timeString.substr(4, 3);
13+
day = std::stoi(timeString.substr(8, 2));
14+
15+
hour = std::stoi(timeString.substr(11, 2));
16+
minute = std::stoi(timeString.substr(14, 2));
17+
second = std::stoi(timeString.substr(17, 2));
18+
}
19+
20+
std::string Time::AsString(const std::string &format) const {
21+
std::string result = format;
22+
23+
result = ReplaceAll(result, "%Y", std::to_string(year));
24+
result = ReplaceAll(result, "%M", month);
25+
result = ReplaceAll(result, "%D", std::to_string(day));
1126

12-
std::string currentTimeString = ctime(&currentTime);
13-
currentTimeString.pop_back();
27+
result = ReplaceAll(result, "%h", std::to_string(hour));
28+
result = ReplaceAll(result, "%m", std::to_string(minute));
29+
result = ReplaceAll(result, "%s", std::to_string(second));
1430

15-
return currentTimeString;
31+
return result;
32+
}
33+
34+
std::string Time::GetCurrentTime(const std::string &format) {
35+
using std::chrono::system_clock;
36+
37+
const auto currentTime = system_clock::to_time_t(system_clock::now());
38+
return Time(currentTime).AsString(format);
1639
}
1740
} // namespace SSBL

src/example/example.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ int main() {
44
using namespace SSBL;
55

66
Logger logger;
7+
8+
// Configuration (color and timestamp)
79
logger.SetConfig(Config::All);
810

11+
// Timestamp formatting
12+
logger.SetTimestampFormat("%h:%m:%s");
13+
914
// Log levels
1015
logger.Log() << "Hello, Info!\n";
1116
logger.LogWarn() << "Hello, Warning!\n";

0 commit comments

Comments
 (0)