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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
// ----------------------------------------------------------------------------
// debug.cxx
//
// Copyright (C) 2014
// Stelios Bounanos, M0GLD
// David Freese, W1HKJ
//
// This file is part of fldigi.
//
// fldigi is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// fldigi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <string>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Slider.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Menu_Item.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Browser.H>
#include "debug.h"
#include "icons.h"
#include "gettext.h"
#include "rig.h"
using namespace std;
#define MAX_LINES 65536
static FILE* wfile;
static FILE* rfile;
static int rfd;
static bool tty;
static Fl_Double_Window* window;
static Fl_Browser* btext;
static string buffer;
debug* debug::inst = 0;
//debug::level_e debug::level = debug::ERROR_LEVEL;
debug::level_e debug::level = debug::INFO_LEVEL;
uint32_t debug::mask = ~0u;
const char* prefix[] = { _("Quiet"), _("Error"), _("Warning"), _("Info"), _("Debug") };
static void slider_cb(Fl_Widget* w, void*);
static void clear_cb(Fl_Widget *w, void*);
void debug::start(const char* filename)
{
if (debug::inst)
return;
inst = new debug(filename);
window = new Fl_Double_Window(600, 256, _("Event log"));
int pad = 2;
Fl_Slider* slider = new Fl_Slider(pad, pad, 128, 20, prefix[level]);
slider->tooltip(_("Change log level"));
slider->align(FL_ALIGN_RIGHT);
slider->type(FL_HOR_NICE_SLIDER);
slider->range(0.0, LOG_NLEVELS - 1);
slider->step(1.0);
slider->value(level);
slider->callback(slider_cb);
Fl_Button* clearbtn = new Fl_Button(window->w() - 60, pad, 60, 20, "clear");
clearbtn->callback(clear_cb);
btext = new Fl_Browser(pad, slider->h()+pad, window->w()-2*pad, window->h()-slider->h()-2*pad, 0);
window->resizable(btext);
buffer.clear();
window->end();
}
void debug::stop(void)
{
delete inst;
inst = 0;
delete window;
}
static char fmt[1024];
static char sztemp[8096];
static string estr = "";
bool debug_in_use = false;
void debug::log(level_e level, const char* func, const char* srcf, int line, const char* format, ...)
{
if (!inst) return;
if (level > debug::level) return;
snprintf(fmt, sizeof(fmt), "%c: %s: %s\n", *prefix[level], func, format);
while(debug_in_use) MilliSleep(1);
va_list args;
va_start(args, format);
vsnprintf(sztemp, sizeof(sztemp), fmt, args);
estr.append(sztemp);
//printf("%s", sztemp);
fprintf(wfile, "%s", sztemp);
va_end(args);
fflush(wfile);
Fl::awake(sync_text, 0);
}
void debug::slog(level_e level, const char* func, const char* srcf, int line, const char* format, ...)
{
if (!inst) return;
if (level > debug::level) return;
snprintf(fmt, sizeof(fmt), "%c:%s\n", *prefix[level], format);
while(debug_in_use) MilliSleep(1);
va_list args;
va_start(args, format);
vsnprintf(sztemp, sizeof(sztemp), fmt, args);
estr.append(sztemp);
//printf("%s\n", sztemp);
fprintf(wfile, "%s", sztemp);
va_end(args);
fflush(wfile);
Fl::awake(sync_text, 0);
}
void debug::elog(const char* func, const char* srcf, int line, const char* text)
{
log(ERROR_LEVEL, func, srcf, line, "%s: %s", text, strerror(errno));
}
void debug::show(void)
{
window->show();
}
void debug::sync_text(void* arg)
{
debug_in_use = true;
size_t p0 = 0, p1 = estr.find('\n');
while (p1 != string::npos) {
btext->insert(1, estr.substr(p0,p1-p0).c_str());
buffer.append(estr.substr(p0, p1 - p0)).append("\n");
p0 = p1 + 1;
p1 = estr.find('\n', p0);
}
estr = "";
debug_in_use = false;
}
debug::debug(const char* filename)
{
if ((wfile = fopen(filename, "w")) == NULL)
throw strerror(errno);
setvbuf(wfile, (char*)NULL, _IOLBF, 0);
set_cloexec(fileno(wfile), 1);
if ((rfile = fopen(filename, "r")) == NULL)
throw strerror(errno);
rfd = fileno(rfile);
set_cloexec(rfd, 1);
#ifndef __WIN32__
int f;
if ((f = fcntl(rfd, F_GETFL)) == -1)
throw strerror(errno);
if (fcntl(rfd, F_SETFL, f | O_NONBLOCK) == -1)
throw strerror(errno);
#endif
tty = isatty(fileno(stderr));
}
debug::~debug()
{
fclose(wfile);
fclose(rfile);
}
static void slider_cb(Fl_Widget* w, void*)
{
debug::level = (debug::level_e)((Fl_Slider*)w)->value();
w->label(prefix[debug::level]);
w->parent()->redraw();
}
static void clear_cb(Fl_Widget* w, void*)
{
btext->clear();
buffer.clear();
}
|