Chaste  Release::2018.1
Warnings.cpp
1 /*
2 
3 Copyright (c) 2005-2018, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF 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 "BoostFilesystem.hpp"
45 #include "FileFinder.hpp"
46 #include "PosixPathFixer.hpp"
47 #include "GetCurrentWorkingDirectory.hpp"
48 
50 
52 {
53 }
54 
56 {
57  PrintWarnings();
58  QuietDestroy();
59 }
60 
62 {
63  if (mpInstance)
64  {
65  delete mpInstance;
66  mpInstance = nullptr;
67  }
68 }
69 
71 {
72  if (mpInstance)
73  {
74  for (WarningsContainerType::iterator it = mpInstance->mWarningMessages.begin();
75  it != mpInstance->mWarningMessages.end();
76  ++it)
77  {
78  /*
79  * Look at my warnings please.
80  * First in pair is the context.
81  * Second in pair is that actual warning.
82  */
83  std::cout << it->first << it->second << std::endl;
84  }
85  }
86 }
87 
89 {
90  if (mpInstance == nullptr)
91  {
92  mpInstance = new Warnings();
93  std::atexit(NoisyDestroy);
94  }
95  return mpInstance;
96 }
97 
98 void Warnings::AddWarning(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber, bool onlyOnce)
99 {
100  std::string posix_filename(ChastePosixPathFixer::ToPosix(fs::path(rFilename)));
101  std::stringstream line_number_stream;
102  line_number_stream << lineNumber;
103  std::string context("Chaste warning: in file " + posix_filename + " at line " + line_number_stream.str() + ": ");
104  std::pair<std::string, std::string> item(context, rMessage);
105 
106  if (onlyOnce)
107  {
108  WarningsContainerType::iterator it = find(mWarningMessages.begin(), mWarningMessages.end(), item);
109  if (it != mWarningMessages.end())
110  {
111  return;
112  }
113  }
114 
115  mWarningMessages.push_back(item);
116  LOG(1, context + rMessage);
117 }
118 
120 {
121  return mWarningMessages.size();
122 }
123 
125 {
126  if (mWarningMessages.empty())
127  {
128  EXCEPTION("There are no warnings");
129  }
130  std::string message = mWarningMessages.front().second; // Second in pair is the actual warning
131  mWarningMessages.pop_front();
132 
133  return message;
134 }
unsigned GetNumWarnings()
Definition: Warnings.cpp:119
#define EXCEPTION(message)
Definition: Exception.hpp:143
static Warnings * Instance()
Definition: Warnings.cpp:88
static void QuietDestroy()
Definition: Warnings.cpp:61
static void PrintWarnings()
Definition: Warnings.cpp:70
static std::string ToPosix(const fs::path path)
std::string GetNextWarningMessage()
Definition: Warnings.cpp:124
static Warnings * mpInstance
Definition: Warnings.hpp:58
WarningsContainerType mWarningMessages
Definition: Warnings.hpp:63
Warnings()
Definition: Warnings.cpp:51
static void NoisyDestroy()
Definition: Warnings.cpp:55
void AddWarning(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber, bool onlyOnce=false)
Definition: Warnings.cpp:98