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);
184 _initializeTraceMng(trace,
false, file_suffix);
197 if (m_verbosity_level != Trace::UNSPECIFIED_VERBOSITY_LEVEL) {
198 Int32 level = m_verbosity_level;
199 if (minimal_level != Trace::UNSPECIFIED_VERBOSITY_LEVEL)
200 level = std::max(level, minimal_level);
203 if (m_stdout_verbosity_level != Trace::UNSPECIFIED_VERBOSITY_LEVEL) {
204 Int32 level = m_stdout_verbosity_level;
205 if (minimal_level != Trace::UNSPECIFIED_VERBOSITY_LEVEL)
206 level = std::max(level, minimal_level);
217 _setAllTraceClassConfig(trace, bytes,
false);
224setDefaultClassConfigXmlBuffer(ByteConstSpan bytes)
231 ITraceMng* tm = m_application->
traceMng();
239_getFile(
const String& rank)
241 std::lock_guard<std::mutex> guard(m_getfile_mutex);
243 auto i = m_output_files.find(rank);
244 if (i != m_output_files.end())
245 return i->second.get();
247 StringBuilder buf(m_output_file_prefix);
248 if (!m_output_file_prefix.null()) {
253 String bufstr = buf.toString();
255 ReferenceCounter<ITraceStream> stream(ITraceStream::createFileStream(bufstr));
256 m_output_files.insert(std::make_pair(rank, stream));
266 _setAllTraceClassConfig(trace, m_default_config_doc.get(),
true);
275 ScopedPtrT<IXmlDocumentHolder> config_doc;
276 if (!bytes.empty()) {
279 ITraceMng* print_tm = m_application->traceMng();
282 _setAllTraceClassConfig(trace, config_doc.get(), do_log);
293 XmlNode root_element;
295 XmlNode document_node = m_default_config_doc->documentNode();
296 if (!document_node.null())
297 root_element = document_node.documentElement();
299 _setAllTraceClassConfig(trace, root_element, do_log);
309 trace->logdate() <<
"Reading trace classes";
311 trace->removeAllClassConfig();
312 bool is_info_activated = trace->isInfoActivated();
317 if (!is_info_activated && !m_is_parallel_output)
318 debug_level = Trace::None;
319 TraceClassConfig medium_cc(is_info_activated,
true, debug_level);
320 trace->setClassConfig(
"*", medium_cc);
321 if (root_element.null()) {
323 trace->log() <<
"No user configuration";
327 XmlNodeList children = root_element.child(
"traces").children(
"trace-class");
328 String ustr_name(
"name");
329 String ustr_info(
"info");
330 String ustr_pinfo(
"parallel-info");
331 String ustr_debug(
"debug");
332 String ustr_true(
"true");
333 String ustr_none(
"none");
334 String ustr_lowest(
"lowest");
335 String ustr_low(
"low");
336 String ustr_medium(
"medium");
337 String ustr_high(
"high");
338 String ustr_highest(
"highest");
339 String ustr_star(
"*");
340 String ustr_print_class_name(
"print-class-name");
341 String ustr_print_elapsed_time(
"print-elapsed-time");
343 for (
const auto& xnode : children) {
344 String module_name = xnode.attrValue(ustr_name);
345 String activate_str = xnode.attrValue(ustr_info);
346 String parallel_activate_str = xnode.attrValue(ustr_pinfo);
347 String dbg_lvl_str = xnode.attrValue(ustr_debug);
348 String print_class_name_str = xnode.attrValue(ustr_print_class_name);
349 String print_elapsed_time_str = xnode.attrValue(ustr_print_elapsed_time);
350 if (module_name.null())
352 TraceClassConfig def_config = trace->classConfig(
"*");
353 bool is_activate = def_config.isActivated();
354 builtInGetValue(is_activate, activate_str);
355 bool is_parallel_activate = is_activate;
358 if (!is_info_activated) {
360 if (parallel_activate_str == ustr_true)
366 if (dbg_lvl_str == ustr_none)
367 dbg_lvl = Trace::None;
368 if (dbg_lvl_str == ustr_lowest)
370 if (dbg_lvl_str == ustr_low)
372 if (dbg_lvl_str == ustr_medium)
374 if (dbg_lvl_str == ustr_high)
376 if (dbg_lvl_str == ustr_highest)
380 bool is_print_class_name =
true;
381 builtInGetValue(is_print_class_name, print_class_name_str);
382 bool is_print_elapsed_time =
false;
383 builtInGetValue(is_print_elapsed_time, print_elapsed_time_str);
384 int flags = Trace::PF_Default;
385 if (!is_print_class_name)
387 if (is_print_elapsed_time)
390 TraceClassConfig mc(is_activate, is_parallel_activate, dbg_lvl, flags);
392 trace->log() <<
"Config " << mc.isActivated() <<
' '
393 << mc.isParallelActivated() <<
' ' << module_name;
394 trace->setClassConfig(module_name, mc);
396 trace->log() <<
"Config module class"
397 <<
" name=" << module_name
398 <<
" activated=" << is_activate
399 <<
" 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 String errorFileName()=0
Gets the error file name.
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.