aboutsummaryrefslogtreecommitdiff
path: root/src/common/Logger.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-10-19 22:33:01 +0800
committercrupest <crupest@outlook.com>2021-10-19 22:33:01 +0800
commitfd2e84640b2be52f97f48d818d26fc1289a50c7a (patch)
treeab94a27fae4b4bb47a6124a478b20c82a1393b9b /src/common/Logger.cpp
parent44f24f8ece48fd4acc2e8d5f5a052cbc1981768c (diff)
downloadcru-fd2e84640b2be52f97f48d818d26fc1289a50c7a.tar.gz
cru-fd2e84640b2be52f97f48d818d26fc1289a50c7a.tar.bz2
cru-fd2e84640b2be52f97f48d818d26fc1289a50c7a.zip
...
Diffstat (limited to 'src/common/Logger.cpp')
-rw-r--r--src/common/Logger.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/common/Logger.cpp b/src/common/Logger.cpp
index 6ba5d578..3034c0ad 100644
--- a/src/common/Logger.cpp
+++ b/src/common/Logger.cpp
@@ -3,7 +3,9 @@
#include <array>
#include <cstdlib>
#include <ctime>
+#include <iostream>
#include <memory>
+#include <mutex>
#include <string_view>
namespace cru::log {
@@ -76,4 +78,31 @@ void Logger::Log(LogLevel level, StringView tag, StringView message) {
LogLevelToString(level), tag, message));
}
}
+
+namespace {
+std::mutex stdio_lock;
+
+void WriteStdio(LogLevel level, StringView s) {
+ std::string m = s.ToString().ToUtf8();
+
+ if (level == LogLevel::Error) {
+ std::cerr << m;
+ } else {
+ std::cout << m;
+ }
+}
+} // namespace
+
+StdioLogSource::StdioLogSource(bool use_lock) : use_lock_(use_lock) {}
+
+StdioLogSource::~StdioLogSource() {}
+
+void StdioLogSource::Write(LogLevel level, StringView s) {
+ if (use_lock_) {
+ std::lock_guard<std::mutex> guard(stdio_lock);
+ WriteStdio(level, s);
+ } else {
+ WriteStdio(level, s);
+ }
+}
} // namespace cru::log