17 #include <boost/date_time/posix_time/posix_time.hpp>
23 constexpr std::array<char *const, LogManager::SeverityLevel::error + 1>
35 :
public boost::enable_shared_from_this<LogManager::InternalData> {
39 if (!this->str().empty()) {
51 if (!this->str().empty()) {
66 if (!this->str().empty()) {
83 setp(dummyBuffer, dummyBuffer +
sizeof(dummyBuffer));
84 return (
c == traits_type::eof()) ?
'\0' :
c;
112 return boost::shared_ptr<std::ostream>(shared_from_this(), &strmSelf);
115 return boost::shared_ptr<std::ostream>(shared_from_this(), &strmWorld);
118 return boost::shared_ptr<std::ostream>(shared_from_this(), &strmSync);
121 return boost::shared_ptr<std::ostream>(shared_from_this(), &nullSelf);
125 : worldBuf(comm), syncBuf(comm), strmSelf(&selfBuf), strmWorld(&worldBuf),
126 strmSync(&syncBuf) {}
136 std::map<std::string, LogManager::LoggerType>
142 : cOre(const_cast<
MoFEM::
Core &>(core)) {}
154 PetscInt sev_level = SeverityLevel::inform;
155 PetscBool log_scope = PETSC_FALSE;
156 PetscBool log_quiet = PETSC_FALSE;
157 PetscBool log_no_colors = PETSC_FALSE;
158 PetscBool log_time = PETSC_FALSE;
160 CHKERR PetscOptionsBegin(PETSC_COMM_WORLD,
"log_",
161 "Logging interface options",
"none");
163 CHKERR PetscOptionsEList(
"-severity_level",
"Severity level",
"",
166 CHKERR PetscOptionsEList(
"-sl",
"Severity level",
"",
170 CHKERR PetscOptionsBool(
"-scope",
"Log scope",
"", log_scope, &log_scope,
173 CHKERR PetscOptionsBool(
"-quiet",
"Quiet log attributes",
"", log_quiet,
176 CHKERR PetscOptionsBool(
"-no_color",
"Terminal with no colors",
"",
177 log_no_colors, &log_no_colors, NULL);
178 CHKERR PetscOptionsBool(
"-nc",
"Terminal with no colors",
"", log_no_colors,
179 &log_no_colors, NULL);
181 CHKERR PetscOptionsBool(
"-time",
"Log time",
"",
182 log_time, &log_time, NULL);
184 ierr = PetscOptionsEnd();
187 logging::core::get()->set_filter(MoFEM::LogKeywords::severity >= sev_level);
190 logging::core::get()->add_global_attribute(
"Scope", attrs::named_scope());
205 logging::formatting_ostream &strm) {
209 auto sev = rec[severity];
210 auto p = rec[proc_attr];
211 auto l = rec[line_id];
213 auto tg = rec[tag_attr];
214 auto tl = rec[timeline];
216 auto set_color = [&](
const auto str) {
217 #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
225 auto local_time = boost::posix_time::second_clock::local_time();
226 strm <<
"(Local time ";
227 strm << local_time.date().year() <<
"-" << local_time.date().month()
228 <<
"-" << local_time.date().day() <<
" "
229 << local_time.time_of_day().hours() <<
":"
230 << local_time.time_of_day().minutes() <<
":"
231 << local_time.time_of_day().seconds();
238 set_color(
"\033[32m");
240 set_color(
"\033[0m");
245 case SeverityLevel::error:
246 set_color(
"\033[1m");
247 case SeverityLevel::warning:
248 set_color(
"\033[31m");
250 case SeverityLevel::inform:
251 set_color(
"\033[34m");
253 case SeverityLevel::verbose:
254 set_color(
"\033[35m");
256 case SeverityLevel::noisy:
257 set_color(
"\033[36m");
263 set_color(
"\033[0m");
266 strm << std::hex << std::setw(8) << std::setfill(
'0') <<
l.get()
267 << std::dec << std::setfill(
' ') <<
": ";
270 for (::boost::log::attributes::named_scope_list::const_iterator iter =
272 iter != s->end(); ++iter) {
273 const auto path = std::string(iter->file_name.data());
274 const auto file = path.substr(path.find_last_of(
"/\\") + 1);
275 strm <<
"(" << file <<
":" << iter->line <<
">" << iter->scope_name
283 set_color(
"\033[1m");
284 strm <<
"[" << tg.get() <<
"] ";
285 set_color(
"\033[0m");
289 strm <<
"[" << tl.get() <<
"] ";
292 auto msg = rec[logging::expressions::smessage];
297 boost::shared_ptr<LogManager::SinkType>
299 std::string comm_filter) {
301 auto backend = boost::make_shared<sinks::text_ostream_backend>();
303 backend->add_stream(stream_ptr);
304 backend->auto_flush(
true);
306 auto sink = boost::make_shared<SinkType>(backend);
307 sink->set_filter((expr::has_attr(channel) && channel == comm_filter));
317 auto core_log = logging::core::get();
318 core_log->remove_all_sinks();
320 boost::shared_ptr<std::ostream>(&std::clog, boost::null_deleter()),
336 MPI_Comm_rank(comm, &rank);
337 core_log->add_global_attribute(
"Proc", attrs::constant<unsigned int>(rank));
367 lg.add_attribute(
"LineID", attrs::counter<unsigned int>(1));
370 lg.add_attribute(
"Scope", attrs::named_scope());
382 lg.add_attribute(
"Tag", attrs::constant<std::string>(tag));
386 getLog(channel).add_attribute(
"Tag", attrs::constant<std::string>(tag));
391 LoggerType(boost::log::keywords::channel = channel);
399 "Channel <" + channel +
"> not found");
419 std::array<char, 1024> buff;
421 CHKERR PetscVSNPrintf(buff.data(), 1024, format, &length, Argp);
423 auto get_str = [&buff]() {
428 str = std::string(buff.data());
431 const auto str = get_str();
437 MoFEM::LogManager::SeverityLevel::inform;
438 if (str.find(
"WARNING") != std::string::npos)
439 sev = MoFEM::LogManager::SeverityLevel::warning;
441 std::istringstream is(str);
443 std::vector<std::string> log_list;
445 while (getline(is, line,
'\n'))
446 log_list.push_back(line);
448 if (str.back() !=
'\n') {
454 for(
auto &line : log_list)
466 std::array<char, 1024> buf;
467 vsprintf(buf.data(), fmt,
args);
468 return std::string(buf.data());