Skip to content

Commit

Permalink
Added custom loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
muit committed Aug 8, 2024
1 parent 9b21a80 commit 42bab5e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Include/Pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

namespace p
{
PIPE_API void Initialize(StringView logPath = {});
PIPE_API void Initialize(Logger* logger = nullptr);

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Windows (msvc, Debug)

'Initialize': this use of 'void' is not valid

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Windows (msvc, Debug)

'Logger': undeclared identifier

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Windows (msvc, Debug)

'logger': undeclared identifier

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Windows (clang-17, Release)

unknown type name 'Logger'

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Windows (msvc, Release)

'Initialize': this use of 'void' is not valid

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Windows (msvc, Release)

'Logger': undeclared identifier

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Windows (msvc, Release)

'logger': undeclared identifier

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Windows (clang-17, Debug)

unknown type name 'Logger'

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Linux (gcc-13, Release)

variable or field ‘Initialize’ declared void

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Linux (gcc-13, Release)

‘Logger’ was not declared in this scope

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Linux (gcc-13, Release)

‘logger’ was not declared in this scope

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Linux (gcc-13, Debug)

variable or field ‘Initialize’ declared void

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Linux (gcc-13, Debug)

‘Logger’ was not declared in this scope

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Linux (gcc-13, Debug)

‘logger’ was not declared in this scope

Check failure on line 11 in Include/Pipe.h

View workflow job for this annotation

GitHub Actions / Linux (clang-16, Release)

unknown type name 'Logger'
PIPE_API void Shutdown();
}; // namespace p
9 changes: 8 additions & 1 deletion Include/Pipe/Core/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

namespace p
{
PIPE_API void InitLog(StringView logPath = {});
struct Logger
{
std::function<void(StringView)> infoCallback;
std::function<void(StringView)> warningCallback;
std::function<void(StringView)> errorCallback;
};

PIPE_API void InitLog(Logger* logger = nullptr);
PIPE_API void ShutdownLog();

PIPE_API void Info(StringView msg);
Expand Down
1 change: 0 additions & 1 deletion Include/PipeReflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,6 @@ namespace p
{
return TPtr<To>{value};
}

TPtr<From> ptr{value};
return Cast<To>(ptr);
}
Expand Down
65 changes: 48 additions & 17 deletions Src/Core/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,65 @@

namespace p
{
void InitLog(StringView logPath) {}
// clang-format off
const Logger defaultLogger = Logger{
.infoCallback = [](StringView msg) {
String text;
auto now = DateTime::Now();
now.ToString("[%Y/%m/%d %H:%M:%S]", text);
Strings::FormatTo(text, "[Info] {}\n", msg);
std::cout << text;
},
.warningCallback = [](StringView msg) {
String text;
auto now = DateTime::Now();
now.ToString("[%Y/%m/%d %H:%M:%S]", text);
Strings::FormatTo(text, "[Warning] {}\n", msg);
std::cout << text;
},
.errorCallback = [](StringView msg) {
String text;
auto now = DateTime::Now();
now.ToString("[%Y/%m/%d %H:%M:%S]", text);
Strings::FormatTo(text, "[Error] {}\n", msg);
std::cout << text;
}
};
// clang-format on

void ShutdownLog() {}
const Logger* globalLogger = nullptr;

void InitLog(Logger* logger)
{
globalLogger = logger ? logger : &defaultLogger;
}

void ShutdownLog()
{
globalLogger = nullptr;
}

void Info(StringView msg)
{
String text;
auto now = DateTime::Now();
now.ToString("[%Y/%m/%d %H:%M:%S]", text);
Strings::FormatTo(text, "[Info] {}", msg);
std::cout << text << std::endl;
if (globalLogger)
{
globalLogger->infoCallback(msg);
}
}

void Warning(StringView msg)
{
String text;
auto now = DateTime::Now();
now.ToString("[%Y/%m/%d %H:%M:%S]", text);
Strings::FormatTo(text, "[Warning] {}", msg);
std::cout << text << std::endl;
if (globalLogger)
{
globalLogger->warningCallback(msg);
}
}

void Error(StringView msg)
{
String text;
auto now = DateTime::Now();
now.ToString("[%Y/%m/%d %H:%M:%S]", text);
Strings::FormatTo(text, "[Error] {}", msg);
std::cout << text << std::endl;
if (globalLogger)
{
globalLogger->errorCallback(msg);
}
}
} // namespace p
4 changes: 2 additions & 2 deletions Src/Pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

namespace p
{
void Initialize(StringView logPath)
void Initialize(Logger* logger)

Check failure on line 16 in Src/Pipe.cpp

View workflow job for this annotation

GitHub Actions / Windows (msvc, Debug)

'p::Initialize': redefinition; previous definition was 'data variable'

Check failure on line 16 in Src/Pipe.cpp

View workflow job for this annotation

GitHub Actions / Windows (msvc, Release)

'p::Initialize': redefinition; previous definition was 'data variable'
{
InitializeMemory();
InitializeReflect();
InitLog(logPath);
InitLog(logger);
}

void Shutdown()
Expand Down
2 changes: 1 addition & 1 deletion Tests/Reflection/TypeName.spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ go_bandit([]() {
AssertThat(GetTypeName<i16>(), Equals("i16"));
AssertThat(GetTypeName<i32>(), Equals("i32"));
AssertThat(GetTypeName<i64>(), Equals("i64"));
AssertThat(GetTypeName<char>(), Equals("TChar"));
AssertThat(GetTypeName<char>(), Equals("char"));
AssertThat(GetTypeName<StringView>(), Equals("StringView"));
AssertThat(GetTypeName<String>(), Equals("String"));
});
Expand Down

0 comments on commit 42bab5e

Please sign in to comment.