14#include "arccore/base/ReferenceCounter.h"
16#include "arcane/utils/ITraceMng.h"
17#include "arcane/utils/StringBuilder.h"
18#include "arcane/utils/Iostream.h"
19#include "arcane/utils/ScopedPtr.h"
20#include "arcane/utils/PlatformUtils.h"
21#include "arcane/utils/String.h"
22#include "arcane/utils/TraceClassConfig.h"
23#include "arcane/utils/Array.h"
24#include "arcane/utils/ValueConvert.h"
25#include "arcane/utils/Iterator.h"
26#include "arcane/utils/ITraceMngPolicy.h"
28#include "arcane/core/XmlNode.h"
29#include "arcane/core/XmlNodeList.h"
30#include "arcane/core/IApplication.h"
31#include "arcane/core/IXmlDocumentHolder.h"
60 , m_is_parallel(
false)
62 , m_is_master_has_output_file(
false)
63 , m_stdout_verbosity_level(Trace::UNSPECIFIED_VERBOSITY_LEVEL)
64 , m_verbosity_level(Trace::UNSPECIFIED_VERBOSITY_LEVEL)
65 , m_is_parallel_output(
false)
80 bool isParallel()
const override {
return m_is_parallel; }
82 bool isDebug()
const override {
return m_is_debug; }
84 bool isParallelOutput()
const override {
return m_is_parallel_output; }
86 Int32 standardOutputVerbosityLevel()
const override {
return m_stdout_verbosity_level; }
88 Int32 verbosityLevel()
const override {
return m_verbosity_level; }
90 bool isMasterHasOutputFile()
const override {
return m_is_master_has_output_file; }
92 void setDefaultClassConfigXmlBuffer(
ByteConstSpan bytes)
override;
96 IApplication* m_application;
99 bool m_is_master_has_output_file;
100 Int32 m_stdout_verbosity_level;
101 Int32 m_verbosity_level;
102 std::map<String, Arccore::ReferenceCounter<ITraceStream>> m_output_files;
103 String m_output_file_prefix;
104 bool m_is_parallel_output;
105 ScopedPtrT<IXmlDocumentHolder> m_default_config_doc;
106 std::mutex m_init_mutex;
107 std::mutex m_getfile_mutex;
111 ITraceStream* _getFile(
const String& rank);
112 void _initializeTraceClasses(ITraceMng* trace);
113 void _setAllTraceClassConfig(ITraceMng* trace,
ByteConstArrayView bytes,
bool do_log);
114 void _setAllTraceClassConfig(ITraceMng* trace, XmlNode root_element,
bool do_log);
115 void _setAllTraceClassConfig(ITraceMng* trace, IXmlDocumentHolder* doc,
bool do_log);
116 void _initializeTraceMng(ITraceMng* trace,
bool is_master,
const String& rank_str);
136 _initializeTraceMng(trace, rank == 0, String::fromNumber(rank));
143_initializeTraceMng(
ITraceMng* trace,
bool is_master,
const String& rank_str)
145 bool is_output_file =
false;
148 trace_id += rank_str;
154 bool is_info_disabled = !is_master;
155 if (m_is_parallel_output) {
156 is_info_disabled =
false;
157 is_output_file =
true;
159 if (is_master && m_is_master_has_output_file)
160 is_output_file =
true;
164 if (is_output_file) {
165 ITraceStream* ofile = _getFile(rank_str);
169 _initializeTraceClasses(trace);
180 _initializeTraceMng(trace,
false, file_suffix);
193 if (m_verbosity_level != Trace::UNSPECIFIED_VERBOSITY_LEVEL) {
194 Int32 level = m_verbosity_level;
195 if (minimal_level != Trace::UNSPECIFIED_VERBOSITY_LEVEL)
196 level = std::max(level, minimal_level);
199 if (m_stdout_verbosity_level != Trace::UNSPECIFIED_VERBOSITY_LEVEL) {
200 Int32 level = m_stdout_verbosity_level;
201 if (minimal_level != Trace::UNSPECIFIED_VERBOSITY_LEVEL)
202 level = std::max(level, minimal_level);
213 _setAllTraceClassConfig(trace, bytes,
false);
220setDefaultClassConfigXmlBuffer(ByteConstSpan bytes)
227 ITraceMng* tm = m_application->
traceMng();
235_getFile(
const String& rank)
237 std::lock_guard<std::mutex> guard(m_getfile_mutex);
239 auto i = m_output_files.find(rank);
240 if (i != m_output_files.end())
241 return i->second.get();
243 StringBuilder buf(m_output_file_prefix);
244 if (!m_output_file_prefix.null()) {
249 String bufstr = buf.toString();
251 ReferenceCounter<ITraceStream> stream(ITraceStream::createFileStream(bufstr));
252 m_output_files.insert(std::make_pair(rank, stream));
262 _setAllTraceClassConfig(trace, m_default_config_doc.get(),
true);
271 ScopedPtrT<IXmlDocumentHolder> config_doc;
272 if (!bytes.empty()) {
275 ITraceMng* print_tm = m_application->traceMng();
278 _setAllTraceClassConfig(trace, config_doc.get(), do_log);
289 XmlNode root_element;
291 XmlNode document_node = m_default_config_doc->documentNode();
292 if (!document_node.null())
293 root_element = document_node.documentElement();
295 _setAllTraceClassConfig(trace, root_element, do_log);
305 trace->logdate() <<
"Reading trace classes";
307 trace->removeAllClassConfig();
308 bool is_info_activated = trace->isInfoActivated();
313 if (!is_info_activated && !m_is_parallel_output)
314 debug_level = Trace::None;
315 TraceClassConfig medium_cc(is_info_activated,
true, debug_level);
316 trace->setClassConfig(
"*", medium_cc);
317 if (root_element.null()) {
319 trace->log() <<
"No user configuration";
323 XmlNodeList children = root_element.child(
"traces").children(
"trace-class");
324 String ustr_name(
"name");
325 String ustr_info(
"info");
326 String ustr_pinfo(
"parallel-info");
327 String ustr_debug(
"debug");
328 String ustr_true(
"true");
329 String ustr_none(
"none");
330 String ustr_lowest(
"lowest");
331 String ustr_low(
"low");
332 String ustr_medium(
"medium");
333 String ustr_high(
"high");
334 String ustr_highest(
"highest");
335 String ustr_star(
"*");
336 String ustr_print_class_name(
"print-class-name");
337 String ustr_print_elapsed_time(
"print-elapsed-time");
339 for (
const auto& xnode : children) {
340 String module_name = xnode.attrValue(ustr_name);
341 String activate_str = xnode.attrValue(ustr_info);
342 String parallel_activate_str = xnode.attrValue(ustr_pinfo);
343 String dbg_lvl_str = xnode.attrValue(ustr_debug);
344 String print_class_name_str = xnode.attrValue(ustr_print_class_name);
345 String print_elapsed_time_str = xnode.attrValue(ustr_print_elapsed_time);
346 if (module_name.null())
348 TraceClassConfig def_config = trace->classConfig(
"*");
349 bool is_activate = def_config.isActivated();
350 builtInGetValue(is_activate, activate_str);
351 bool is_parallel_activate = is_activate;
354 if (!is_info_activated) {
356 if (parallel_activate_str == ustr_true)
362 if (dbg_lvl_str == ustr_none)
363 dbg_lvl = Trace::None;
364 if (dbg_lvl_str == ustr_lowest)
366 if (dbg_lvl_str == ustr_low)
368 if (dbg_lvl_str == ustr_medium)
370 if (dbg_lvl_str == ustr_high)
372 if (dbg_lvl_str == ustr_highest)
376 bool is_print_class_name =
true;
377 builtInGetValue(is_print_class_name, print_class_name_str);
378 bool is_print_elapsed_time =
false;
379 builtInGetValue(is_print_elapsed_time, print_elapsed_time_str);
380 int flags = Trace::PF_Default;
381 if (!is_print_class_name)
383 if (is_print_elapsed_time)
386 TraceClassConfig mc(is_activate, is_parallel_activate, dbg_lvl, flags);
388 trace->log() <<
"Config " << mc.isActivated() <<
' '
389 << mc.isParallelActivated() <<
' ' << module_name;
390 trace->setClassConfig(module_name, mc);
392 trace->log() <<
"Config module class"
393 <<
" name=" << module_name
394 <<
" activated=" << is_activate
395 <<
" dbglvl=" << (int)dbg_lvl;
virtual ITraceMng * traceMng() const =0
Trace manager.
Interface for the configuration manager of a trace manager.
virtual bool setInfoActivated(bool v)=0
Modifies the activation state of info messages.
virtual void setMaster(bool is_master)=0
Sets the 'master' state of the instance.
virtual ARCCORE_DEPRECATED_2018 void setRedirectStream(std::ostream *o)=0
Redirects all messages to the stream o.
virtual void setStandardOutputVerbosityLevel(Int32 level)=0
Sets the verbosity level of outputs on std::cout.
virtual Int32 standardOutputVerbosityLevel() const =0
Message verbosity level on std::cout.
virtual void finishInitialize()=0
Finishes initialization.
virtual void setTraceId(const String &id)=0
Sets the manager identifier.
virtual Int32 verbosityLevel() const =0
Message verbosity level.
virtual void setVerbosityLevel(Int32 level)=0
Sets the verbosity level of the outputs.
Manager of a DOM document.
static IXmlDocumentHolder * createNull()
Creates and returns a null document.
static IXmlDocumentHolder * loadFromBuffer(Span< const Byte > buffer, const String &name, ITraceMng *tm)
Loads an XML document.
Unicode character string constructor.
String toString() const
Returns the constructed character string.
Unicode character string.
Management of trace behavior.
void setStandardOutputVerbosityLevel(Int32 level) override
Verbosity level for the standard output stream (stdout).
void setIsParallelOutput(bool v) override
Indicates if all ranks output traces to a file in parallel.
void setClassConfigFromXmlBuffer(ITraceMng *trace, ByteConstArrayView bytes) override
Sets the values of the TraceClassConfig of trace via the data contained in bytes.
void setIsDebug(bool v) override
Indicates if debug outputs are active.
void setIsParallel(bool v) override
Indicates if parallelism is active.
void setIsMasterHasOutputFile(bool active) override
Indicates if a master ITraceMng outputs traces to a file in addition to standard output.
void initializeTraceMng(ITraceMng *trace, Int32 rank) override
Initializes trace.
void setDefaultVerboseLevel(ITraceMng *trace, Int32 minimal_level) override
void setVerbosityLevel(Int32 level) override
Verbosity level.
void build() override
Constructs the instance.
eDebugLevel
Trace debug level.
@ Medium
Medium level (default).
@ Lowest
No debug information.
@ PF_ElapsedTime
Display of elapsed time.
@ PF_NoClassName
Display or not the message class.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Span< const std::byte > ByteConstSpan
Read-only view of a 1D array of characters.
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
std::int32_t Int32
Signed integer type of 32 bits.