~ubuntu-branches/ubuntu/wily/flrig/wily

« back to all changes in this revision

Viewing changes to .pc/0001-License-Declaration.patch/src/debug.cxx

  • Committer: Package Import Robot
  • Author(s): Kamal Mostafa
  • Date: 2014-10-25 11:17:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20141025111710-n32skgya3l9u1brw
Tags: 1.3.17-1
* New upstream release (Closes: #761839)
* Debian Standards-Version: 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ----------------------------------------------------------------------------
2
 
//      debug.cxx
3
 
//
4
 
// Copyright (C) 2008
5
 
//              Stelios Bounanos, M0GLD
6
 
//
7
 
// This file is part of fldigi.
8
 
//
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.
13
 
//
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.
18
 
//
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
 
// ----------------------------------------------------------------------------
22
 
 
23
 
#include <cstdio>
24
 
#include <cstring>
25
 
#include <cstdarg>
26
 
#include <string>
27
 
#include <iostream>
28
 
 
29
 
#include <unistd.h>
30
 
#include <fcntl.h>
31
 
#include <errno.h>
32
 
#include <time.h>
33
 
 
34
 
#include <FL/Fl.H>
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>
40
 
 
41
 
#include <FL/Fl_Browser.H>
42
 
 
43
 
#include "debug.h"
44
 
#include "icons.h"
45
 
#include "gettext.h"
46
 
#include "rig.h"
47
 
 
48
 
using namespace std;
49
 
 
50
 
#define MAX_LINES 65536
51
 
 
52
 
static FILE* wfile;
53
 
static FILE* rfile;
54
 
static int rfd;
55
 
static bool tty;
56
 
 
57
 
static Fl_Double_Window*        window;
58
 
static Fl_Browser*                      btext;
59
 
static string buffer;
60
 
 
61
 
debug* debug::inst = 0;
62
 
debug::level_e debug::level = debug::ERROR_LEVEL;
63
 
uint32_t debug::mask = ~0u;
64
 
 
65
 
const char* prefix[] = { _("Quiet"), _("Error"), _("Warning"), _("Info"), _("Debug") };
66
 
 
67
 
static void slider_cb(Fl_Widget* w, void*);
68
 
static void clear_cb(Fl_Widget *w, void*);
69
 
 
70
 
void debug::start(const char* filename)
71
 
{
72
 
        if (debug::inst)
73
 
                return;
74
 
        inst = new debug(filename);
75
 
 
76
 
        window = new Fl_Double_Window(600, 256, _("Event log"));
77
 
 
78
 
        int pad = 2;
79
 
 
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);
85
 
        slider->step(1.0);
86
 
        slider->value(level);
87
 
        slider->callback(slider_cb);
88
 
 
89
 
        Fl_Button* clearbtn = new Fl_Button(window->w() - 60, pad, 60, 20, "clear");
90
 
        clearbtn->callback(clear_cb);
91
 
 
92
 
        btext = new Fl_Browser(pad,  slider->h()+pad, window->w()-2*pad, window->h()-slider->h()-2*pad, 0);
93
 
        window->resizable(btext);
94
 
 
95
 
        buffer.clear();
96
 
 
97
 
        window->end();
98
 
}
99
 
 
100
 
void debug::stop(void)
101
 
{
102
 
        delete inst;
103
 
        inst = 0;
104
 
        delete window;
105
 
}
106
 
 
107
 
static char fmt[1024];
108
 
static char sztemp[1024];
109
 
static string estr = "";
110
 
bool   debug_in_use = false;
111
 
 
112
 
void debug::log(level_e level, const char* func, const char* srcf, int line, const char* format, ...)
113
 
{
114
 
        if (!inst) return;
115
 
        if (level > debug::level) return;
116
 
 
117
 
        snprintf(fmt, sizeof(fmt), "%c: %s: %s\n", *prefix[level], func, format);
118
 
 
119
 
    while(debug_in_use) MilliSleep(1);
120
 
    
121
 
        va_list args;
122
 
        va_start(args, format);
123
 
 
124
 
        vsnprintf(sztemp, sizeof(sztemp), fmt, args);
125
 
 
126
 
        estr.append(sztemp);
127
 
        fprintf(wfile, "%s", sztemp);
128
 
 
129
 
        va_end(args);
130
 
 
131
 
        fflush(wfile);
132
 
 
133
 
    Fl::awake(sync_text, 0);
134
 
}
135
 
 
136
 
void debug::slog(level_e level, const char* func, const char* srcf, int line, const char* format, ...)
137
 
{
138
 
        if (!inst) return;
139
 
        if (level > debug::level) return;
140
 
 
141
 
        snprintf(fmt, sizeof(fmt), "%c:%s\n", *prefix[level], format);
142
 
 
143
 
    while(debug_in_use) MilliSleep(1);
144
 
    
145
 
        va_list args;
146
 
        va_start(args, format);
147
 
 
148
 
        vsnprintf(sztemp, sizeof(sztemp), fmt, args);
149
 
        estr.append(sztemp);
150
 
        fprintf(wfile, "%s", sztemp);
151
 
        va_end(args);
152
 
 
153
 
        fflush(wfile);
154
 
 
155
 
    Fl::awake(sync_text, 0);
156
 
}
157
 
 
158
 
void debug::elog(const char* func, const char* srcf, int line, const char* text)
159
 
{
160
 
        if (level > debug::level) return;
161
 
        log(ERROR_LEVEL, func, srcf, line, "%s: %s", text, strerror(errno));
162
 
}
163
 
 
164
 
void debug::show(void)
165
 
{
166
 
        window->show();
167
 
}
168
 
 
169
 
void debug::sync_text(void* arg)
170
 
{
171
 
    debug_in_use = true;
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");
176
 
                p0 = p1 + 1;
177
 
                p1 = estr.find('\n', p0);
178
 
        }
179
 
    estr = "";
180
 
    debug_in_use = false;
181
 
}
182
 
 
183
 
debug::debug(const char* filename)
184
 
{
185
 
        if ((wfile = fopen(filename, "w")) == NULL)
186
 
                throw strerror(errno);
187
 
        setvbuf(wfile, (char*)NULL, _IOLBF, 0);
188
 
        set_cloexec(fileno(wfile), 1);
189
 
 
190
 
        if ((rfile = fopen(filename, "r")) == NULL)
191
 
                throw strerror(errno);
192
 
        rfd = fileno(rfile);
193
 
        set_cloexec(rfd, 1);
194
 
#ifndef __WIN32__
195
 
        int f;
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);
200
 
#endif
201
 
        tty = isatty(fileno(stderr));
202
 
}
203
 
 
204
 
debug::~debug()
205
 
{
206
 
        fclose(wfile);
207
 
        fclose(rfile);
208
 
}
209
 
 
210
 
static void slider_cb(Fl_Widget* w, void*)
211
 
{
212
 
        debug::level = (debug::level_e)((Fl_Slider*)w)->value();
213
 
        w->label(prefix[debug::level]);
214
 
        w->parent()->redraw();
215
 
}
216
 
 
217
 
static void clear_cb(Fl_Widget* w, void*)
218
 
{
219
 
        btext->clear();
220
 
        buffer.clear();
221
 
}
222