3 * Copyright 2006,2009 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
27 #if 0 // This needs reimplementation with boost threads and synchronization
29 #include <gri_logger.h>
33 #include <boost/weak_ptr.hpp>
38 * This class creates the thread that reads from the ringbuffer and
39 * and writes to the file. This is opaque to the user.
41 class gri_log_poster : public omni_thread
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;
50 virtual void* run_undetached(void * arg);
53 gri_log_poster(const char *filename);
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; }
62 gri_log_poster::gri_log_poster(const char *filename)
64 d_ringbuffer_ready(1, 1), // binary semaphore
66 d_writer_overrun(false)
68 if ((d_fp = fopen(filename, "w")) == 0){
70 throw std::runtime_error("can't open file");
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);
77 start_undetached(); // start the thread
80 gri_log_poster::~gri_log_poster()
89 * This is the body of the logging thread.
92 gri_log_poster::run_undetached(void *arg)
96 //fprintf(stderr, "Enter: run_undetached!\n");
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);
104 d_ringbuffer_ready.wait();
106 if (d_writer_overrun){
107 fputs(">>>>> gri_logger: writer overrun. Info lost <<<<<\n", d_fp);
108 d_writer_overrun = false;
112 // fprintf(stderr, "Exit: run_undetached!\n");
116 // ------------------------------------------------------------------------
118 static boost::weak_ptr<gri_logger> s_singleton; // weak pointer IQ test ;-)
119 static omni_mutex s_singleton_mutex;
122 gri_logger::singleton()
124 omni_mutex_lock l(s_singleton_mutex);
127 if (r = s_singleton.lock())
130 r = gri_logger_sptr(new gri_logger("gri_logger.log"));
136 gri_logger::gri_logger(const char *filename)
138 d_poster = new gri_log_poster(filename);
141 gri_logger::~gri_logger()
144 d_poster->join(NULL);
148 gri_logger::write(const void *buf, size_t count)
150 omni_mutex_lock l(d_write_mutex);
151 gr_buffer_sptr writer = d_poster->writer();
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);
160 d_poster->note_writer_overrun();
165 gri_logger::printf(const char *format, ...)
171 va_start(ap, format);
172 n = vsnprintf(buf, sizeof(buf), format, ap);
174 if (n > -1 && n < (ssize_t) sizeof(buf))