Chaste Commit::8b5d759ac2eb95e67ae57699734101efccb0a0a9
Warnings.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include <cstdlib>
37#include <cassert>
38#include <iostream>
39#include <algorithm>
40
41#include "Warnings.hpp"
42#include "Exception.hpp"
43#include "LogFile.hpp"
44#include "FileFinder.hpp"
45#include "PosixPathFixer.hpp"
46#include "GetCurrentWorkingDirectory.hpp"
47
49
53
55{
58}
59
61{
62 if (mpInstance)
63 {
64 delete mpInstance;
65 mpInstance = nullptr;
66 }
67}
68
70{
71 if (mpInstance)
72 {
73 for (WarningsContainerType::iterator it = mpInstance->mWarningMessages.begin();
74 it != mpInstance->mWarningMessages.end();
75 ++it)
76 {
77 /*
78 * Look at my warnings please.
79 * First in pair is the context.
80 * Second in pair is that actual warning.
81 */
82 std::cout << it->first << it->second << std::endl;
83 }
84 }
85}
86
88{
89 if (mpInstance == nullptr)
90 {
91 mpInstance = new Warnings();
92 std::atexit(NoisyDestroy);
93 }
94 return mpInstance;
95}
96
97void Warnings::AddWarning(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber, bool onlyOnce)
98{
99 std::string posix_filename(ChastePosixPathFixer::ToPosix(fs::path(rFilename)));
100 std::stringstream line_number_stream;
101 line_number_stream << lineNumber;
102 std::string context("Chaste warning: in file " + posix_filename + " at line " + line_number_stream.str() + ": ");
103 std::pair<std::string, std::string> item(context, rMessage);
104
105 if (onlyOnce)
106 {
107 WarningsContainerType::iterator it = find(mWarningMessages.begin(), mWarningMessages.end(), item);
108 if (it != mWarningMessages.end())
109 {
110 return;
111 }
112 }
113
114 mWarningMessages.push_back(item);
115 LOG(1, context + rMessage);
116}
117
119{
120 return mWarningMessages.size();
121}
122
124{
125 if (mWarningMessages.empty())
126 {
127 EXCEPTION("There are no warnings");
128 }
129 std::string message = mWarningMessages.front().second; // Second in pair is the actual warning
130 mWarningMessages.pop_front();
131
132 return message;
133}
#define EXCEPTION(message)
static std::string ToPosix(const fs::path path)
static void QuietDestroy()
Definition Warnings.cpp:60
void AddWarning(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber, bool onlyOnce=false)
Definition Warnings.cpp:97
WarningsContainerType mWarningMessages
Definition Warnings.hpp:63
unsigned GetNumWarnings()
Definition Warnings.cpp:118
static void NoisyDestroy()
Definition Warnings.cpp:54
static Warnings * mpInstance
Definition Warnings.hpp:58
std::string GetNextWarningMessage()
Definition Warnings.cpp:123
static Warnings * Instance()
Definition Warnings.cpp:87
static void PrintWarnings()
Definition Warnings.cpp:69