aboutsummaryrefslogtreecommitdiff
path: root/Source/ablastr/warn_manager/WarnManager.cpp
blob: 2686907b1814f3e2ebf86065ead9c9a17f5c4667 (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Copyright 2022 Luca Fedeli
 *
 * This file is part of WarpX.
 *
 * License: BSD-3-Clause-LBNL
 */

#include "WarnManager.H"

#include "ablastr/utils/msg_logger/MsgLogger.H"
#include "ablastr/utils/text/StringUtils.H"
#include "ablastr/utils/TextMsg.H"

#include <AMReX_ParallelDescriptor.H>

#include <algorithm>
#include <sstream>

namespace abl_msg_logger = ablastr::utils::msg_logger;
using namespace ablastr::warn_manager;

namespace
{
    WarnPriority MapPriorityToWarnPriority (
        const abl_msg_logger::Priority& priority)
    {
        using namespace abl_msg_logger;
        if (priority == Priority::low)
            return WarnPriority::low;
        else if (priority == Priority::medium)
            return WarnPriority::medium;
        else if (priority == Priority::high)
            return WarnPriority::high;
        else
            ABLASTR_ABORT_WITH_MESSAGE(
                "Parsing Priority to WarnPriority has failed");

        return WarnPriority::high;
    }
}

WarnManager& WarnManager::GetInstance() {
    static auto warn_manager = WarnManager{};
    return warn_manager;
}

WarnManager::WarnManager():
    m_rank{amrex::ParallelDescriptor::MyProc()},
    m_p_logger{std::make_unique<abl_msg_logger::Logger>()}
{}

void WarnManager::RecordWarning(
            std::string topic,
            std::string text,
            WarnPriority priority)
{
    auto msg_priority = abl_msg_logger::Priority::high;
    if(priority == WarnPriority::low)
        msg_priority = abl_msg_logger::Priority::low;
    else if(priority == WarnPriority::medium)
        msg_priority = abl_msg_logger::Priority::medium;

    if(m_always_warn_immediately){

        amrex::Warning(
            ablastr::utils::TextMsg::Warn(
                "["
                + std::string(abl_msg_logger::PriorityToString(msg_priority))
                + "]["
                + topic
                + "] "
                + text));
    }

#ifdef AMREX_USE_OMP
    #pragma omp critical
#endif
    {
        m_p_logger->record_msg(abl_msg_logger::Msg{topic, text, msg_priority});
    }

    if(m_abort_on_warning_threshold){

        auto abort_priority = abl_msg_logger::Priority::high;
        if(m_abort_on_warning_threshold == WarnPriority::low)
            abort_priority = abl_msg_logger::Priority::low;
        else if(m_abort_on_warning_threshold == WarnPriority::medium)
            abort_priority = abl_msg_logger::Priority::medium;

        ABLASTR_ALWAYS_ASSERT_WITH_MESSAGE(
            msg_priority < abort_priority,
            "A warning with priority '"
            + abl_msg_logger::PriorityToString(msg_priority)
            + "' has been raised."
        );
    }
}

std::string WarnManager::PrintLocalWarnings(const std::string& when) const
{
    auto all_warnings = m_p_logger->get_msgs_with_counter();
    std::sort(all_warnings.begin(), all_warnings.end(),
        [](const auto& a, const auto& b){return a.msg < b.msg;});

    std::stringstream ss;

    ss << "\n" << WarnManager::GetHeader(when, warn_line_size, false);

    if(all_warnings.empty()){
        ss << "* No recorded warnings.\n";
    }
    else{
        for(const auto& warn_msg : all_warnings){
            ss << PrintWarnMsg(warn_msg);
            ss << "*\n";
        }
    }

    ss << std::string(warn_line_size, '*') << "\n\n" ;

    return ss.str();
}

std::string WarnManager::PrintGlobalWarnings(const std::string& when) const
{
    auto all_warnings =
        m_p_logger->collective_gather_msgs_with_counter_and_ranks();

    if(m_rank != amrex::ParallelDescriptor::IOProcessorNumber())
        return "[see I/O rank message]";

    std::sort(all_warnings.begin(), all_warnings.end(),
        [](const auto& a, const auto& b){
            return a.msg_with_counter.msg < b.msg_with_counter.msg;});

    std::stringstream ss;

    ss << "\n" << WarnManager::GetHeader(when, warn_line_size, true);

    if(all_warnings.empty()){
        ss << "* No recorded warnings.\n";
    }
    else{
        for(const auto& warn_msg : all_warnings){
            ss << PrintWarnMsg(warn_msg);
            ss << "*\n";
        }
    }

    ss << std::string(warn_line_size, '*') << "\n\n" ;

    return ss.str();
}

void WarnManager::SetAlwaysWarnImmediately(bool always_warn_immediately)
{
    m_always_warn_immediately = always_warn_immediately;
}

bool WarnManager::GetAlwaysWarnImmediatelyFlag() const
{
    return m_always_warn_immediately;
}

void WarnManager::SetAbortThreshold(std::optional<WarnPriority> abort_threshold)
{
    m_abort_on_warning_threshold = abort_threshold;
}

std::optional<WarnPriority> WarnManager::GetAbortThreshold() const
{
    return m_abort_on_warning_threshold;
}

void WarnManager::debug_read_warnings_from_input(amrex::ParmParse& params)
{
    std::vector<std::string> warnings;
    params.queryarr("test_warnings", warnings);

    for (const auto& warn : warnings){
        amrex::ParmParse pp_warn(warn);

        std::string topic;
        pp_warn.query("topic", topic);

        std::string msg;
        pp_warn.query("msg", msg);

        std::string spriority;
        pp_warn.query("priority", spriority);
        const auto wpriority = MapPriorityToWarnPriority(
            abl_msg_logger::StringToPriority(spriority));

        int all_involved = 0;
        pp_warn.query("all_involved", all_involved);
        if(all_involved != 0){
            this->RecordWarning(topic, msg, wpriority);
        }
        else{
            std::vector<int> who_involved;
            pp_warn.queryarr("who_involved", who_involved);
            if(std::find (who_involved.begin(), who_involved.end(), m_rank)
                != who_involved.end()){
                this->RecordWarning(topic, msg, wpriority);
            }
        }
    }

}

std::string WarnManager::PrintWarnMsg(
    const abl_msg_logger::MsgWithCounter& msg_with_counter) const
{
    std::stringstream ss;
    ss << "* --> ";
    if (msg_with_counter.msg.priority == abl_msg_logger::Priority::high)
        ss << "[!!!]";
    else if (msg_with_counter.msg.priority == abl_msg_logger::Priority::medium)
        ss << "[!! ]";
    else if (msg_with_counter.msg.priority == abl_msg_logger::Priority::low)
        ss << "[!  ]";
    else
        ss << "[???]";

    ss << " [" + msg_with_counter.msg.topic << "] ";

    if(msg_with_counter.counter == 2)
        ss << "[raised twice]\n";
    else if(msg_with_counter.counter == 1)
        ss << "[raised once]\n";
    else
        ss << "[raised " << msg_with_counter.counter << " times]\n";

    ss << MsgFormatter(msg_with_counter.msg.text, warn_line_size, warn_tab_size);

    return ss.str();
}

std::string WarnManager::PrintWarnMsg(
    const abl_msg_logger::MsgWithCounterAndRanks& msg_with_counter_and_ranks) const
{
    std::stringstream ss;
    ss << this->PrintWarnMsg(msg_with_counter_and_ranks.msg_with_counter);

    std::string raised_by = "@ Raised by: ";
    if (!msg_with_counter_and_ranks.all_ranks){
        for (const auto rr : msg_with_counter_and_ranks.ranks)
            raised_by += " " + std::to_string(rr);
    }
    else{
        raised_by += "ALL\n";
    }
    ss << WarnManager::MsgFormatter(raised_by, warn_line_size, warn_tab_size);

    return ss.str();
}

std::string WarnManager::GetHeader(
    const std::string& when,
    const int line_size,
    const bool is_global)
{
    const std::string warn_header{"**** WARNINGS "};

    std::stringstream ss;

    ss << warn_header <<
        std::string(line_size - static_cast<int>(warn_header.length()), '*') << "\n" ;

    if(is_global){
        ss << "* GLOBAL warning list  after " << " [ " <<  when << " ]\n*\n";
    }
    else{
        auto const mpi_rank = amrex::ParallelDescriptor::MyProc();
        ss << "* LOCAL" << " ( rank # " << mpi_rank << " ) "
            << " warning list  after " <<  when << "\n*\n";
    }

    return ss.str();
}

std::string
WarnManager::MsgFormatter(
        const std::string& msg,
        const int line_size,
        const int tab_size)
{
    const auto prefix = "*" + std::string(tab_size, ' ');
    const auto prefix_length = static_cast<int>(prefix.length());

    const auto wrapped_text = ablastr::utils::text::automatic_text_wrap(
        msg, line_size-prefix_length);

    std::stringstream ss_out;
    for (const auto& line : wrapped_text)
        ss_out << prefix << line << "\n";

    return ss_out.str();
}

WarnManager& ablastr::warn_manager::GetWMInstance()
{
    return WarnManager::GetInstance();
}

void ablastr::warn_manager::WMRecordWarning(
    std::string topic,
    std::string text,
    WarnPriority priority)
{
    WarnManager::GetInstance().RecordWarning(
        topic, text, priority);
}