mastodonpp/src/log.hpp

58 lines
1.6 KiB
C++
Raw Normal View History

2020-01-05 11:06:29 +01:00
/* This file is part of mastodonpp.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODONPP_LOG_HPP
#define MASTODONPP_LOG_HPP
#include <iostream>
#include <string_view>
2020-01-05 11:06:29 +01:00
namespace mastodonpp
{
2020-11-13 13:45:59 +01:00
using std::cerr; // NOLINT(misc-unused-using-decls)
using std::string_view;
2020-01-06 10:03:18 +01:00
//! @private
constexpr auto shorten_filename(const string_view &filename)
{
2020-11-13 12:01:18 +01:00
for (const string_view dir : {"/src/", "/include/"})
{
const auto pos{filename.rfind(dir)};
if (pos != string_view::npos)
{
return filename.substr(pos + dir.size());
}
}
return filename;
}
2020-01-05 11:06:29 +01:00
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
2020-11-13 13:45:59 +01:00
#define commonlog \
cerr << '[' << shorten_filename(__FILE__) << ':' << __LINE__ << ']'
2020-01-05 11:06:29 +01:00
#ifndef NDEBUG
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
2020-11-13 13:45:59 +01:00
# define debuglog commonlog << " DEBUG: "
2020-01-05 11:06:29 +01:00
#else
2020-11-13 13:45:59 +01:00
# define debuglog false && cerr
2020-01-05 11:06:29 +01:00
#endif
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
2020-01-05 20:39:05 +01:00
#define errorlog commonlog << " ERROR: "
2020-01-05 11:06:29 +01:00
} // namespace mastodonpp
2020-11-13 13:45:59 +01:00
#endif // MASTODONPP_LOG_HPP