CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Debugger/WebSocket/LogBroadcaster.cpp
Views: 1401
1
// Copyright (c) 2018- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
#include <mutex>
20
#include "Common/Log/LogManager.h"
21
#include "Core/Debugger/WebSocket/LogBroadcaster.h"
22
#include "Core/Debugger/WebSocket/WebSocketUtils.h"
23
24
class DebuggerLogListener : public LogListener {
25
public:
26
void Log(const LogMessage &msg) override {
27
std::lock_guard<std::mutex> guard(lock_);
28
messages_[nextMessage_] = msg;
29
nextMessage_++;
30
if (nextMessage_ >= BUFFER_SIZE)
31
nextMessage_ -= BUFFER_SIZE;
32
count_++;
33
}
34
35
std::vector<LogMessage> GetMessages() {
36
std::lock_guard<std::mutex> guard(lock_);
37
int splitPoint;
38
int readCount;
39
if (read_ + BUFFER_SIZE < count_) {
40
// We'll start with our oldest then.
41
splitPoint = nextMessage_;
42
readCount = Count();
43
} else {
44
splitPoint = read_;
45
readCount = count_ - read_;
46
}
47
48
read_ = count_;
49
50
std::vector<LogMessage> results;
51
int splitEnd = std::min(splitPoint + readCount, (int)BUFFER_SIZE);
52
for (int i = splitPoint; i < splitEnd; ++i) {
53
results.push_back(messages_[i]);
54
readCount--;
55
}
56
for (int i = 0; i < readCount; ++i) {
57
results.push_back(messages_[i]);
58
}
59
60
return results;
61
}
62
63
int Count() const {
64
return count_ < BUFFER_SIZE ? count_ : BUFFER_SIZE;
65
}
66
67
private:
68
enum { BUFFER_SIZE = 1024 };
69
LogMessage messages_[BUFFER_SIZE];
70
std::mutex lock_;
71
int nextMessage_ = 0;
72
int count_ = 0;
73
int read_ = 0;
74
};
75
76
LogBroadcaster::LogBroadcaster() {
77
listener_ = new DebuggerLogListener();
78
if (LogManager::GetInstance())
79
LogManager::GetInstance()->AddListener(listener_);
80
}
81
82
LogBroadcaster::~LogBroadcaster() {
83
if (LogManager::GetInstance())
84
LogManager::GetInstance()->RemoveListener(listener_);
85
delete listener_;
86
}
87
88
struct DebuggerLogEvent {
89
const LogMessage &l;
90
91
operator std::string() {
92
JsonWriter j;
93
j.begin();
94
j.writeString("event", "log");
95
j.writeString("timestamp", l.timestamp);
96
j.writeString("header", l.header);
97
j.writeString("message", l.msg);
98
j.writeInt("level", (int)l.level);
99
j.writeString("channel", l.log);
100
j.end();
101
return j.str();
102
}
103
};
104
105
// Log message (log)
106
//
107
// Sent unexpectedly with these properties:
108
// - timestamp: string timestamp of event.
109
// - header: string header information about the event (including file/line.)
110
// - message: actual log message as a string.
111
// - level: number severity level (1 = highest.)
112
// - channel: string describing log channel / grouping.
113
void LogBroadcaster::Broadcast(net::WebSocketServer *ws) {
114
auto messages = listener_->GetMessages();
115
for (auto msg : messages) {
116
ws->Send(DebuggerLogEvent{msg});
117
}
118
}
119
120