-
Notifications
You must be signed in to change notification settings - Fork 2
/
gstlog.cpp
151 lines (117 loc) · 3.45 KB
/
gstlog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "gstlog.hpp"
#include <string>
#include <iostream>
static const ImVec4 WARNING_COLOR = ImVec4 (1.0f, 0.75f, 0.0f, 1.0f);
static const ImVec4 ERROR_COLOR = ImVec4 (1.0f, 0.0f, 0.25f, 1.0f);
static const ImVec4 TEXT_FG_COLOR = ImVec4 (0.0f, 0.0f, 0.0f, 1.0f);
static const ImVec4 LOG_BG_COLOR = ImVec4 (0.02f, 0.02f, 0.02f, 1.0f);
GstLog::GstLog ()
: buf ()
, line_offsets ()
, track (true)
, filter ()
, file ()
, timer (g_timer_new ())
{
g_timer_start (timer);
g_snprintf (log_path, sizeof (log_path), "%s" G_DIR_SEPARATOR_S "gst-%p.log", g_get_tmp_dir (), this);
g_setenv ("GST_DEBUG_FILE", log_path, TRUE);
std::cout << log_path << std::endl;
const gchar* gst_debug_env = g_getenv ("GST_DEBUG");
g_snprintf (gst_debug, sizeof (gst_debug), "GST_DEBUG=%s", gst_debug_env ? gst_debug_env : "");
}
GstLog::~GstLog ()
{
g_timer_destroy (timer);
}
static void
render_log_line (const gchar* line, const gchar* line_end)
{
ImDrawList* draw_list = ImGui::GetWindowDrawList ();
const ImVec2 p = ImGui::GetCursorScreenPos ();
ImVec4 colf;
bool draw_bg = false;
if (g_strstr_len (line, line_end - line, "WARN")) {
colf = WARNING_COLOR;
draw_bg = true;
}
else if (g_strstr_len (line, line_end - line, "ERROR")) {
colf = ERROR_COLOR;
draw_bg = true;
}
if (draw_bg) {
ImVec2 text_size = ImGui::CalcTextSize (line, line_end);
draw_list->AddRectFilled (ImVec2 (p.x, p.y), ImVec2 (p.x + text_size.x, p.y + text_size.y), ImColor (colf));
ImGui::PushStyleColor (ImGuiCol_Text, TEXT_FG_COLOR);
ImGui::TextUnformatted (line, line_end);
ImGui::PopStyleColor ();
}
else
ImGui::TextUnformatted (line, line_end);
}
void GstLog::readLines ()
{
gdouble elapsed = g_timer_elapsed (timer, NULL);
if (elapsed < 0.5)
return;
g_timer_start (timer);
if (!file.is_open ())
file.open (log_path, std::fstream::in);
if (file.is_open ()) {
file.clear ();
const int max_lines = 100;
int i = 0;
std::string line;
for (; i < max_lines && std::getline (file, line); i++) {
line_offsets.push_back (buf.size ());
buf.append (line.c_str (), line.c_str () + line.size ());
}
}
}
void GstLog::render (bool* open)
{
readLines ();
ImGui::SetNextWindowSize (ImVec2 (600, 250), ImGuiCond_FirstUseEver);
ImGui::Begin ("GStreamer Log", open);
if (ImGui::Button ("Clear")) {
buf.clear ();
line_offsets.clear ();
}
ImGui::SameLine ();
bool copy = ImGui::Button ("Copy");
ImGui::SameLine ();
bool set_track = ImGui::Button ("Track");
ImGui::SameLine ();
ImGui::Text (gst_debug);
ImGui::SameLine ();
filter.Draw ("filter", -100.0f);
ImGui::Separator ();
ImGui::PushStyleColor (ImGuiCol_ChildBg, LOG_BG_COLOR);
ImGui::BeginChild ("scrolling");
ImGui::PushStyleVar (ImGuiStyleVar_ItemSpacing, ImVec2 (0, 1));
if (copy)
ImGui::LogToClipboard ();
const char* buf_begin = buf.begin ();
const char* line = buf_begin;
for (int line_no = 0; line != NULL; line_no++)
{
const char* line_end = (line_no < line_offsets.Size) ? buf_begin + line_offsets[line_no] : NULL;
if (filter.IsActive ()) {
if (filter.PassFilter (line, line_end))
render_log_line (line, line_end);
}
else
render_log_line (line, line_end);
line = line_end && line_end[1] ? line_end + 1 : NULL;
}
if (!set_track && track && (ImGui::GetScrollMaxY () - ImGui::GetScrollY () > 0.1f))
track = false;
if (set_track || track) {
ImGui::SetScrollHere (1.0f);
track = true;
}
ImGui::PopStyleVar ();
ImGui::EndChild ();
ImGui::PopStyleColor ();
ImGui::End ();
}