1
// ----------------------------------------------------------------------------
5
// Stelios Bounanos, M0GLD
7
// This file is part of fldigi.
9
// fldigi is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 3 of the License, or
12
// (at your option) any later version.
14
// fldigi is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program. If not, see <http://www.gnu.org/licenses/>.
21
// ----------------------------------------------------------------------------
35
#include <FL/Fl_Double_Window.H>
36
#include <FL/Fl_Slider.H>
37
#include <FL/Fl_Button.H>
38
#include <FL/Fl_Menu_Item.H>
39
#include <FL/Fl_Menu_Button.H>
41
#include <FL/Fl_Browser.H>
50
#define MAX_LINES 65536
57
static Fl_Double_Window* window;
58
static Fl_Browser* btext;
61
debug* debug::inst = 0;
62
debug::level_e debug::level = debug::ERROR_LEVEL;
63
uint32_t debug::mask = ~0u;
65
const char* prefix[] = { _("Quiet"), _("Error"), _("Warning"), _("Info"), _("Debug") };
67
static void slider_cb(Fl_Widget* w, void*);
68
static void clear_cb(Fl_Widget *w, void*);
70
void debug::start(const char* filename)
74
inst = new debug(filename);
76
window = new Fl_Double_Window(600, 256, _("Event log"));
80
Fl_Slider* slider = new Fl_Slider(pad, pad, 128, 20, prefix[level]);
81
slider->tooltip(_("Change log level"));
82
slider->align(FL_ALIGN_RIGHT);
83
slider->type(FL_HOR_NICE_SLIDER);
84
slider->range(0.0, LOG_NLEVELS - 1);
87
slider->callback(slider_cb);
89
Fl_Button* clearbtn = new Fl_Button(window->w() - 60, pad, 60, 20, "clear");
90
clearbtn->callback(clear_cb);
92
btext = new Fl_Browser(pad, slider->h()+pad, window->w()-2*pad, window->h()-slider->h()-2*pad, 0);
93
window->resizable(btext);
100
void debug::stop(void)
107
static char fmt[1024];
108
static char sztemp[1024];
109
static string estr = "";
110
bool debug_in_use = false;
112
void debug::log(level_e level, const char* func, const char* srcf, int line, const char* format, ...)
115
if (level > debug::level) return;
117
snprintf(fmt, sizeof(fmt), "%c: %s: %s\n", *prefix[level], func, format);
119
while(debug_in_use) MilliSleep(1);
122
va_start(args, format);
124
vsnprintf(sztemp, sizeof(sztemp), fmt, args);
127
fprintf(wfile, "%s", sztemp);
133
Fl::awake(sync_text, 0);
136
void debug::slog(level_e level, const char* func, const char* srcf, int line, const char* format, ...)
139
if (level > debug::level) return;
141
snprintf(fmt, sizeof(fmt), "%c:%s\n", *prefix[level], format);
143
while(debug_in_use) MilliSleep(1);
146
va_start(args, format);
148
vsnprintf(sztemp, sizeof(sztemp), fmt, args);
150
fprintf(wfile, "%s", sztemp);
155
Fl::awake(sync_text, 0);
158
void debug::elog(const char* func, const char* srcf, int line, const char* text)
160
if (level > debug::level) return;
161
log(ERROR_LEVEL, func, srcf, line, "%s: %s", text, strerror(errno));
164
void debug::show(void)
169
void debug::sync_text(void* arg)
172
size_t p0 = 0, p1 = estr.find('\n');
173
while (p1 != string::npos) {
174
btext->insert(1, estr.substr(p0,p1-p0).c_str());
175
buffer.append(estr.substr(p0, p1 - p0)).append("\n");
177
p1 = estr.find('\n', p0);
180
debug_in_use = false;
183
debug::debug(const char* filename)
185
if ((wfile = fopen(filename, "w")) == NULL)
186
throw strerror(errno);
187
setvbuf(wfile, (char*)NULL, _IOLBF, 0);
188
set_cloexec(fileno(wfile), 1);
190
if ((rfile = fopen(filename, "r")) == NULL)
191
throw strerror(errno);
196
if ((f = fcntl(rfd, F_GETFL)) == -1)
197
throw strerror(errno);
198
if (fcntl(rfd, F_SETFL, f | O_NONBLOCK) == -1)
199
throw strerror(errno);
201
tty = isatty(fileno(stderr));
210
static void slider_cb(Fl_Widget* w, void*)
212
debug::level = (debug::level_e)((Fl_Slider*)w)->value();
213
w->label(prefix[debug::level]);
214
w->parent()->redraw();
217
static void clear_cb(Fl_Widget* w, void*)