From fd945013143466e26f0d481d675401fa745716ba Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 6 Jan 2010 23:48:32 +0000 Subject: Fancy (optinally) coloured logging system. Do all logging output via Raul logging streams. git-svn-id: http://svn.drobilla.net/lad/trunk/raul@2348 a436a847-0d15-0410-975c-d299462d15a1 --- src/SMFReader.cpp | 13 +++------ src/SMFWriter.cpp | 8 +++--- src/Thread.cpp | 22 +++++++------- src/log.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 src/log.cpp (limited to 'src') 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 #include #include -#include #include +#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 #include #include -#include #include +#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 #include +#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 + * + * 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 -- cgit v1.2.1