Change default bandwidth to 25 MHz to match maximum USRP2 bandwidth
[debian/gnuradio] / gnuradio-core / src / lib / io / gri_logger.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #if 0 // This needs reimplementation with boost threads and synchronization
28
29 #include <gri_logger.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <stdexcept>
33 #include <boost/weak_ptr.hpp>
34 #include <string.h>
35
36
37 /*
38  * This class creates the thread that reads from the ringbuffer and
39  * and writes to the file.  This is opaque to the user.
40  */
41 class gri_log_poster : public omni_thread
42 {
43   FILE                 *d_fp;
44   gr_buffer_sptr        d_writer;
45   gr_buffer_reader_sptr d_reader;
46   omni_semaphore        d_ringbuffer_ready;
47   volatile bool         d_time_to_die;
48   volatile bool         d_writer_overrun;
49
50   virtual void* run_undetached(void * arg);
51
52 public:
53   gri_log_poster(const char *filename);
54   ~gri_log_poster();
55
56   void kill() { d_time_to_die = true; post(); }
57   gr_buffer_sptr writer() const { return d_writer; }
58   void post() { d_ringbuffer_ready.post(); }
59   void note_writer_overrun() { d_writer_overrun = true; }
60 };
61
62 gri_log_poster::gri_log_poster(const char *filename)
63   : omni_thread(),
64     d_ringbuffer_ready(1, 1),           // binary semaphore
65     d_time_to_die(false),
66     d_writer_overrun(false)
67 {
68   if ((d_fp = fopen(filename, "w")) == 0){
69     perror (filename);
70     throw std::runtime_error("can't open file");
71   }
72
73   // Create a 1MB buffer.
74   d_writer = gr_make_buffer(1 * 1024 * 1024, sizeof(unsigned char));
75   d_reader = gr_buffer_add_reader(d_writer, 0);
76
77   start_undetached();  // start the thread
78 }
79
80 gri_log_poster::~gri_log_poster()
81 {
82   if (d_fp != 0){
83     fclose(d_fp);
84     d_fp = 0;
85   }
86 }
87
88 /*
89  * This is the body of the logging thread.
90  */
91 void *
92 gri_log_poster::run_undetached(void *arg)
93 {
94   int nbytes;
95
96   //fprintf(stderr, "Enter: run_undetached!\n");
97
98   while (!d_time_to_die){
99     while ((nbytes = d_reader->items_available()) > 0){
100       fwrite(d_reader->read_pointer(), 1, nbytes, d_fp);
101       d_reader->update_read_pointer(nbytes);
102     }
103     fflush(d_fp);
104     d_ringbuffer_ready.wait();
105
106     if (d_writer_overrun){
107       fputs(">>>>> gri_logger: writer overrun.  Info lost <<<<<\n", d_fp);
108       d_writer_overrun = false;
109     }
110   }
111
112   // fprintf(stderr, "Exit: run_undetached!\n");
113   return 0;
114 }
115
116 // ------------------------------------------------------------------------
117
118 static boost::weak_ptr<gri_logger> s_singleton;  // weak pointer IQ test ;-)
119 static omni_mutex s_singleton_mutex;
120
121 gri_logger_sptr
122 gri_logger::singleton()
123 {
124   omni_mutex_lock l(s_singleton_mutex);
125   gri_logger_sptr r;
126
127   if (r = s_singleton.lock())
128     return r;
129
130   r = gri_logger_sptr(new gri_logger("gri_logger.log"));
131   s_singleton = r;
132   return r;
133 }
134   
135
136 gri_logger::gri_logger(const char *filename)
137 {
138   d_poster = new gri_log_poster(filename);
139 }
140
141 gri_logger::~gri_logger()
142 {
143   d_poster->kill();
144   d_poster->join(NULL);
145 }
146
147 void
148 gri_logger::write(const void *buf, size_t count)
149 {
150   omni_mutex_lock l(d_write_mutex);
151   gr_buffer_sptr writer = d_poster->writer();
152   
153   // either write it all, or drop it on the ground
154   if (count <= (size_t) writer->space_available()){
155     memcpy(writer->write_pointer(), buf, count);
156     writer->update_write_pointer(count);
157     d_poster->post();
158   }
159   else {
160     d_poster->note_writer_overrun();
161   }
162 }
163
164 void
165 gri_logger::printf(const char *format, ...)
166 {
167   va_list       ap;
168   char          buf[4096];
169   int           n;
170   
171   va_start(ap, format);
172   n = vsnprintf(buf, sizeof(buf), format, ap);
173   va_end(ap);
174   if (n > -1 && n < (ssize_t) sizeof(buf))
175     write(buf, n);
176 }
177
178 #endif