Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
ObjectWithTrace.h
1/*
2 * Copyright 2020 IFPEN-CEA
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 */
18
19#pragma once
20
21#include <alien/data/Universe.h>
22#include <alien/utils/Precomp.h>
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Alien
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33/*
34 * Objet permettant les traces utilisateurs.
35 * Utilisation via section de trace definies par lambda
36 * Le but est de limiter les problèmes et tests de traces si ITraceMng est
37 * nullptr De plus, les actions d'écritures sont atomiques. Il est facile d'améliorer
38 * l'implémentation pour les packer ou pour gérer le multithreading.
39 *
40 * Activation des traces d'Alien:
41 *
42 * Alien::ITraceMng* trace = ...
43 * Alien::setTraceMng(trace);
44 *
45 * Niveau de verbosite:
46 *
47 * Alien::setVerbosity(Alien::Verbosity::Debug);
48 *
49 * Utilisation:
50 *
51 * alien_debug([&] {
52 * ... message à imprimer ...
53 * ... on peut utiliser debug() << ... ou info(), warning(), etc.
54 * ... En pratique, utiliser cout() de preference ...
55 * });
56 *
57 *
58 */
59
60class ObjectWithTrace
61{
62 public:
63 ObjectWithTrace()
64 : m_is_locked(true)
65 {}
66
67 virtual ~ObjectWithTrace() {}
68
69 public:
70 template <typename T>
71 void alien_info(T&& t) const
72 {
73 _print(Verbosity::Info, std::move(t));
74 }
75
76 template <typename T>
77 void alien_debug(T&& t) const
78 {
79 _print(Verbosity::Debug, std::move(t));
80 }
81
82 template <typename T>
83 void alien_warning(T&& t) const
84 {
85 _print(Verbosity::Warning, std::move(t));
86 }
87
88 template <typename T>
89 void alien_fatal(T&& t) const
90 {
91 auto* trace = traceMng();
92 if (trace != nullptr) {
93 m_is_locked = false;
94 t();
95 m_is_locked = true;
96 }
97 throw FatalErrorException(
98 A_FUNCINFO, "Fatal error in Alien - for more details, increase verbosity level");
99 }
100
101 protected:
102 // Trace a utiliser dans les sections de trace
103 TraceMessage cout() const
104 {
105 _checkLock();
106 return traceMng()->info();
107 }
108
109 private:
110 void _checkLock() const
111 {
112 if (m_is_locked) {
113 throw FatalErrorException("Trace in Alien should be done using trace section");
114 }
115 }
116
117 template <typename T>
118 void _print(Verbosity::Level N, T&& t) const
119 {
120 auto* trace = traceMng();
121 if (trace == nullptr)
122 return;
123 m_is_locked = false;
124 if (Universe().verbosityLevel() <= N) {
125 t();
126 }
127 m_is_locked = true;
128 }
129
130 public:
131 ITraceMng* traceMng() const { return Universe().traceMng(); }
132
133 private:
134 // NB: on ne stocke pas le ITraceMng* car on ne sait pas s'il sera
135 // positionne apres la construction de l'objet...
136
137 // Permet de bloquer pour forcer l'affichage via les fonctions de print
138 mutable bool m_is_locked;
139};
140
141/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
143
144} // namespace Alien
145
146/*---------------------------------------------------------------------------*/
147/*---------------------------------------------------------------------------*/
Universe.h.
Level
Verbosity level.
Definition Universe.h:37
Alien universe. Common structure to store shared objects between all elements of the library Alien.
Definition Universe.h:53
Arccore::ITraceMng * traceMng() const
Get the trace manager.
Definition Universe.cc:110
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17