From fde5c6661d296a79d6d6a8a1bcd888d2182deb8d Mon Sep 17 00:00:00 2001 From: TheMarlboroMan Date: Mon, 27 Jan 2020 18:37:01 +0100 Subject: Small changes to source, added disposable logger, basic functionality, untested --- src/tracer.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/tracer.h (limited to 'src/tracer.h') diff --git a/src/tracer.h b/src/tracer.h new file mode 100644 index 0000000..e42c29e --- /dev/null +++ b/src/tracer.h @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * tracer.h + * + * Sun Dec 29 18:04:28 CET 2019 + * Copyright 2019 Daniel Pastor + * marlborometal@gmail.com + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DrumGizmo 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#pragma once + +#include +#include +#include + +//! This file contains the trace tool for developmnent purposes. The trace tool +//! just prints messages to a given file. +namespace tracer +{ + +//! This class takes care of the output stream where trace messages will be +//! printed. Its methods are not supposed to be called by anyone else than the +//! "trace" functions. +class tracer_manager +{ + public: + + //! Returns a reference to the file stream. + static std::ofstream& get() + { + if(nullptr==outfile.get()) + { +//TODO: This should not be fixed, but either depend on the OS or be able to +//specified somehow. + outfile.reset(new std::ofstream("/tmp/drumgizmo.trace", + std::ios::app)); + } + + if(nullptr==outfile.get()) + { + throw std::runtime_error("the tracer file could not be initialized"); + } + + return *(outfile.get()); + } + + private: + + //! Single output file. Will be initialized to nullptr when the program + //! starts and freed when the program ends. + static std::unique_ptr outfile; +}; + +//! Trace function without parameters, used on the last call to the variadic +//! templace "trace". +void trace(); + +//! Variadic trace function. Obtains a reference to the file stream and prints +//! its arguments there. +template +void trace(T val, Args... args) +{ + tracer_manager::get()<