summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--raul/Atom.hpp14
-rw-r--r--raul/AtomLiblo.hpp3
-rw-r--r--raul/AtomRDF.hpp4
-rw-r--r--raul/Process.hpp3
-rw-r--r--raul/RingBuffer.hpp3
-rw-r--r--raul/TableImpl.hpp6
-rw-r--r--raul/log.hpp94
-rw-r--r--src/SMFReader.cpp13
-rw-r--r--src/SMFWriter.cpp8
-rw-r--r--src/Thread.cpp22
-rw-r--r--src/log.cpp85
-rw-r--r--wscript12
12 files changed, 233 insertions, 34 deletions
diff --git a/raul/Atom.hpp b/raul/Atom.hpp
index 7581e46..4dfb723 100644
--- a/raul/Atom.hpp
+++ b/raul/Atom.hpp
@@ -210,4 +210,18 @@ static inline std::ostream& operator<<(std::ostream& os, const Raul::Atom& atom)
return os;
}
+static inline std::ostream& operator<<(std::ostream& os, Raul::Atom::Type type)
+{
+ switch (type) {
+ case Raul::Atom::NIL: return os << "Nil";
+ case Raul::Atom::INT: return os << "Int32";
+ case Raul::Atom::FLOAT: return os << "Float";
+ case Raul::Atom::BOOL: return os << "Bool";
+ case Raul::Atom::URI: return os << "URI";
+ case Raul::Atom::STRING: return os << "String";
+ case Raul::Atom::BLOB: return os << "Blob";
+ }
+ return os;
+}
+
#endif // RAUL_ATOM_HPP
diff --git a/raul/AtomLiblo.hpp b/raul/AtomLiblo.hpp
index 7494ae4..fe6888a 100644
--- a/raul/AtomLiblo.hpp
+++ b/raul/AtomLiblo.hpp
@@ -20,6 +20,7 @@
#include <iostream>
#include <lo/lo.h>
+#include "raul/log.hpp"
#include "raul/Atom.hpp"
namespace Raul {
@@ -85,7 +86,7 @@ lo_arg_to_atom(char type, lo_arg* arg)
case 'F':
return Atom((bool)false);
default:
- std::cerr << "WARNING: Unable to convert OSC type '"
+ warn << "Unable to convert OSC type '"
<< type << "' to Atom" << std::endl;
return Atom();
}
diff --git a/raul/AtomRDF.hpp b/raul/AtomRDF.hpp
index 3c3373d..0b961d2 100644
--- a/raul/AtomRDF.hpp
+++ b/raul/AtomRDF.hpp
@@ -22,7 +22,7 @@
#include <string>
#include <sstream>
#include <cmath>
-
+#include "raul/log.hpp"
#include "raul/Atom.hpp"
#include "redlandmm/Node.hpp"
#include "redlandmm/World.hpp"
@@ -101,7 +101,7 @@ atom_to_node(Redland::World& world, const Atom& atom)
case Atom::BLOB:
case Atom::NIL:
default:
- //std::cerr << "WARNING: Unserializable Atom!" << std::endl;
+ warn << "Unserializable Atom" << std::endl;
break;
}
diff --git a/raul/Process.hpp b/raul/Process.hpp
index 675f1d7..719a9d4 100644
--- a/raul/Process.hpp
+++ b/raul/Process.hpp
@@ -24,6 +24,7 @@
#include <sys/time.h>
#include <sys/resource.h>
#include <boost/utility.hpp>
+#include "raul/log.hpp"
namespace Raul {
@@ -47,7 +48,7 @@ public:
const std::string arguments = command.substr((command.find(" ") + 1));
- std::cerr << "Launching child process '" << executable << "' with arguments '"
+ info << "Launching child process '" << executable << "' with arguments '"
<< arguments << "'" << std::endl;
// Use the same double fork() trick as JACK to prevent zombie children
diff --git a/raul/RingBuffer.hpp b/raul/RingBuffer.hpp
index 2c937cf..23d4d90 100644
--- a/raul/RingBuffer.hpp
+++ b/raul/RingBuffer.hpp
@@ -22,6 +22,7 @@
#include <cstring>
#include <iostream>
#include <glib.h>
+#include "raul/log.hpp"
namespace Raul {
@@ -189,7 +190,7 @@ bool
RingBuffer<T>::skip(size_t size)
{
if (read_space() < size) {
- std::cerr << "WARNING: Attempt to skip past end of MIDI ring buffer" << std::endl;
+ warn << "Attempt to skip past end of MIDI ring buffer" << std::endl;
return false;
}
diff --git a/raul/TableImpl.hpp b/raul/TableImpl.hpp
index 81971d6..0b32a29 100644
--- a/raul/TableImpl.hpp
+++ b/raul/TableImpl.hpp
@@ -241,12 +241,6 @@ Table<K, T>::cram(const Table<K,T>& range)
_entries = new_entries;
- /*std::cerr << "********************************\n";
- for (size_t i=0; i < size(); ++i) {
- std::cerr << _entries[i].first << std::endl;
- }
- std::cerr << "********************************\n";*/
-
assert(size() == orig_size + range.size());
#ifdef TABLE_SORT_DEBUG
assert(is_sorted());
diff --git a/raul/log.hpp b/raul/log.hpp
new file mode 100644
index 0000000..17d728f
--- /dev/null
+++ b/raul/log.hpp
@@ -0,0 +1,94 @@
+/* This file is part of Raul.
+ * Copyright (C) 2009 Dave Robillard <http://drobilla.net>
+ *
+ * Raul 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 2 of the License, or (at your option) any later
+ * version.
+ *
+ * Raul 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 details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RAUL_LOG_HPP
+#define RAUL_LOG_HPP
+
+#include <iostream>
+#include <sstream>
+
+namespace Raul {
+
+class LogBuffer : public std::streambuf
+{
+public:
+ enum Colour {
+ DEFAULT = 0,
+ RED = 31,
+ GREEN,
+ YELLOW,
+ BLUE,
+ MAGENTA,
+ CYAN,
+ WHITE
+ };
+
+ LogBuffer(const char* prefix="", Colour colour=DEFAULT)
+ : _prefix(prefix)
+ , _colour(colour)
+ , _out(std::cout)
+ {}
+
+ /** Change the colour of the output, e.g. out << colour(RED) << "red" << endl; */
+ const char* colour(Colour c);
+
+ /** Reset the colour of the output, e.g. out << plain() << "plain" << endl; */
+ const char* plain();
+
+protected:
+ int_type overflow(int_type c) {
+ if (c == '\n')
+ emit();
+ else if (c != traits_type::eof())
+ _line += c;
+
+ return c;
+ }
+
+ int sync() {
+ if (!_line.empty())
+ emit();
+ return 0;
+ }
+
+private:
+ void emit();
+
+ const char* _prefix;
+ Colour _colour;
+ std::string _line;
+ std::ostream& _out;
+};
+
+
+class NullBuffer : public std::streambuf
+{
+protected:
+ int_type overflow(int_type c) { return c; }
+ int sync() { return 0; }
+};
+
+
+extern std::ostream info;
+extern std::ostream warn;
+extern std::ostream error;
+extern std::ostream debug;
+
+
+} // namespace Raul
+
+#endif // RAUL_LOG_HPP
diff --git a/src/SMFReader.cpp b/src/SMFReader.cpp
index e7866a2..55d4020 100644
--- a/src/SMFReader.cpp
+++ b/src/SMFReader.cpp
@@ -18,8 +18,8 @@
#include <cstdio>
#include <cassert>
#include <cstring>
-#include <iostream>
#include <glib.h>
+#include "raul/log.hpp"
#include "raul/SMFReader.hpp"
#include "raul/midi_events.h"
@@ -95,7 +95,7 @@ SMFReader::open(const std::string& filename) throw (logic_error, UnsupportedTime
if (_fd)
throw logic_error("Attempt to start new read while write in progress.");
- cout << "Opening SMF file " << filename << " for reading." << endl;
+ info << "Opening SMF file " << filename << " for reading." << endl;
_fd = fopen(filename.c_str(), "r+");
@@ -106,7 +106,7 @@ SMFReader::open(const std::string& filename) throw (logic_error, UnsupportedTime
mthd[4] = '\0';
fread(mthd, 1, 4, _fd);
if (strcmp(mthd, "MThd")) {
- cerr << filename << " is not an SMF file, aborting." << endl;
+ error << filename << " is not an SMF file, aborting." << endl;
fclose(_fd);
_fd = NULL;
return false;
@@ -166,7 +166,7 @@ SMFReader::seek_to_track(unsigned track) throw (std::logic_error)
if (!strcmp(id, "MTrk")) {
++track_pos;
} else {
- std::cerr << "Unknown chunk ID " << id << endl;
+ error << "Unknown chunk ID " << id << endl;
}
uint32_t chunk_size_be;
@@ -252,10 +252,6 @@ SMFReader::read_event(size_t buf_len,
throw PrematureEOF();
uint8_t type = fgetc(_fd);
const uint32_t size = read_var_len(_fd);
- /*cerr.flags(ios::hex);
- cerr << "SMF - meta 0x" << (int)type << ", size = ";
- cerr.flags(ios::dec);
- cerr << size << endl;*/
if ((uint8_t)type == 0x2F) {
return -1; // we hit the logical EOF anyway...
@@ -266,7 +262,6 @@ SMFReader::read_event(size_t buf_len,
}
if (*ev_size > buf_len || *ev_size == 0 || feof(_fd)) {
- //cerr << "SMF - Skipping event" << endl;
// Skip event, return 0
fseek(_fd, *ev_size - 1, SEEK_CUR);
return 0;
diff --git a/src/SMFWriter.cpp b/src/SMFWriter.cpp
index bca7c78..e07555b 100644
--- a/src/SMFWriter.cpp
+++ b/src/SMFWriter.cpp
@@ -19,8 +19,8 @@
#include <cassert>
#include <cstring>
#include <cstdio>
-#include <iostream>
#include <glib.h>
+#include "raul/log.hpp"
#include "raul/SMFWriter.hpp"
using namespace std;
@@ -68,7 +68,7 @@ SMFWriter::start(const std::string& filename,
if (_fd)
throw logic_error("Attempt to start new write while write in progress.");
- cerr << "Opening SMF file " << filename << " for writing." << endl;
+ info << "Opening SMF file " << filename << " for writing." << endl;
_fd = fopen(filename.c_str(), "w+");
@@ -155,7 +155,7 @@ SMFWriter::finish() throw (logic_error)
void
SMFWriter::write_header()
{
- cerr << "SMF Flushing header\n";
+ info << "SMF Flushing header\n";
assert(_fd);
@@ -181,7 +181,7 @@ SMFWriter::write_header()
void
SMFWriter::write_footer()
{
- cerr << "SMF - Writing EOT\n";
+ info << "Writing EOT\n";
fseek(_fd, 0, SEEK_END);
write_var_len(1); // whatever...
diff --git a/src/Thread.cpp b/src/Thread.cpp
index 0449515..8e37dfe 100644
--- a/src/Thread.cpp
+++ b/src/Thread.cpp
@@ -15,10 +15,12 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <iostream>
#include <cstring>
+#include "raul/log.hpp"
#include "raul/Thread.hpp"
+#define LOG(s) s << "[" << _name << "] "
+
using namespace std;
namespace Raul {
@@ -73,7 +75,7 @@ void
Thread::start()
{
if (!_pthread_exists) {
- cout << "[" << _name << " Thread] Starting." << endl;
+ LOG(info) << "Starting thread" << endl;
pthread_attr_t attr;
pthread_attr_init(&attr);
@@ -96,7 +98,7 @@ Thread::stop()
}
_pthread = NULL;
_pthread_exists = false;
- cout << "[" << _name << " Thread] Exiting." << endl;
+ LOG(info) << "Exiting thread" << endl;
}
}
@@ -107,16 +109,16 @@ Thread::set_scheduling(int policy, unsigned int priority)
sp.sched_priority = priority;
int result = pthread_setschedparam(_pthread, policy, &sp);
if (!result) {
- cout << "[" << _name << "] Set scheduling policy to ";
+ LOG(info) << "Set scheduling policy to ";
switch (policy) {
- case SCHED_FIFO: cout << "SCHED_FIFO"; break;
- case SCHED_RR: cout << "SCHED_RR"; break;
- case SCHED_OTHER: cout << "SCHED_OTHER"; break;
- default: cout << "UNKNOWN"; break;
+ case SCHED_FIFO: info << "SCHED_FIFO"; break;
+ case SCHED_RR: info << "SCHED_RR"; break;
+ case SCHED_OTHER: info << "SCHED_OTHER"; break;
+ default: info << "UNKNOWN"; break;
}
- cout << ", priority " << sp.sched_priority << endl;
+ info << ", priority " << sp.sched_priority << endl;
} else {
- cout << "[" << _name << "] Unable to set scheduling policy ("
+ LOG(info) << "Unable to set scheduling policy ("
<< strerror(result) << ")" << endl;
}
}
diff --git a/src/log.cpp b/src/log.cpp
new file mode 100644
index 0000000..e9a61b2
--- /dev/null
+++ b/src/log.cpp
@@ -0,0 +1,85 @@
+/* This file is part of Raul.
+ * Copyright (C) 2009 Dave Robillard <http://drobilla.net>
+ *
+ * Raul 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 2 of the License, or (at your option) any later
+ * version.
+ *
+ * Raul 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 details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "raul/log.hpp"
+#include "raul-config.h"
+
+namespace Raul {
+
+#ifdef LOG_COLOUR
+
+LogBuffer info_buffer("", LogBuffer::GREEN);
+LogBuffer warn_buffer("", LogBuffer::YELLOW);
+LogBuffer error_buffer("", LogBuffer::RED);
+#ifdef LOG_DEBUG
+LogBuffer debug_buffer("", LogBuffer::CYAN);
+#else
+NullBuffer debug_buffer;
+#endif
+
+#else // !LOG_COLOUR
+
+LogBuffer info_buffer("INFO: ");
+LogBuffer warn_buffer("WARNING: ");
+LogBuffer error_buffer("ERROR: ");
+#ifdef LOG_DEBUG
+LogBuffer debug_buffer("DEBUG: ");
+#else
+NullBuffer debug_buffer;
+#endif
+
+#endif // LOG_COLOUR
+
+std::ostream info(&info_buffer);
+std::ostream warn(&warn_buffer);
+std::ostream error(&error_buffer);
+std::ostream debug(&debug_buffer);
+
+
+const char*
+Raul::LogBuffer::colour(Colour c)
+{
+ std::stringstream ss;
+ ss << "\033[0;" << _colour << "m";
+ return ss.str().c_str();
+}
+
+
+const char*
+Raul::LogBuffer::plain()
+{
+ return "\033[0m";
+}
+
+
+void
+Raul::LogBuffer::emit()
+{
+ if (_colour != DEFAULT)
+ _out << colour(_colour);
+
+ _out << _prefix << _line;
+
+ if (_colour != DEFAULT)
+ _out << plain();
+
+ _out << std::endl;
+ _line.clear();
+}
+
+
+} // namespace Raul
diff --git a/wscript b/wscript
index 7b12b42..2a91358 100644
--- a/wscript
+++ b/wscript
@@ -28,6 +28,10 @@ def set_options(opt):
autowaf.set_options(opt)
opt.add_option('--test', action='store_true', default=False, dest='build_tests',
help="Build unit tests")
+ opt.add_option('--log-colour', action='store_true', default=True, dest='log_colour',
+ help="Coloured console/log output")
+ opt.add_option('--log-debug', action='store_true', default=False, dest='log_debug',
+ help="Print debugging output")
def configure(conf):
autowaf.configure(conf)
@@ -40,6 +44,13 @@ def configure(conf):
conf.env['BUILD_TESTS'] = Options.options.build_tests
+ if Options.options.log_colour:
+ conf.define('LOG_COLOUR', 1)
+ if Options.options.log_debug:
+ conf.define('LOG_DEBUG', 1)
+
+ conf.write_config_header('raul-config.h')
+
# Boost headers
autowaf.check_header(conf, 'boost/shared_ptr.hpp', mandatory=True)
autowaf.check_header(conf, 'boost/weak_ptr.hpp', mandatory=True)
@@ -67,6 +78,7 @@ def build(bld):
src/SMFWriter.cpp
src/Symbol.cpp
src/Thread.cpp
+ src/log.cpp
'''
obj.export_incdirs = ['.']
obj.includes = ['.', './src']