1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file LoggerMessage.cpp
//---------------------------------------------------------------------------//
#include "VecGeom/management/LoggerMessage.h"
#include <cassert>
#include <exception>
#include <functional>
#include <sstream>
#include "VecGeom/management/Logger.h"
#include "VecGeom/management/LoggerTypes.h"
namespace vecgeom {
namespace detail {
//---------------------------------------------------------------------------//
/*!
* Construct with reference to function object, etc.
*
* The handle *may be* null, indicating that the output of this message will
* not be displayed.
*/
LoggerMessage::LoggerMessage(LogHandler *handle, Provenance prov, LogLevel lev)
: handle_(handle), prov_(prov), lev_(lev)
{
assert(!handle_ || *handle_);
if (handle_) {
// std::function is defined, so create the output stream
os_ = std::make_unique<std::ostringstream>();
}
}
//---------------------------------------------------------------------------//
/*!
* Flush message on destruction.
*/
LoggerMessage::~LoggerMessage()
{
if (os_) {
try {
auto &os = dynamic_cast<std::ostringstream &>(*os_);
// Write to the handler
(*handle_)(prov_, lev_, os.str());
} catch (std::exception const &e) {
std::cerr << "An error occurred writing a log message: " << e.what() << std::endl;
}
}
}
//---------------------------------------------------------------------------//
} // namespace detail
} // namespace vecgeom
|