dl-fldigi/src/misc/debug.cxx

339 wiersze
7.9 KiB
C++

2008-08-26 22:33:34 +00:00
// ----------------------------------------------------------------------------
// debug.cxx
//
// Copyright (C) 2008-2010
2008-08-26 22:33:34 +00:00
// Stelios Bounanos, M0GLD
//
// 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 <config.h>
2009-04-17 18:17:55 +00:00
#ifdef __MINGW32__
# include "compat.h"
#endif
#include <sstream>
#include <fstream>
2008-08-26 22:33:34 +00:00
#include <cstdio>
#include <cstring>
#include <cstdarg>
#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>
2009-01-05 16:00:01 +00:00
#include <FL/Fl_Menu_Item.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Browser.H>
2008-08-26 22:33:34 +00:00
#include "debug.h"
#include "timeops.h"
#include "icons.h"
#include "gettext.h"
2008-08-26 22:33:34 +00:00
#include "threads.h"
#ifndef FLARQ_VERSION
# include "fl_digi.h"
#endif
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
KISS interface * New configuration panel 'IO' * Add power density zones. * Query and Set Kiss hardware commands * Support bcast rsid over kiss socket * limit selectable modem options to 8 bit versions only (kiss i/o mode) * Employs simple HDLC encapsulation frame for transmit/receive CRC data verification. * Uses histogram based threshold detection. * Include manual squelch button for KISS op's. * User configurable inhibit transmit on busy frequencies for nn seconds * Impose transmit wait on valid frame reception * Uses dual UDP ports for concurrent operation of programs using the same IP address. * Clears the transmit buffer when data is available for more then 10 mins and has not been transmitted. * Provides ax25 decoding. Code hijacked from the FLDIGI packet modem co - redirect character data display on the RX panel * Add non standard KISS frame to handle ARQ data. - interface can handle both ARQ data and HDLC ax25 frames on the same port. * Squelch related command and control functions to the KISS hardware interface. * User configuration item to disable CSMA from config panel and kiss hardware frame. * Additional KISS command to adjust KPSQL detection gain (for testing). * Removed unused function in sockets.cxx not compatible with LINUX OS. (from John, G8BPQ) * New command 'BUSY:<T|F>' * Custom bindUDP() for UDP support. * Waterfall display gain adjusted based on sample rate changes. * Expanded conditions that indicate BUSY:T on command request. * Log loopback condition to event log. * Restore KPQSL set level position on start up. * Conditionally log of the KISS data path strings * Added debug filter 'KISS control' to event log. * KPSQL threshold button indicator remains ON during transmit inhibit. indication of active 'Busy Channel'. * Added DEBUG Logging command LOG_HEX(const char *buf, int len); D:,... ReadFromHostSocket: ..BUSY:. C0 06 42 55 53 59 3A C0 * Added thread locks to waterfall.cxx: - powerDensity() - peakFreq(), - powerDensityMaximum() These are being accessed by more then one thread. * Swapped hash.m4 swapped test case to std::tr1::hash from std::hash - Updated pskrep.cxx to handle the change in hash.m4 * 8PSK2000 removed. * Coordination merge with branch kiss21. * Code clean up * Updated Documentation. Remove unused png's. * Request from Jim, W2XO: Add log.set_rst_in and log.set_rst_out xmlrpc commands. * Compiler warning pacification (global in scope). * New XMLRPC commands to query and set active IO port. * Documentation update for XMLRPC commands * WSAStartup and WSACleanup one for one issue. * Update Docs for 3.22 release. New png's added.
2014-10-13 12:21:30 +00:00
static pthread_mutex_t debug_hd_mutex = PTHREAD_MUTEX_INITIALIZER;
extern Fl_Double_Window *fl_digi_main;
extern void update_main_title();
2008-08-26 22:33:34 +00:00
using namespace std;
#define MAX_LINES 65536
static FILE* wfile = 0;
static FILE* rfile = 0;
2008-08-26 22:33:34 +00:00
static int rfd;
static bool tty;
static Fl_Double_Window* window;
static Fl_Browser* btext;
static string linebuf;
2008-08-26 22:33:34 +00:00
debug* debug::inst = 0;
debug::level_e debug::level = debug::INFO_LEVEL;
2009-01-05 16:00:01 +00:00
uint32_t debug::mask = ~0u;
2008-08-26 22:33:34 +00:00
bool debug_pskmail = false;
bool debug_audio = false;
static const char* prefix[] = {
_("Quiet"), _("Error"), _("Warning"), _("Info"), _("Verbose"), _("Debug")
};
2008-08-26 22:33:34 +00:00
static void slider_cb(Fl_Widget* w, void*);
2009-01-05 16:00:01 +00:00
static void src_menu_cb(Fl_Widget* w, void*);
static void clear_cb(Fl_Widget *w, void*);
2009-01-05 16:00:01 +00:00
Fl_Menu_Item src_menu[] = {
{ _("ARQ control"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
2009-01-05 16:00:01 +00:00
{ _("Audio"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
{ _("Modem"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
{ _("Rig control"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
{ _("Flrig I/O"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
2009-01-05 16:00:01 +00:00
{ _("RPC"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
KISS interface * New configuration panel 'IO' * Add power density zones. * Query and Set Kiss hardware commands * Support bcast rsid over kiss socket * limit selectable modem options to 8 bit versions only (kiss i/o mode) * Employs simple HDLC encapsulation frame for transmit/receive CRC data verification. * Uses histogram based threshold detection. * Include manual squelch button for KISS op's. * User configurable inhibit transmit on busy frequencies for nn seconds * Impose transmit wait on valid frame reception * Uses dual UDP ports for concurrent operation of programs using the same IP address. * Clears the transmit buffer when data is available for more then 10 mins and has not been transmitted. * Provides ax25 decoding. Code hijacked from the FLDIGI packet modem co - redirect character data display on the RX panel * Add non standard KISS frame to handle ARQ data. - interface can handle both ARQ data and HDLC ax25 frames on the same port. * Squelch related command and control functions to the KISS hardware interface. * User configuration item to disable CSMA from config panel and kiss hardware frame. * Additional KISS command to adjust KPSQL detection gain (for testing). * Removed unused function in sockets.cxx not compatible with LINUX OS. (from John, G8BPQ) * New command 'BUSY:<T|F>' * Custom bindUDP() for UDP support. * Waterfall display gain adjusted based on sample rate changes. * Expanded conditions that indicate BUSY:T on command request. * Log loopback condition to event log. * Restore KPQSL set level position on start up. * Conditionally log of the KISS data path strings * Added debug filter 'KISS control' to event log. * KPSQL threshold button indicator remains ON during transmit inhibit. indication of active 'Busy Channel'. * Added DEBUG Logging command LOG_HEX(const char *buf, int len); D:,... ReadFromHostSocket: ..BUSY:. C0 06 42 55 53 59 3A C0 * Added thread locks to waterfall.cxx: - powerDensity() - peakFreq(), - powerDensityMaximum() These are being accessed by more then one thread. * Swapped hash.m4 swapped test case to std::tr1::hash from std::hash - Updated pskrep.cxx to handle the change in hash.m4 * 8PSK2000 removed. * Coordination merge with branch kiss21. * Code clean up * Updated Documentation. Remove unused png's. * Request from Jim, W2XO: Add log.set_rst_in and log.set_rst_out xmlrpc commands. * Compiler warning pacification (global in scope). * New XMLRPC commands to query and set active IO port. * Documentation update for XMLRPC commands * WSAStartup and WSACleanup one for one issue. * Update Docs for 3.22 release. New png's added.
2014-10-13 12:21:30 +00:00
{ _("Spotter"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
{ _("KISS control"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
{ _("Mac Logger"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE | FL_MENU_DIVIDER },
2009-01-05 16:00:01 +00:00
{ _("Other"), 0, 0, 0, FL_MENU_TOGGLE | FL_MENU_VALUE },
{ 0 }
};
#include <iostream>
void debug::rotate_log(const char* filename)
{
const int n = 5; // rename existing log files to keep up to 5 old versions
ostringstream oldfn, newfn;
ostringstream::streampos p;
oldfn << filename << '.';
newfn << filename << '.';
p = oldfn.tellp();
for (int i = n - 1; i > 0; i--) {
oldfn.seekp(p);
newfn.seekp(p);
oldfn << i;
newfn << i + 1;
rename(oldfn.str().c_str(), newfn.str().c_str());
}
rename(filename, oldfn.str().c_str());
}
2008-08-26 22:33:34 +00:00
void debug::start(const char* filename)
{
if (debug::inst)
return;
rotate_log(filename);
2008-08-26 22:33:34 +00:00
inst = new debug(filename);
2015-09-19 11:32:36 +00:00
window = new Fl_Double_Window(800, 400, _("Event log"));
2008-08-26 22:33:34 +00:00
window->xclass(PACKAGE_TARNAME);
int pad = 2;
Fl_Menu_Button* button = new Fl_Menu_Button(pad, pad, 128, 22, _("Log sources"));
2009-01-05 16:00:01 +00:00
button->menu(src_menu);
button->callback(src_menu_cb);
Fl_Slider* slider = new Fl_Slider(button->x() + button->w() + pad, pad, 128, 22, prefix[level]);
slider->tooltip(_("Change log level"));
2008-08-26 22:33:34 +00:00
slider->align(FL_ALIGN_RIGHT);
slider->type(FL_HOR_NICE_SLIDER);
slider->range(0.0, LOG_NLEVELS - 1);
2008-08-26 22:33:34 +00:00
slider->step(1.0);
slider->value(level);
slider->callback(slider_cb);
Fl_Button* clearbtn = new Fl_Button(window->w() - 64, pad, 60, 22, "clear");
clearbtn->callback(clear_cb);
btext = new Fl_Browser(pad, slider->h()+pad, window->w()-2*pad, window->h()-slider->h()-2*pad, 0);
btext->textfont(FL_HELVETICA);
btext->textsize(14);
window->resizable(btext);
2008-08-26 22:33:34 +00:00
window->end();
}
void debug::stop(void)
{
guard_lock debug_lock(&debug_mutex);
if (window) {
window->hide();
delete window;
window = 0;
}
if (inst) {
delete inst;
inst = 0;
}
2008-08-26 22:33:34 +00:00
}
static char fmt[1024];
static char dtext[16384];
2008-08-26 22:33:34 +00:00
void debug::log(level_e level, const char* func, const char* srcf, int line, const char* format, ...)
{
guard_lock debug_lock(&debug_mutex);
2008-08-26 22:33:34 +00:00
if (!inst)
return;
if (unlikely(debug::level == DEBUG_LEVEL) ||
debug_pskmail || debug_audio) {
2008-08-26 22:33:34 +00:00
time_t t = time(NULL);
struct tm stm;
(void)localtime_r(&t, &stm);
snprintf(fmt, sizeof(fmt), "%c: [%02d:%02d:%02d] %s:%d: %s\n",
KISS interface * New configuration panel 'IO' * Add power density zones. * Query and Set Kiss hardware commands * Support bcast rsid over kiss socket * limit selectable modem options to 8 bit versions only (kiss i/o mode) * Employs simple HDLC encapsulation frame for transmit/receive CRC data verification. * Uses histogram based threshold detection. * Include manual squelch button for KISS op's. * User configurable inhibit transmit on busy frequencies for nn seconds * Impose transmit wait on valid frame reception * Uses dual UDP ports for concurrent operation of programs using the same IP address. * Clears the transmit buffer when data is available for more then 10 mins and has not been transmitted. * Provides ax25 decoding. Code hijacked from the FLDIGI packet modem co - redirect character data display on the RX panel * Add non standard KISS frame to handle ARQ data. - interface can handle both ARQ data and HDLC ax25 frames on the same port. * Squelch related command and control functions to the KISS hardware interface. * User configuration item to disable CSMA from config panel and kiss hardware frame. * Additional KISS command to adjust KPSQL detection gain (for testing). * Removed unused function in sockets.cxx not compatible with LINUX OS. (from John, G8BPQ) * New command 'BUSY:<T|F>' * Custom bindUDP() for UDP support. * Waterfall display gain adjusted based on sample rate changes. * Expanded conditions that indicate BUSY:T on command request. * Log loopback condition to event log. * Restore KPQSL set level position on start up. * Conditionally log of the KISS data path strings * Added debug filter 'KISS control' to event log. * KPSQL threshold button indicator remains ON during transmit inhibit. indication of active 'Busy Channel'. * Added DEBUG Logging command LOG_HEX(const char *buf, int len); D:,... ReadFromHostSocket: ..BUSY:. C0 06 42 55 53 59 3A C0 * Added thread locks to waterfall.cxx: - powerDensity() - peakFreq(), - powerDensityMaximum() These are being accessed by more then one thread. * Swapped hash.m4 swapped test case to std::tr1::hash from std::hash - Updated pskrep.cxx to handle the change in hash.m4 * 8PSK2000 removed. * Coordination merge with branch kiss21. * Code clean up * Updated Documentation. Remove unused png's. * Request from Jim, W2XO: Add log.set_rst_in and log.set_rst_out xmlrpc commands. * Compiler warning pacification (global in scope). * New XMLRPC commands to query and set active IO port. * Documentation update for XMLRPC commands * WSAStartup and WSACleanup one for one issue. * Update Docs for 3.22 release. New png's added.
2014-10-13 12:21:30 +00:00
*prefix[level], stm.tm_hour, stm.tm_min, stm.tm_sec, srcf, line, format);
2008-08-26 22:33:34 +00:00
}
else
snprintf(fmt, sizeof(fmt), "%c: %s: %s\n", *prefix[level], func, format);
va_list args;
va_start(args, format);
intptr_t nt = vsnprintf(dtext, sizeof(dtext), fmt, args);
2008-08-26 22:33:34 +00:00
va_end(args);
fprintf(wfile, "%s", dtext);
if (tty) {
if (level <= DEBUG_LEVEL && level > QUIET_LEVEL) {
fprintf(stderr, "%s", dtext);
}
}
2009-04-17 18:17:55 +00:00
#ifdef __MINGW32__
fflush(wfile);
#endif
2008-08-26 22:33:34 +00:00
linebuf.append(dtext);
Fl::awake(sync_text, (void*)nt);
2008-08-26 22:33:34 +00:00
}
KISS interface * New configuration panel 'IO' * Add power density zones. * Query and Set Kiss hardware commands * Support bcast rsid over kiss socket * limit selectable modem options to 8 bit versions only (kiss i/o mode) * Employs simple HDLC encapsulation frame for transmit/receive CRC data verification. * Uses histogram based threshold detection. * Include manual squelch button for KISS op's. * User configurable inhibit transmit on busy frequencies for nn seconds * Impose transmit wait on valid frame reception * Uses dual UDP ports for concurrent operation of programs using the same IP address. * Clears the transmit buffer when data is available for more then 10 mins and has not been transmitted. * Provides ax25 decoding. Code hijacked from the FLDIGI packet modem co - redirect character data display on the RX panel * Add non standard KISS frame to handle ARQ data. - interface can handle both ARQ data and HDLC ax25 frames on the same port. * Squelch related command and control functions to the KISS hardware interface. * User configuration item to disable CSMA from config panel and kiss hardware frame. * Additional KISS command to adjust KPSQL detection gain (for testing). * Removed unused function in sockets.cxx not compatible with LINUX OS. (from John, G8BPQ) * New command 'BUSY:<T|F>' * Custom bindUDP() for UDP support. * Waterfall display gain adjusted based on sample rate changes. * Expanded conditions that indicate BUSY:T on command request. * Log loopback condition to event log. * Restore KPQSL set level position on start up. * Conditionally log of the KISS data path strings * Added debug filter 'KISS control' to event log. * KPSQL threshold button indicator remains ON during transmit inhibit. indication of active 'Busy Channel'. * Added DEBUG Logging command LOG_HEX(const char *buf, int len); D:,... ReadFromHostSocket: ..BUSY:. C0 06 42 55 53 59 3A C0 * Added thread locks to waterfall.cxx: - powerDensity() - peakFreq(), - powerDensityMaximum() These are being accessed by more then one thread. * Swapped hash.m4 swapped test case to std::tr1::hash from std::hash - Updated pskrep.cxx to handle the change in hash.m4 * 8PSK2000 removed. * Coordination merge with branch kiss21. * Code clean up * Updated Documentation. Remove unused png's. * Request from Jim, W2XO: Add log.set_rst_in and log.set_rst_out xmlrpc commands. * Compiler warning pacification (global in scope). * New XMLRPC commands to query and set active IO port. * Documentation update for XMLRPC commands * WSAStartup and WSACleanup one for one issue. * Update Docs for 3.22 release. New png's added.
2014-10-13 12:21:30 +00:00
void debug::hex_dump(const char* func, const char * data, int length)
{
guard_lock debug_lock(&debug_hd_mutex);
char cbuff[32];
char hbuff[64];
char tbuff[32];
int index = 0;
int data_index = 0;
int count = length;
unsigned int c = 0;
int hi = 0;
int step = 16;
if(!func)
func = "Unassigned";
if(!data || length < 0) return;
while(count > 0) {
memset(cbuff, 0, sizeof(cbuff));
memset(hbuff, 0, sizeof(hbuff));
memset(tbuff, 0, sizeof(tbuff));
hi = 0;
for(index = 0; index < step; index++) {
if(data_index < length) {
c = ((unsigned int) data[data_index]) & 0xFF;
if(c >= ' ' && c <= 0xff) {
cbuff[index] = c;
} else {
cbuff[index] = '.';
}
snprintf(tbuff, sizeof(tbuff) - 1, "%02X", c);
hbuff[hi++] = tbuff[0];
hbuff[hi++] = tbuff[1];
hbuff[hi++] = ' ';
} else {
break;
}
data_index++;
}
LOG_DEBUG("%s: %s %s", func, cbuff, hbuff);
count -= step;
}
}
2008-08-26 22:33:34 +00:00
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));
2008-08-26 22:33:34 +00:00
}
void debug::show(void)
{
2015-09-19 11:32:36 +00:00
btext->bottomline(btext->size());
2008-08-26 22:33:34 +00:00
window->show();
}
2009-04-17 18:17:55 +00:00
void debug::sync_text(void* arg)
2008-08-26 22:33:34 +00:00
{
guard_lock debug_lock(&debug_mutex);
size_t p1 = 0, p2 = linebuf.find("\n");
while (p2 != string::npos) {
btext->add(linebuf.substr(p1, p2 - p1).c_str());
p1 = p2 + 1;
p2 = linebuf.find("\n", p1);
2008-08-26 22:33:34 +00:00
}
btext->redraw();
btext->bottomline(btext->size());
linebuf.clear();
return;
2008-08-26 22:33:34 +00:00
}
debug::debug(const char* filename)
{
if ((wfile = fopen(filename, "w")) == NULL)
throw strerror(errno);
2009-04-17 18:17:55 +00:00
setvbuf(wfile, (char*)NULL, _IOLBF, 0);
2008-09-02 15:04:43 +00:00
set_cloexec(fileno(wfile), 1);
2008-08-26 22:33:34 +00:00
if ((rfile = fopen(filename, "r")) == NULL)
throw strerror(errno);
rfd = fileno(rfile);
2008-09-02 15:04:43 +00:00
set_cloexec(rfd, 1);
2009-04-17 18:17:55 +00:00
#ifndef __MINGW32__
2008-08-26 22:33:34 +00:00
int f;
if ((f = fcntl(rfd, F_GETFL)) == -1)
throw strerror(errno);
if (fcntl(rfd, F_SETFL, f | O_NONBLOCK) == -1)
throw strerror(errno);
2009-04-17 18:17:55 +00:00
#endif
2008-09-02 15:04:43 +00:00
tty = isatty(fileno(stderr));
linebuf.clear();
2008-08-26 22:33:34 +00:00
}
debug::~debug()
{
if (wfile) fclose(wfile);
if (rfile) fclose(rfile);
2008-08-26 22:33:34 +00:00
}
static void slider_cb(Fl_Widget* w, void*)
{
2009-01-05 16:00:01 +00:00
debug::level = (debug::level_e)((Fl_Slider*)w)->value();
w->label(prefix[debug::level]);
w->parent()->redraw();
}
static void src_menu_cb(Fl_Widget* w, void*)
{
debug::mask ^= 1 << ((Fl_Menu_*)w)->value();
2008-08-26 22:33:34 +00:00
}
static void clear_cb(Fl_Widget* w, void*)
{
guard_lock debug_lock(&debug_mutex);
btext->clear();
linebuf.clear();
}