xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
logger.hpp
浏览该文件的文档.
1#pragma once
2#include <chrono>
3#include <iostream>
4#include <memory>
5#include <ostream>
6#include <sstream>
7#include <string>
8#include <vector>
9#ifdef _WIN32
10#include <process.h>
11
12#include <thread>
13
14#define GET_PID() _getpid()
15#define GET_TID() std::this_thread::get_id()._Get_underlying_id()
16
17#elif defined(__linux__)
18#include <sys/syscall.h>
19#include <unistd.h>
20
21#define GET_PID() getpid()
22#define GET_TID() static_cast<unsigned long>(syscall(SYS_gettid))
23
24#elif defined(__APPLE__)
25#include <pthread.h>
26#include <unistd.h>
27
28#define GET_PID() getpid()
29static inline unsigned long GET_TID() {
30 uint64_t tid;
31 pthread_threadid_np(nullptr, &tid);
32 return static_cast<unsigned long>(tid);
33}
34
35#else
36#error "Unsupported platform"
37#endif
38
39#include "./message.hpp"
40
41class IpcLogTest;
42namespace xclogger {
43class ImplCatgory;
44template <class SubmitStream_ptr>
45class LogStream {
46 friend class ::IpcLogTest;
47
48 private:
49 SubmitStream_ptr submit_stream_;
50 Message msg_;
51
52 public:
53 LogStream(SubmitStream_ptr submit_stream, const char* file, int line,
54 const char* function, int level, const char* role = "",
55 const char* label = "")
56 : submit_stream_(submit_stream),
57 msg_(role, label, file, function,
58 std::chrono::duration_cast<std::chrono::microseconds>(
59 std::chrono::system_clock::now().time_since_epoch())
60 .count(),
61 GET_PID(), GET_TID(), line, level, {}){};
63 if (submit_stream_) {
64 if constexpr (std::is_same_v<SubmitStream_ptr,
65 std::shared_ptr<std::ostream>>)
66 xclogger::operator<< <ImplCatgory>(std::cout, msg_);
67 else
68 (*submit_stream_) << msg_;
69 } else
70 xclogger::operator<< <ImplCatgory>(std::cout, msg_);
71 }
72 template <typename T>
73 LogStream& operator<<(const T& msg) {
74 msg_.messages.emplace_back((std::stringstream() << msg).str());
75 return *this;
76 }
77};
78template <class Category = xclogger::ImplCatgory>
80 return std::shared_ptr<std::ostream>();
81}
82} // namespace xclogger
83
84#ifdef XCLOG_FUNCTION_TRACK
85#define XCLOG_FUNCTION_NAME __FUNCTION__
86#else
87#define XCLOG_FUNCTION_NAME ""
88#endif
89#ifdef XCLOG_FILE_TRACK
90#define XCLOG_FILE_NAME __FILE__
91#else
92#define XCLOG_FILE_NAME ""
93#endif
94#ifdef XCLOG_LINE_TRACK
95#define XCLOG_LINE_NAME __LINE__
96#else
97#define XCLOG_LINE_NAME 0
98#endif
99
100#define XCLOG_SUBMIT_STREAM_INSTENCE_IMPT() \
101 template <> \
102 inline auto ::xclogger::SubmitStreamInstance<::xclogger::ImplCatgory>()
103
104#define XLOG(level, role, label) \
105 ::xclogger::LogStream( \
106 ::xclogger::SubmitStreamInstance<::xclogger::ImplCatgory>(), \
107 XCLOG_FILE_NAME, XCLOG_LINE_NAME, XCLOG_FUNCTION_NAME, level, #role, \
108 #label)
109#define LOG(level) XLOG(level, , )
LogStream & operator<<(const T &msg)
Definition logger.hpp:73
LogStream(SubmitStream_ptr submit_stream, const char *file, int line, const char *function, int level, const char *role="", const char *label="")
Definition logger.hpp:53
auto SubmitStreamInstance()
Definition logger.hpp:79
std::vector< std::string > messages
Definition message.hpp:23