diff --git a/src/debug_utils.h b/src/debug_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..b4d3d183e9a8aba6ea35200ecd8fb8d6f5bfcd5a --- /dev/null +++ b/src/debug_utils.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2018 Savoir-faire Linux Inc. + * + * Author: Philippe Gorley <philippe.gorley@savoirfairelinux.com> + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 more 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include "config.h" + +#include <chrono> +#include <ratio> + +#warning Debug utilities included in build + +using Clock = std::chrono::steady_clock; + +namespace ring { namespace debug { + +/** + * Ex: + * Timer t; + * std::this_thread::sleep_for(std::chrono::milliseconds(10)); + * RING_DBG() << "Task took " << t.getDuration<std::chrono::nanoseconds>() << " ns"; + */ +class Timer +{ +public: + Timer() { start_ = Clock::now(); } + + template<class Period = std::ratio<1>> + uint64_t getDuration() + { + auto diff = std::chrono::duration_cast<Period>(Clock::now() - start_); + return diff.count(); + } + +private: + std::chrono::time_point<Clock> start_; +}; + +}} // namespace ring::debug